0

我是 R 新手,我想基于一个名为 GWModel 的 R 包构建我自己的 Shiny 应用程序。这是我的代码

library(shiny)
library(GWmodel)
library(sp)

ui<-shinyUI(fluidPage(
    titlePanel("Model"),
    
    
    sidebarLayout(
        sidebarPanel(
            textInput(inputId = "tanah",
                      label = "Luas Tanah",
                      value = 72),
            textInput(inputId = "bangunan",
                      label = "Luas Bangunan",
                      value = 36),
            textInput(inputId = "lat",
                      label = "Latitude",
                      value = -6.401150),
            textInput(inputId = "long",
                      label = "Longitude",
                      value = 106.770416),
            actionButton("Run_model", "Run model")
        ),
        
        mainPanel(
            
            tabsetPanel(
                tabPanel("model summary", tableOutput('summary'), verbatimTextOutput('summary2'))
            )
        )
    )))


server<- function(input,output,session){
    
    #Train data
    train.data <- read.csv("depoklagi.csv")
    
    
    
    #Train Data SPDF
    train.data.spdf <- SpatialPointsDataFrame(coords = train.data[,c("longlamudi","latlamudi")],
                                              data = train.data[,c("hrglamudi","jdllamudi","ltxlamudi","lbxlamudi","brlamudi")],
                                              proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
    
    #Train Data DM
    train.data.DM <- gw.dist(dp.locat = coordinates(train.data.spdf))
    
    #Train Data BW (given)
    train.data.bw <-  bw.gwr(hrglamudi ~ ltxlamudi + lbxlamudi,
                             data = train.data.spdf,
                             approach = "CV",
                             kernel = "bisquare",
                             adaptive = T,
                             dMat = train.data.DM)
    
    ####################################################################################################################
    
    #Test data compile
    test.data <- reactive({
        X <- as.numeric(1)
        hrglamudi <- as.numeric(0)
        jdllamudi <- as.character("Prediction")
        ltxlamudi <- as.numeric(as.character(input$tanah))
        lbxlamudi <- as.numeric(as.character(input$bangunan))
        brlamudi <- as.numeric(2)
        latlamudi <- as.numeric(as.character(input$lat))
        longlamudi <- as.numeric(as.character(input$long))
        
        test.data <- cbind(X,hrglamudi,jdllamudi,ltxlamudi,lbxlamudi,brlamudi,latlamudi,longlamudi)
        test.data <- as.data.frame(test.data)
        
        test.data$X <- as.numeric(test.data$X)
        test.data$hrglamudi <- as.numeric(test.data$hrglamudi)
        test.data$jdllamudi <- as.character(test.data$jdllamudi)
        test.data$ltxlamudi <- as.numeric(as.character(test.data$ltxlamudi))
        test.data$lbxlamudi <- as.numeric(as.character(test.data$lbxlamudi))
        test.data$brlamudi <- as.numeric(test.data$brlamudi)
        test.data$latlamudi <- as.numeric(as.character(test.data$latlamudi))
        test.data$longlamudi <- as.numeric(as.character(test.data$longlamudi))
        
        test.data
    })
    
    #Test data SPDF
    test.data.spdf <- reactive({SpatialPointsDataFrame(coords = test.data()[,c("longlamudi","latlamudi")],
                                                       data = test.data()[,c("hrglamudi","jdllamudi","ltxlamudi","lbxlamudi","brlamudi")],
                                                       proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
        
        test.data.spdf
    })
    
    #Test data DM
    test.data.DM <- reactive({gw.dist(dp.locat = coordinates(train.data.spdf),
                                      rp.locat = coordinates(test.data.spdf()))
        test.data.DM
    })
    
    #################################################################################################################### 
    
    
    #GWModel
    model <- eventReactive(input$Run_model,{gwr.predict(hrglamudi ~ ltxlamudi + lbxlamudi + brlamudi,
                                   data = train.data.spdf,
                                   predictdata = test.data.spdf(),
                                   bw = train.data.bw,
                                   kernel = "bisquare",
                                   adaptive = T,
                                   dMat1 = test.data.DM(),
                                   dMat2 = train.data.DM)
        model
    })
    
    output$summary <- renderTable(test.data())
    output$summary2 <- renderText(model()$SDF$prediction)
}

shinyApp(ui=ui, server=server)

当我运行该应用程序时,它一直返回“找不到对象'pd.locat'”,我已经阅读了gwr.predict HereHere后面的代码以获取我使用的数据

我希望成为输出的是模型的预测数字。我通过阅读本文和解决方案来感谢您的时间。谢谢

4

1 回答 1

0

问题在于您的反应元素。在那个使test.data您在调用的反应中创建一个对象test.data然后对其进行修改的方法中,因此您必须在最后返回该对象,这是通过test.data作为函数的最后一个元素调用来完成的。在创建test.data.spdftest.data.DMmodel. 在这些函数中的每一个中,您都在调用一个产生输出的函数,并且您让函数 generate 直接返回输出。您没有将其保存到需要返回的对象中。每个响应式的最后一行是对不存在的对象的调用。如果您取出每个返回对象的反应式中的行(即,上面提到的三个中的最后一行),那么应用程序应该可以工作。

library(shiny)
library(GWmodel)
library(sp)

ui<-shinyUI(fluidPage(
  titlePanel("Model"),
  
  
  sidebarLayout(
    sidebarPanel(
      actionButton("browser", "browser"),
#      tags$script("$('#browser').hide();"),
      
      textInput(inputId = "tanah",
                label = "Luas Tanah",
                value = 72),
      textInput(inputId = "bangunan",
                label = "Luas Bangunan",
                value = 36),
      textInput(inputId = "lat",
                label = "Latitude",
                value = -6.401150),
      textInput(inputId = "long",
                label = "Longitude",
                value = 106.770416),
      actionButton("Run_model", "Run model")
    ),
    
    mainPanel(
      
      tabsetPanel(
        tabPanel("model summary", tableOutput('summary'), verbatimTextOutput('summary2'))
      )
    )
  )))


server<- function(input,output,session){
  observeEvent(input$browser,{
      browser()
  })
  
  #Train data
  train.data <- read.csv("depoklagi.csv")
  
  
  
  #Train Data SPDF
  train.data.spdf <- SpatialPointsDataFrame(coords = train.data[,c("longlamudi","latlamudi")],
                                            data = train.data[,c("hrglamudi","jdllamudi","ltxlamudi","lbxlamudi","brlamudi")],
                                            proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
  
  #Train Data DM
  train.data.DM <- gw.dist(dp.locat = coordinates(train.data.spdf))
  
  #Train Data BW (given)
  train.data.bw <-  bw.gwr(hrglamudi ~ ltxlamudi + lbxlamudi,
                           data = train.data.spdf,
                           approach = "CV",
                           kernel = "bisquare",
                           adaptive = T,
                           dMat = train.data.DM)
  
  ####################################################################################################################
  
  #Test data compile
  test.data <- reactive({
    X <- as.numeric(1)
    hrglamudi <- as.numeric(0)
    jdllamudi <- as.character("Prediction")
    ltxlamudi <- as.numeric(as.character(input$tanah))
    lbxlamudi <- as.numeric(as.character(input$bangunan))
    brlamudi <- as.numeric(2)
    latlamudi <- as.numeric(as.character(input$lat))
    longlamudi <- as.numeric(as.character(input$long))
    
    test.data <- cbind(X,hrglamudi,jdllamudi,ltxlamudi,lbxlamudi,brlamudi,latlamudi,longlamudi)
    test.data <- as.data.frame(test.data)
    
    test.data$X <- as.numeric(test.data$X)
    test.data$hrglamudi <- as.numeric(test.data$hrglamudi)
    test.data$jdllamudi <- as.character(test.data$jdllamudi)
    test.data$ltxlamudi <- as.numeric(as.character(test.data$ltxlamudi))
    test.data$lbxlamudi <- as.numeric(as.character(test.data$lbxlamudi))
    test.data$brlamudi <- as.numeric(test.data$brlamudi)
    test.data$latlamudi <- as.numeric(as.character(test.data$latlamudi))
    test.data$longlamudi <- as.numeric(as.character(test.data$longlamudi))
    
    test.data
  })
  
  #Test data SPDF
  test.data.spdf <- reactive({SpatialPointsDataFrame(coords = test.data()[,c("longlamudi","latlamudi")],
                                                     data = test.data()[,c("hrglamudi","jdllamudi","ltxlamudi","lbxlamudi","brlamudi")],
                                                     proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
    
  })
  
  #Test data DM
  test.data.DM <- reactive({gw.dist(dp.locat = coordinates(train.data.spdf),
                                    rp.locat = coordinates(test.data.spdf()))
  })
  
  #################################################################################################################### 
  
  
  #GWModel
  model <- eventReactive(input$Run_model,{gwr.predict(hrglamudi ~ ltxlamudi + lbxlamudi + brlamudi,
                                                      data = train.data.spdf,
                                                      predictdata = test.data.spdf(),
                                                      bw = train.data.bw,
                                                      kernel = "bisquare",
                                                      adaptive = T,
                                                      dMat1 = test.data.DM(),
                                                      dMat2 = train.data.DM)
  })
  
  output$summary <- renderTable(test.data())
  output$summary2 <- renderText(model()$SDF$prediction)
}

shinyApp(ui=ui, server=server)
于 2020-09-05T13:42:10.290 回答