使用 R Shiny 中的模塊進行多元線性回歸

RAC

我正在嘗試使用 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]
巴特

代碼還需要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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章