【问题标题】:Multiple Linear regression using modules in R Shiny使用 R Shiny 中的模块进行多元线性回归
【发布时间】:2021-11-26 12:16:07
【问题描述】:

我正在尝试使用 R Shiny Modules 进行线性回归。思路如下:

  1. 我有一个模块,它将 data.table 作为函数的输入。
  2. 然后模块 UI 会询问线性回归的 X 和 Y 变量
  3. 单击按钮后,应执行线性回归并打印摘要输出。

我尝试了以下代码,但抛出了一个我无法修复的错误。

请帮忙。

代码


Linear.Regression.UI <- function(id, data.tibble){
  ns <- NS(id)
  tagList(
    actionButton(ns("ClickforRegression"), label = "Click Here to Do Regression"),
    
    selectInput(inputId = ns("Regression.Y.Input"),
                label = "Select Regression Dependent Variable",
                choices = names(data.tibble),
                ),
    selectInput(inputId = ns("Regression.X.Input"),
                label = "Select Regression Independent Variables",
                choices= names(data.tibble),
                multiple=TRUE),
    
    verbatimTextOutput("Linear.Model.Output.Summary")
  )#end of tagList

}#end of Linear.Regression.UI


Linear.Regression.Server <- function(id){
  moduleServer(id, function(input, output, session){
    ns <- session$ns
    
    observeEvent(eventExpr = input$ClickforRegression,
                 linear.model <-  lm(reformulate(input$Regression.X.Input, input$Regression.Y.Input), data = data.tibble)
                 )#end of observeEvent
    
    
    output$Linear.Model.Output.Summary <- renderPrint(summary(linear.model()))
    
    
  })#end of moduleServer
  
}


Regression.App <- function(data.tibble){
  ui <- fluidPage(
    Linear.Regression.UI("Data", data.tibble = iris)
  )
  
  server <- function(input, output, session)
  {
    Linear.Regression.Server("Data")
  }
  
  shinyApp(ui, server)
}


Regression.App()

错误

Listening on http://127.0.0.1:3883
Warning: Error in is.data.frame: object 'data.tibble' not found
  [No stack trace available]

【问题讨论】:

  • 错误是什么?
  • 道歉。我用错误编辑了我的原始帖子
  • 您需要将data.tibble 传递给Linear.Regression.Server 函数,而不是UI。您可以使用 updateSelectInputLinear.Regression.Server 内的反应上下文中填充 UI 中 selectInputs 的下拉列表。

标签: r shiny module shinydashboard shiny-reactivity


【解决方案1】:

代码还需要ns 修复,但主要修复是将数据传递到服务器:

Linear.Regression.UI <- function(id, data.tibble){
  ns <- NS(id)
  tagList(
    actionButton(ns("ClickforRegression"), label = "Click Here to Do Regression"),
    
    selectInput(inputId = ns("Regression.Y.Input"),
                label = "Select Regression Dependent Variable",
                choices = names(data.tibble),
    ),
    selectInput(inputId = ns("Regression.X.Input"),
                label = "Select Regression Independent Variables",
                choices= names(data.tibble),
                multiple=TRUE),
    
    verbatimTextOutput(ns("Linear.Model.Output.Summary"))
  )#end of tagList
  
}#end of Linear.Regression.UI


Linear.Regression.Server <- function(id, data.tibble ){
  moduleServer(id, function(input, output, session){
    ns <- session$ns
    
    linear.model<- reactiveVal()
    observeEvent(eventExpr = input$ClickforRegression,{ message("sdf");
      linear.model(lm(reformulate(input$Regression.X.Input, input$Regression.Y.Input), data = data.tibble))}
    )#end of observeEvent
    
    
    output$Linear.Model.Output.Summary <- renderPrint(summary(linear.model()))
    
    
  })#end of moduleServer
  
}


Regression.App <- function(data.tibble){
  ui <- fluidPage(
    Linear.Regression.UI("Data", data.tibble = iris)
  )
  
  server <- function(input, output, session)
  {
    Linear.Regression.Server("Data", data.tibble = iris)
  }
  
  shinyApp(ui, server)
}


Regression.App()

【讨论】:

  • 非常感谢@Bart。我有两个后续问题。 1) linear.model 已经在 observeEvent Handler 中。然而,你定义了 linear.model
  • 我认为用observeEvent 赋值不会产生反应值。这就是为什么我认为这种方式可行。我确信存在其他选项,重要的是 linear.model 成为您以后可以使用的反应式。
猜你喜欢
  • 2020-10-03
  • 1970-01-01
  • 2018-08-11
  • 2013-07-14
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
相关资源
最近更新 更多