Reputation: 77
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.
Upvotes: 1
Views: 425
Reputation: 4150
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)
Upvotes: 2
Reputation: 51
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)
Upvotes: 2