【问题标题】:Perform multiple linear regression with variables based on shiny widget selection使用基于闪亮小部件选择的变量执行多元线性回归
【发布时间】:2020-09-28 22:42:44
【问题描述】:

我想在一个闪亮的应用程序中执行多重线性回归,但每次我想根据 2 个闪亮的小部件更改因变量和自变量。这能实现吗?

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

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    当然,你可以这样访问它:

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

    【讨论】:

    • 恐怕在你的代码中有些东西不起作用。第二个输入“val2”可能需要有多个值,然后它不起作用
    • 当我在原始数据框中使用 paste() 时碰巧遇到此错误,可能是因为列名中的单词之间有空格警告:解析错误::1:17: 意外符号1:粉丝~^的数量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2021-03-31
    • 1970-01-01
    • 2016-11-14
    • 2021-01-09
    • 2017-09-19
    • 1970-01-01
    相关资源
    最近更新 更多