Is it possible to run a function when a specific reactiveVal
changes?
I imagine it similar to observeEvent(input$someTextInput, {})
where the observer only runs when the value in someTextInput
changes.
I know that I could achieve this with a general observe({})
, but then I run into a problem such as in the following toy example.
library(shiny)
shinyApp(
fluidPage(
textInput('i1', 'Make uppercase'),
textInput('i2', 'Make lowercase'),
textInput('out', 'Output (ideally not editable)')
),
function(input, output, session) {
i1 <- reactiveVal('')
i2 <- reactiveVal('')
observeEvent(input$i1, { i1(toupper(input$i1)) })
observeEvent(input$i2, { i2(tolower(input$i2)) })
observe({ print(i2()); updateTextInput(session, 'out', value = i1()) })
observe({ print(i1()); updateTextInput(session, 'out', value = i2()) })
}
)
Notice that without the print
statements the code would work as I would want it to work. As in, when I change something in input$i1
, that value gets put into my "output", and if I type something into input$i2
, only that value will be put into my output.
However, with those print statements I assume that shiny wants to run both observers as soon as either reactive value i1
or i2
changes, causing the output to only contain the value inside i2
.
Is there some way for me to tell shiny to only run the first observe
when i1
changes and to only run the second observe
when i2
changes?
I also asked this question on StackOverflow, unfortunately however it hasn't gained a lot traction so far.