reactiveValue example

Published onesixx on

 

 

https://stackoverflow.com/questions/37634614/how-to-call-a-function-once-and-then-use-the-results-multiple-times

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)

 

 

Categories: Shiny

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.