【发布时间】:2018-01-22 19:17:57
【问题描述】:
为什么要在下面的代码中调用 df() 而不是 df?这是与反应函数一起使用的正确语法吗?
function(input, output, session){
df <- reactive({
head(cars, input$nrows)
})
output$plot <- renderPlot({
plot(df()) #Why call df() instead of df?
})
output$table <- renderTable({
df() #Why call df() instead of df?
})
}
【问题讨论】:
-
所以我不知道你需要多少解释。函数
reactive返回一个函数。所以foo <- reactive({...})正在根据你给它的表达式创建一个函数foo()。这就是为什么它必须作为函数调用。更深入的“为什么”答案可能会深入研究如何获得反应行为的唯一方法是通过函数...... -
@joran 现在说得通了。我不知道反应式返回一个函数。感谢您的解释!
标签: r shiny reactive-programming