【发布时间】:2019-07-11 04:58:39
【问题描述】:
我正在尝试构建一个闪亮的应用程序,允许用户基于能够从下拉菜单中选择输入特征(数据集列)来预测(lm)结果。
问题是我找不到将输入变量设置到预测模型中的方法,该模型将根据下拉菜单在输入变量之间切换。
Image 1: where SNR is used from dropdown
Image 2: where different variable (RSRP) is used
我遇到了问题,首先能够根据下拉窗口中的选择来更改绘图中的滑块比例(左侧)和 x 轴,但是我通过使用 conditionalPanel 并设置条件克服了这个问题到所需的 input.indepvar
虽然这会起作用:
预测(拟合,newdata = data.frame(SNR = varInput))
这不会
selectVar
预测(拟合,newdata = data.frame(selectVar = varInput))
即使打印出 selectVar 结果会给出 SNR(我将其呈现为最后的测试输出)。
我基本上想以某种方式将 SNR 值替换为从下拉菜单中选择的任何值 (input$indepvar)。
使用的数据来自该数据集中的行人文件夹:http://www.cs.ucc.ie/~dr11/4G_Dataset/LTE_Dataset.zip
## Only run this example in interactive R sessions
if (interactive()) {
shinyApp(
ui <- fluidPage(
titlePanel("Regression Model (Dataset: pedestrian)"),
sidebarLayout(
sidebarPanel(
selectInput("outcome", label = h3("Outcome"),
choices = list("DL_bitrate" = "DL_bitrate",
"UL_bitrate" = "UL_bitrate",
"RSSI" = "RSSI",
"SNR" = "SNR",
"RSRP" = "RSRP",
"CQI" = "CQI"), selected = 1),
selectInput("indepvar", label = h3("Explanatory variable"),
choices = list("SNR" = "SNR",
"RSRP" = "RSRP",
"RSRQ" = "RSRQ",
"CQI" = "CQI",
"ServingCell_Distance" = "ServingCell_Distance"), selected = 1),
conditionalPanel(
condition = "input.indepvar == 'SNR'",
sliderInput("sliderIndVar",
label = "SNR Range:",
min = -10, max = 30, 5)
),
conditionalPanel(
condition = "input.indepvar == 'RSRP'",
sliderInput("sliderIndVar", # Plan on updating this but was working with SNR to start with
label = "RSRP Range:",
min = -115, max = -65, 100)
),
conditionalPanel(
condition = "input.indepvar == 'RSRQ'",
sliderInput("sliderIndVar", # Plan on updating this but was working with SNR to start with
label = "RSRQ Range:",
min = -18, max =-8, -12)
),
conditionalPanel(
condition = "input.indepvar == 'CQI'",
sliderInput("sliderIndVar", # Plan on updating this but was working with SNR to start with
label = "CQI Range:",
min = 0, max = 15, 8)
),
conditionalPanel(
condition = "input.indepvar == 'ServingCell_Distance'",
sliderInput("sliderIndVar", # Plan on updating this but was working with SNR to start with
label = "Serving Cell Distance:",
min = 150, max = 75000, 1000)
)
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Scatterplot", plotOutput("scatterplot"),
h3("Slider Value:"),
textOutput("text"), # This show the select input from the slider for test purpose
h3("Predicted Value:"), # This show the resultant prediction for test purpose
textOutput("pred1"), # Works ok for SNR - as I've hard coded SNR in server model1pred
h3("Select variable:"), # This show the variable selected from the drop down
textOutput("var1")), # I want to use this variable as input to model1pred
tabPanel("Distribution", # Plots of distributions
fluidRow(
column(6, plotOutput("distribution1")),
column(6, plotOutput("distribution2")))
),
tabPanel("Model Summary", verbatimTextOutput("summary")), # Regression output
tabPanel("Data", DT::dataTableOutput('tbl')) # Data as datatable
)
)
)),
server <- function(input, output) {
# Regression output
output$summary <- renderPrint({
fit <- lm(pedestrian[,input$outcome] ~ pedestrian[,input$indepvar])
names(fit$coefficients) <- c("Intercept", input$var2)
summary(fit)
})
# Prediction value
model1pred <- reactive({
varInput <- input$sliderIndVar
# selectVar<-input$indepvar # Was trying to use something like thi to replace SNR in prediction!!
predict(fit, newdata = data.frame(SNR = varInput)) # I want to replace SNR with which variable is selected from the
# selectInput("indepvar" drop menu.
# predict(fit, newdata = data.frame(selectVar = varInput)) # This doesn't work for example
})
# Data output
output$tbl = DT::renderDataTable({
DT::datatable(pedestrian, options = list(lengthChange = FALSE))
})
# Scatterplot output
output$scatterplot <- renderPlot({
varInput <- input$sliderIndVar # UPDATE HERE to be flexible with drop down menu!!
plot(pedestrian[,input$indepvar], pedestrian[,input$outcome], main="Scatterplot",
xlab=input$indepvar, ylab=input$outcome, pch=19)
abline(lm(pedestrian[,input$outcome] ~ pedestrian[,input$indepvar]), col="red")
lines(lowess(pedestrian[,input$indepvar],pedestrian[,input$outcome]), col="blue")
points(varInput, model1pred(), col = "red", pch = 16, cex = 2)
}, height=400)
# Histogram output var 1
output$distribution1 <- renderPlot({
hist(pedestrian[,input$outcome], main="", xlab=input$outcome)
}, height=300, width=300)
# Histogram output var 2
output$distribution2 <- renderPlot({
hist(pedestrian[,input$indepvar], main="", xlab=input$indepvar)
}, height=300, width=300)
# Slider input & Prediction
output$text <- renderText({input$sliderIndVar})
# UPDATE HERE!!
output$pred1 <- renderText(model1pred()) # I want to also update the prediction based on the selection from the drop down menu
output$var1 <- renderText(input$indepvar) # I can get this to update based on the
}
)
}
当我选择 SNR 以外的任何内容时,预测值不会更新。
因此,我在图表上的预测点也没有更新。 其他一切都有效(轴刻度、基于所选输入变量的滑块刻度)。 任何想法将不胜感激。
【问题讨论】:
标签: r drop-down-menu shiny user-input prediction