0

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)
        })
        
        
    }
)
firmo23
  • 7,490
  • 2
  • 38
  • 114

1 Answers1

2

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())
        })
        
        
        
        
        
    }
)
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Im afraid something does not work in your code. The second input 'val2' may need to have more than one values and it does not work then – firmo23 Sep 28 '20 at 14:37
  • I happen to get this error when I use paste() in my original dataframe maybe because of the white space between words in column names Warning: Error in parse: :1:17: unexpected symbol 1: Fans ~ Number of ^ – firmo23 Sep 28 '20 at 15:10
  • Thats the Q. https://stackoverflow.com/questions/64105434/the-spaces-between-words-in-column-names-of-a-dataframe-cause-problem-in-a-shiny – firmo23 Sep 28 '20 at 15:47