在 r shiny 中对反应性数据框进行多元回归的最佳方法是什么?

What is the best method for doing multiple regression on a reactive dataframe in r shiny?

我有一个反应性数据框,我希望用户 select 来自该反应性数据框的因变量和多个自变量以及 return 回归输出。 有没有人对在 Shiny 中对反应性数据框进行多元回归的最佳方法提出建议?

我看到这个帖子:

但我评论说代码不起作用。

我也看到了这个问题:

但这只是简单的一对一回归。

好的,所以我查看了您所说的 不起作用,我对其进行了轻微修改,以允许您也 select 因变量。当您将因变量包含在自变量中时会出现错误,但我相信您可以找到一种方法来确保因变量不作为 selection 包含在自变量中。

library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)
library(recipes)
#data(mtcars)

AttributeChoices=c("mpg","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="dependent", label = "Dependent Variables",
                                    choices = as.list(AttributeChoices)),
                        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(!!!input$dependent,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)

出于某种原因,它现在需要 prep(),只需将其添加到管道的末尾,我还按照@dodo1672 所说改进了选择。

library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)
library(recipes)
#data(mtcars)

AttributeChoices=c("mpg","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="dependent", label = "Dependent Variables",
                                  choices = as.list(AttributeChoices)),
                      uiOutput("indep"),
                      verbatimTextOutput(outputId = "RegOut")
                      
             )
  ))
# Define server logic 
server <- function(input, output) {
  
  #-------------------REGRESSION-------------------#
  
  
  
  output$indep <- renderUI({
    selectInput(inputId = "indep", label = "Independent Variables", 
                multiple = TRUE, choices = as.list(AttributeChoices[AttributeChoices!= input$dependent]), selected = AttributeChoices[1])
  })
  
  
  
  recipe_formula <- reactive({
    req(input$indep)
    mtcars %>%
      recipe() %>%
      update_role(!!!input$dependent, new_role = "outcome") %>%
      update_role(!!!input$indep, new_role = "predictor") %>%
      prep() %>% 
      formula()
    })
  
  lm_reg <- reactive(
    lm(recipe_formula(),data = mtcars)
  )
  
  
 
  
  output$RegOut = renderPrint({
    summary(lm_reg())
    })
  
}

# Run the application 
shinyApp(ui = ui, server = server)