【发布时间】:2021-11-26 12:16:07
【问题描述】:
我正在尝试使用 R Shiny Modules 进行线性回归。思路如下:
- 我有一个模块,它将 data.table 作为函数的输入。
- 然后模块 UI 会询问线性回归的 X 和 Y 变量
- 单击按钮后,应执行线性回归并打印摘要输出。
我尝试了以下代码,但抛出了一个我无法修复的错误。
请帮忙。
代码
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。您可以使用updateSelectInput从Linear.Regression.Server内的反应上下文中填充 UI 中selectInputs 的下拉列表。
标签: r shiny module shinydashboard shiny-reactivity