Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change variable outside of reactive function/context

I am looking for a way to change a variable, which has been initialized outside of the reactivity context in a shiny app.

Meaning: I have two variables which I need to set, depending on what happens in the shiny app, to TRUE or FALSE.

That needs to be done from an an reactive function.

Example:

a <- FALSE
c <- FALSE

observeEvent(input$test, { #this triggers c to change to TRUE
    a <- FALSE
    c <- TRUE
)}

Some user clicks on the button with the label test

> c
> TRUE
like image 625
Stophface Avatar asked Mar 15 '23 18:03

Stophface


1 Answers

If I understand your question correctly (you cannot update c inside the observeEvent function), you need to put your c variable in a reactive and change it inside the observeEvent:

variables = reactiveValues(a = FALSE, c = FALSE)

observeEvent(input$test, {
    variables$a = FALSE
    variables$c = TRUE
})

Then you can use variables$c in your code, and anything depending on variables will be updated if you press the button (e.g. a plot).

like image 134
Paul Hiemstra Avatar answered Mar 17 '23 16:03

Paul Hiemstra