firmo23
firmo23

Reputation: 8444

Perform multiple linear regression with variables based on shiny widget selection

I would like to perform multiple linear regression in a shiny app but every time I would like to change dependent and independent variables based on 2 shiny widgets. Could this be achieved?

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
library(dplyr)
shinyApp(
    ui = dashboardPagePlus(
        header = dashboardHeaderPlus(title = "Social Media Metrics", titleWidth = 320
                                     
        ),
        sidebar = dashboardSidebar(width = 320,
                                   
                                   uiOutput("value"),
                                   uiOutput("value2")
                                   
        ),
        body = dashboardBody(
            verbatimTextOutput("plot") 
        )
        
        
    ),
    server = function(input, output) {
        
        
        
        output$value<-renderUI({
            
                pickerInput(
                    inputId = "val"
                    ,
                    label = "DEPENDENT" 
                    ,
                    choices = colnames(iris)[-5] #all rows of selected column
                    ,
                    multiple = F, options = list(`actions-box` = TRUE)
                    
                )
            
            
        })
        output$value2<-renderUI({
            
            pickerInput(
                inputId = "val2"
                ,
                label = "INDEPENDENT" 
                ,
                choices = colnames(iris)[-5] #all rows of selected column
                ,
                multiple = T, options = list(`actions-box` = TRUE)
                
            )
            
            
        })
        output$plot<-renderPrint({
            model <- lm(input$val ~ input$val2, data = iris)
            summary(model)
        })
        
        
    }
)

Upvotes: 0

Views: 341

Answers (1)

Pork Chop
Pork Chop

Reputation: 29407

Sure, you can access it like so:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
library(dplyr)
shinyApp(
    ui = dashboardPagePlus(
        header = dashboardHeaderPlus(title = "Social Media Metrics", titleWidth = 320
                                     
        ),
        sidebar = dashboardSidebar(width = 320,
                                   
                                   uiOutput("value"),
                                   uiOutput("value2")
                                   
        ),
        body = dashboardBody(
            verbatimTextOutput("plot") 
        )
        
        
    ),
    server = function(input, output) {
        
        
        
        output$value<-renderUI({
            
            pickerInput(
                inputId = "val"
                ,
                label = "DEPENDENT" 
                ,
                choices = colnames(iris)[-5] #all rows of selected column
                ,
                multiple = F, options = list(`actions-box` = TRUE)
                
            )
            
            
        })
        output$value2<-renderUI({
            
            pickerInput(
                inputId = "val2"
                ,
                label = "INDEPENDENT" 
                ,
                choices = colnames(iris)[-5] #all rows of selected column
                ,
                multiple =T, options = list(`actions-box` = TRUE)
                
            )
        })
        
        
        
        model <- eventReactive(c(input$val,input$val2),{
            req(c(input$val,input$val2))
            lm(as.formula(paste(input$val," ~ ",paste(input$val2,collapse="+"))),data=iris)
        })
        
        output$plot <- renderPrint({
            summary(model())
        })
        
        
        
        
        
    }
)

Upvotes: 2

Related Questions