问题
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