R 正在尝试创建一个闪亮的应用程序。获取指定公式输入的错误
我正在尝试创建一个闪亮的应用程序,在这里我可以输入一个回归公式并得到结果的输出。我试过这个:R 正在尝试创建一个闪亮的应用程序。获取指定公式输入的错误,r,shiny,R,Shiny,我正在尝试创建一个闪亮的应用程序,在这里我可以输入一个回归公式并得到结果的输出。我试过这个: library(shiny) ui <- fluidPage( titlePanel("Text Input"), sidebarLayout( sidebarPanel( textInput(inputId = "text1", label = &quo
library(shiny)
ui <- fluidPage(
titlePanel("Text Input"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "text1",
label = "Regression"),
value = "Enter formula"),
mainPanel(
verbatimTextOutput("text1"),
verbatimTextOutput("regout")
)
)
)
server <- function(input, output, session) {
output$text1 <- renderPrint({
input$text1
})
formula <- reactive(input$text1)
output$regout <- renderPrint({
summary(formula)
})
}
shinyApp(ui = ui, server = server)
但我得到了一个错误:
Error: object of type 'closure' is not subsettable
“closure”类型的对象是什么意思?如何使其工作?任何渲染函数都已经是反应上下文,因此您不需要将公式变量声明为其自己的反应上下文。这个错误消息非常普遍,但我假设它是由于在一个反应性上下文中有一个反应性上下文的这种奇怪性质而出现的,尽管我相信它并没有被明确禁止。无论如何,下面是服务器函数中的代码应该是什么样子
server <- function(input, output, session) {
output$text1 <- renderPrint({
input$text1
})
output$regout <- renderText({
summary(input$text1)
})
}
定义自己的公式可能会很棘手,因为您通常将它们作为字符串文本传递到
lm()
函数中。但是,是的,这就是我的全部,祝你好运 您错过了括号:summary(formula())
。但是如果您键入lm(mpg~disp+wt,data=mtcars)
您将得到字符串“lm(mpg~disp+wt,data=mtcars)”
。这将不会运行代码lm(mpg~disp+wt,data=mtcars)
您可以输入mpg~disp+wt
并执行以下操作:
lmformula <- reactive({
tryCatch({
as.formula(input$text1)
}, error = function(e) NULL)
})
lmreg <- reactive({
if(!is.null(lmformula())){
tryCatch({
lm(lmformula(), data = mtcars)
}, error = function(e) NULL)
}
})
lmsummary <- reactive({
if(!is.null(lmreg())){
summary(lmreg())
}
})
output$regout <- renderPrint({
lmsummary()
})
lm公式
1 character character
lmformula <- reactive({
tryCatch({
as.formula(input$text1)
}, error = function(e) NULL)
})
lmreg <- reactive({
if(!is.null(lmformula())){
tryCatch({
lm(lmformula(), data = mtcars)
}, error = function(e) NULL)
}
})
lmsummary <- reactive({
if(!is.null(lmreg())){
summary(lmreg())
}
})
output$regout <- renderPrint({
lmsummary()
})