【发布时间】:2020-04-03 17:47:32
【问题描述】:
最近我正在尝试创建一个交互式闪亮应用程序。在这个应用程序中,我想要一个线性模型来预测用户的给定值。此外,用户可以选择预测变量和解释变量。在此之后,模型简单地返回应用程序显示的预测值。但是,我无法弄清楚如何让应用程序使用现有数据框并返回预测值。运行应用程序时出现以下错误:
Warning in storage.mode(v) <- "double" : NAs introduced by coercion
Warning: Error in contrasts<-: contrasts can be applied only to factors with 2 or more levels
[No stack trace available]
脚本本身类似于 Sean Kross (https://seankross.com/developing-data-products/shiny.html#reactivity) 的代码。我使用了下面提供的脚本导致错误:
librar(shiny)
#Create random data frame with three vectors
df <- data.frame(V1=1:100, V2=100:1*runif(100, min=0, max=1), V3=1:100*runif(100, min=0, max=1))
ui <- fluidPage(
headerPanel("Regression"),
sidebarPanel(
p("Select explanatory variable"),
selectInput(inputId = "ExpVar", label = "Explanatory variable", choices = colnames(df)),
p("Select predictor variable"),
selectInput(inputId = "PreVar", label = "Predictor variable", choices = colnames(df)),
numericInput(inputId = "Pred", label = "Predict chosen value", value = 10),
h3("Prediction"),
textOutput("prediction")))
server <- function(input, output){
modpred <- reactive({
Pre <- input$Pred
Pname <- input$PreVar
mod <- lm(input$PreVar~input$ExpVar, data=df)
predict(mod, newdata = data.frame(Pname = Pre))})
output$prediction <- renderText({modpred()})
}
shinyApp(ui, server)
提前感谢您的宝贵时间
【问题讨论】:
标签: r shiny model linear-regression