reactive variables

I wish I could use the same reactive variable several times with different values of the same species.


ui <- fluidPage(
actionButton("btn_change", "Change Values"),
actionButton("btn_print","Print values")
)

server <- function(input, output) {
a <- F
c <- F

observeEvent(input$btn_change, {
a <<- F
c <<- T
})

observeEvent(input$btn_print, {
cat("a =",a,"c =",c,"n")
})
}

shinyApp(ui, server)


two variables a and b start on the server, but below they reassign new values but of the same type. I want to do the same but with reactive variables. thank you for your answer

ui <- fluidPage(
  actionButton("btn_change", "Change Values"),
  actionButton("btn_print","Print values"),
  textOutput("txt1")
)

server <- function(input, output) {
  
  a <- reactiveVal(FALSE)
  c <- reactiveVal(FALSE)

  
  observeEvent(input$btn_change, {
    a(FALSE)  # pass FALSE value into 'a' reactive value
    c(TRUE) # pass TRUE value into 'c' reactive value
  })
  
  observeEvent(input$btn_print, {
    cat("a =",a()," c =",c(),"\n")
  })
  
  output$txt1 <- renderText({
    paste0("a =",a()," c =",c())
  })
}

shinyApp(ui, server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.