Shiny 如何调用函数一次,然后多次使用结果
我正在开发我的闪亮应用程序,我必须做的一件事是调用一个函数(返回一个对象列表),然后在不同的多次调用中使用该函数的结果。不用说,每次需要引用列表中的一个对象时,我都不想多次调用该函数。我如何以最有效的方式实现这一点 例如,函数类似于-Shiny 如何调用函数一次,然后多次使用结果,shiny,shiny-server,Shiny,Shiny Server,我正在开发我的闪亮应用程序,我必须做的一件事是调用一个函数(返回一个对象列表),然后在不同的多次调用中使用该函数的结果。不用说,每次需要引用列表中的一个对象时,我都不想多次调用该函数。我如何以最有效的方式实现这一点 例如,函数类似于- function_to_get_list <- function(){ # first, input data is read at some point then this function is called if(!is.null(input_
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)
}
function\u to\u get\u list您可以通过使用reactiveValues()
来实现这一点
values在修复了一些引用列表元素的索引之后,我能够让它工作
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("action", "RUN")
),
mainPanel(
textOutput("text1"),
textOutput("text2")
)
)
)
server <- function(input, output) {
values <- reactiveValues()
function_to_get_list <- function(){
return(list(c(1:5)))
}
values[['1']] <- function_to_get_list()
output$text1 <- renderText({
if(input$action > 0)
paste("1st element of list ", values[['1']][[1]][[1]])
})
output$text2 <- renderText({
if(input$action > 0)
paste("2nd element of list ", values[['1']][[1]][[2]])
})
}
shinyApp(ui = ui, server = server)
库(闪亮)
用户界面可能会提供问题,所以我可以调试我的答案。干杯,谢谢,米克尔。让它发挥作用。快速提问-所以我们只能有一个值@ashishkul,您也可以创建另一个变量,如values2
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
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]]
})
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("action", "RUN")
),
mainPanel(
textOutput("text1"),
textOutput("text2")
)
)
)
server <- function(input, output) {
values <- reactiveValues()
function_to_get_list <- function(){
return(list(c(1:5)))
}
values[['1']] <- function_to_get_list()
output$text1 <- renderText({
if(input$action > 0)
paste("1st element of list ", values[['1']][[1]][[1]])
})
output$text2 <- renderText({
if(input$action > 0)
paste("2nd element of list ", values[['1']][[1]][[2]])
})
}
shinyApp(ui = ui, server = server)