Perform multiple linear regression with variables based on shiny widget selection

拥有回忆 提交于 2021-02-11 17:57:14

问题


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)
        })
        
        
    }
)

回答1:


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())
        })
        
        
        
        
        
    }
)


来源:https://stackoverflow.com/questions/64103095/perform-multiple-linear-regression-with-variables-based-on-shiny-widget-selectio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!