r - 使用 R Shiny 进行多元线性回归(SelectInput --> multiple=TRUE)

标签 r shiny regression selectinput

我在让 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)

在 StackOverflow 上阅读类似的帖子似乎表明问题可能在于列名称中包含空格,但本示例中的情况并非如此。我不知道如何解决这个问题。谁能帮我指出正确的方向?谢谢!

最佳答案

给你,我喜欢使用配方包来解决这样的问题,而不是依赖于非常困难的字符串操作,诀窍是使用!!!运算符,您甚至可以花哨并让用户传递一些选择帮助器

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)

关于r - 使用 R Shiny 进行多元线性回归(SelectInput --> multiple=TRUE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62367866/

相关文章:

r - 在 R 中添加二阶拟合散点图

r - ggplotly 无法在 Shiny 的应用程序中工作,绘图未显示

html - R Shiny 的表格,顶部和底部都有水平滚动条

r - 如何在 R 中进行 ma 和 loess 归一化?

python - 回归精度

Python statsmodels 返回值缺失

R - 按 dplyr 分组,仅当组中的所有成员重复时才删除重复项

r - 获取值运行的开始和结束索引

linux - 如何聚合文件或合并

javascript - Shiny 的 renderDataTable |如何限制显示的文字大小