使用 R Shiny 进行多重线性回归 (SelectInput --> multiple=TRUE)
Posted
技术标签:
【中文标题】使用 R Shiny 进行多重线性回归 (SelectInput --> multiple=TRUE)【英文标题】:Using R Shiny for Multiple Linear Regression (SelectInput --> multiple=TRUE) 【发布时间】:2020-10-03 15:55:24 【问题描述】:我在让我的 R Shiny 代码生成动态仪表板时遇到了一些麻烦,用户可以在其中选择线性回归模型中的 1 个或多个自变量并打印结果。我已经能够成功地遵循用户只输入一个自变量但有多个自变量的示例,我没有找到同样的运气。我不确定我做错了什么,但我收到一条错误消息,内容为“模型公式中的项无效”。
以下是我目前使用的代码:
library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)
#data(mtcars)
AttributeChoices=c("cyl","disp","hp","drat","wt","qsec","vs")
# Define UI for application
ui = fluidPage(
navbarPage("R Shiny Dashboard",
tabPanel("Welcome",
tabName = "welcome",
icon=icon("door-open"),
fluidPage(theme=shinytheme("cerulean"),
h1("Welcome to my Shiny Dashboard!"),
br(),
p(strong(tags$u("What is this dashboard all about?"))),
p("I'm going to do stuff."),
br(),
p(strong(tags$u("Here's another question."))),
p("Here's my answer."),
br(),
p(strong(tags$u("How can I use this dashboard?"))),
p("You can click on any of the tabs above to see a different analysis of the data.")
)),
tabPanel("Regression",
tabname="regression",
icon=icon("calculator"),
selectInput(inputId = "indep", label = "Independent Variables",
multiple = TRUE, choices = as.list(AttributeChoices), selected = AttributeChoices[1]),
verbatimTextOutput(outputId = "RegOut")
)
))
# Define server logic
server <- function(input, output)
#-------------------REGRESSION-------------------#
lm_reg <- reactive(
lm(as.formula(paste(mtcars$mpg," ~ ",paste(input$indep,collapse="+"))),data=CFD)
)
output$RegOut = renderPrint(summary(lm_reg()))
# Run the application
shinyApp(ui = ui, server = server)
阅读 *** 上的类似帖子似乎表明问题可能在于列名包含空格,但在此示例中并非如此。我不知道如何解决这个问题。谁能帮我指出正确的方向?谢谢!
【问题讨论】:
这段代码是否在非反应性设置上工作,我发现很难在闪亮本身上进行调试,我通常有一个镜像程序来测试这样的事情 【参考方案1】:给你,我喜欢使用配方包来解决这样的问题,而不是依赖非常困难的字符串操作,诀窍是使用 !!!运算符,您甚至可以花哨并让用户传递一些选择助手
library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)
library(recipes)
#data(mtcars)
AttributeChoices=c("cyl","disp","hp","drat","wt","qsec","vs")
# Define UI for application
ui = fluidPage(
navbarPage("R Shiny Dashboard",
tabPanel("Welcome",
tabName = "welcome",
icon=icon("door-open"),
fluidPage(theme=shinytheme("cerulean"),
h1("Welcome to my Shiny Dashboard!"),
br(),
p(strong(tags$u("What is this dashboard all about?"))),
p("I'm going to do stuff."),
br(),
p(strong(tags$u("Here's another question."))),
p("Here's my answer."),
br(),
p(strong(tags$u("How can I use this dashboard?"))),
p("You can click on any of the tabs above to see a different analysis of the data.")
)),
tabPanel("Regression",
tabname="regression",
icon=icon("calculator"),
selectInput(inputId = "indep", label = "Independent Variables",
multiple = TRUE, choices = as.list(AttributeChoices), selected = AttributeChoices[1]),
verbatimTextOutput(outputId = "RegOut")
)
))
# Define server logic
server <- function(input, output)
#-------------------REGRESSION-------------------#
recipe_formula <- reactive(mtcars %>%
recipe() %>%
update_role(mpg,new_role = "outcome") %>%
update_role(!!!input$indep,new_role = "predictor") %>%
formula())
lm_reg <- reactive(
lm(recipe_formula(),data = mtcars)
)
output$RegOut = renderPrint(summary(lm_reg()))
# Run the application
shinyApp(ui = ui, server = server)
【讨论】:
效果很好!太感谢了!你是最棒的! 我找不到解决这个问题的好方法,你的代码给了我错误The recipe must be prepped before the formula can be computed.
我错过了什么?
@Spence_p 尝试用错误的表示形式提出一个单独的问题。以上是关于使用 R Shiny 进行多重线性回归 (SelectInput --> multiple=TRUE)的主要内容,如果未能解决你的问题,请参考以下文章