我是 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
Here和Here后面的代码以获取我使用的数据
我希望成为输出的是模型的预测数字。我通过阅读本文和解决方案来感谢您的时间。谢谢