我试图在我的自定义函数中调用反应函数,但我不能。这样做的最佳方法是什么,这里是简化的代码,我不能将所有内容作为函数参数传递,因为会有太多不能通过。
library(shiny);
myf<-function(){ return(psd()$y) }
shinyApp( ui = textOutput("test2"),
server = function(input, output) {
psd<-reactive({ return(data.frame(x=10,y=30)) })
output$test2 <- renderText({ myf() })
}
)
此代码生成错误:Error in myf() : could not find function "psd"
调用自定义函数的最佳方法是什么?它使用了shinyserver中的其他函数?
答案 0 :(得分:2)
这取决于你是否希望你的功能是否具有反应性。
在使用反应性的情况下:
myf <- reactive({return(psd()$y})
如果您不希望它被反应使用
myf <- function() {return(isolate(psd$y))}
您的被动表达式没有被动值(如输入$)。因此它不会被创建。要更好地了解反应性,请阅读this awesome instruction。
此外,您可能尝试避免硬编码并尝试添加要在函数中调用的反应函数,如下所示:
myf<-function(func){ return(func()$y) }
然后:
output$test2 <- renderText({ myf(psd) })