作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发我的 Shiny 应用程序,我必须做的一件事是调用一个函数(它返回一个对象列表),然后在不同的多个调用中使用该函数的结果。不用说,我不想每次都需要引用列表中的对象之一时多次调用该函数。我将如何以最有效的方式实现这一点?
例如,函数类似于 -
function_to_get_list <- function(){
# first, input data is read at some point then this function is called
if(!is.null(input_data)){
... some processing and calls to create the list of objects
return(list_of_object)
}
else
return(NULL)
}
现在,我想调用此函数一次并将结果保存在一个变量中,这是我需要知道如何正确执行此操作的地方。
list_of_objects <- function_to_get_list()
然后只使用该变量来引用该列表的元素。
output$text1 <- renderText({
list_of_objects[[1]]
})
output$text2 <- renderText({
list_of_objects[[2]]
})
# use of renderText is just to illustrate the calls to use the list
我希望我清楚我想使用上面的示例实现什么,如果没有,请告诉我。
提前致谢!
AK
最佳答案
您可以使用 reactiveValues()
来做到这一点。 Reference
values <- reactiveValues()
function_to_get_list <- function(){
# first, input data is read at some point then this function is called
if(!is.null(input_data)){
... some processing and calls to create the list of objects
values[[1]] <- list_of_objects
}
else
return(NULL)
}
output$text1 <- renderText({
values[[1]][[1]]
})
output$text2 <- renderText({
values[[1]][[2]]
})
关于shiny - 如何调用一次函数然后多次使用结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37634614/
我是一名优秀的程序员,十分优秀!