rshinygwmodel

Shiny apps with GWmodel object not found


i'm new to R and i want to build my own Shiny apps based on an R package called GWModel. Here is my code

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)

When i run the app, it keeps returning "object 'pd.locat' not found", i already read the code behind the gwr.predict Here and Here for the data i used

What i want to expect to become the output is the predicted numbers from the model. I appretiate your time by reading this and for the solution. Thank you


Solution

  • The problem is in your reactive elements. In the one that makes test.data you are making an object within the reactive called test.data and then modifying it, so you have to return the object at the end, which you did by calling test.data as the last element of the function. The same is not true in the reactives that create test.data.spdf, test.data.DM and model. In each of these functions, you're calling a function that produces output and you're having the function produce return the output directly. You're not saving it to an object that then needs to be returned. The last line of each of those reactives is a call to an object that doesn't exist. If you take out the lines in each of those reactives that returns the object (i.e., the last lines of each of the three mentioned just above), then the app should work.

    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)