1

I have a reactive dataframe and I want the user to select the dependent and multiple independent variables from that reactive dataframe and return the regression outputs. Does anyone have recommendations on the best way to do multiple regression on a reactive dataframe in Shiny?

I see this thread: Using R Shiny for Multiple Linear Regression (SelectInput --> multiple=TRUE)

but I have commented showing the code doesn't work.

I also see this question: Perform multiple linear regression with variables based on shiny widget selection

But that is just a simple 1 to 1 regression.

Spence_p
  • 67
  • 1
  • 7
  • Please add a reprex – Bruno Jan 19 '21 at 17:41
  • Don't the two links work? I have the exact same problem as them and am trying the exact same code. I don't want to unnecessarily duplicate and create confusion but maybe it is better I do my own reprex? I just don't even really have a working solution up yet – Spence_p Jan 19 '21 at 20:00
  • They do but it is in good faith to make great reprexes, you are lucky I already solved the last one, if it was someone with less patience they would just skip this question, you could also improve on the question by removing any unnecessary code – Bruno Jan 19 '21 at 20:09

2 Answers2

2

Ok so I took a look at the first answer you said didn't work and I've slightly modified it to allow you to also select the dependent variable. You get an error when you include the dependent variable in the independent variables but I'm sure you could figure out a way to make sure that the dependent variable isn't included in the independent variables as a 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)
dodo1672
  • 51
  • 4
  • I keep getting the same error! `Warning: Error in : The recipe must be prepped before the formula can be computed. 148: ` – Spence_p Jan 19 '21 at 19:55
  • Make sure you have all the required packages installed/updated. Also make sure this is done in a Shiny Web App file. This runs perfect for me in RStudio and I currently can't reproduce the error you're receiving. Try running what I posted just by itself and see if you keep getting the same error. – dodo1672 Jan 19 '21 at 20:10
  • I think this may be related to package versions – Bruno Jan 19 '21 at 20:18
2

For some reason it now requires prep(), just add it at the end of the pipe, I also improved the selection as @dodo1672 had said.

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)
Bruno
  • 4,109
  • 1
  • 9
  • 27
  • 1
    This is exactly what I needed! Thank you both! I'll be honest I'm not entirely sure what is happening in recipe_formula but i think I can learn more about the package and understand it from here! Thank you both again! Guess I should add that adding prep() to the pipe made the code work. Again, I'm not entirely sure why but now it works! – Spence_p Jan 19 '21 at 20:45