【问题标题】:Regression in a ShinyAppShinyApp 中的回归
【发布时间】:2019-05-21 02:47:50
【问题描述】:

我正在尝试为 shinyApp 编写代码,在其中我选择一个响应变量(只有一个选择框),然后在复选框中选择我想要的所有协变量。此外,我想指定我的响应变量(高斯、泊松、二项式等)的分布族。我不知道在主面板和服务器中写什么。有人可以帮我吗?

library(shiny) 
library(plotly)
data(iris)

ui<-fluidPage(
  titlePanel("Regression"),
  sidebarPanel(
    selectInput("var",label="Choose the response variable:",choice=names(iris), selectize=FALSE),
    tags$hr(),
    checkboxGroupInput(inputId ="ind_var",label = "Select at least two independent variables:", choices = names(iris)),

    mainPanel(

    )
  )
)

server<-function(input,output){


}
shinyApp(ui, server)

【问题讨论】:

  • 现在很难说。你想在主面板中显示什么?你想让服务器做什么?服务器是您进行计算的地方——它们是什么?

标签: r shiny


【解决方案1】:

也许这个小例子可以帮到你。

如果要动态创建公式,则必须将其包装成reformulateas.formula

这个 shinyApp 示例打印出线性回归的摘要并根据该模型绘制预测:

library(shiny) 
library(plotly)
data(iris)

ui<-fluidPage(
  titlePanel("Regression"),
  sidebarPanel(
    selectInput("var",label="Choose the response variable:",choice=names(iris), selectize=FALSE),
    tags$hr(),
    checkboxGroupInput(inputId ="ind_var", label = "Select at least two independent variables:", choices = names(iris), selected = "Species"),

    mainPanel(
      plotOutput("plot"),
      verbatimTextOutput("summary")

    )
  )
)

server<-function(input,output){

  lmR <- reactive({
    req(input$var)
    req(input$ind_var)

    ## Option 1 ---------------------
    # formula = as.formula(paste(input$var, " ~ ", paste(input$ind_var, collapse= "+")))
    # stats::lm(formula=formula, data=iris)

    # browser()
    ## Option 2 ---------------------               
    stats::lm(formula=reformulate(input$ind_var, input$var), data=iris)
  })

  output$summary <- renderPrint(width="400", {
    lmR()
  })

  output$plot <- renderPlot({
    plot(predict(lmR()))    
  })

}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 2020-07-04
    • 2018-11-19
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2022-07-31
    • 2016-09-15
    相关资源
    最近更新 更多