user3116408
user3116408

Reputation: 329

Shiny - How to conditionally update a reactive or reactiveValue?

I was trying to update a value with some ifelse conditions.

For instance, in the code below, I just want to update the reactive "multiplier" if the reactive "decim_factor" changes its value, and not if it's only updated. I tried to use the observeEvent with "decim_factor" as eventExpr, but it didn't work.

library(shiny)
rm(list = ls())

ui <- shinyUI(pageWithSidebar(
  headerPanel("Click the button"),
  sidebarPanel(
    numericInput("index","Numeric Input",1),

    p("This is the criteria to change the decim_factor depending on numericInput"),
    br(),
    p(" num_input >= 20 - decim_factor=3"),
    p("20  > num_input >= 10 - decim_factor=2"),
    p("10  > num_input       - decim_factor=1")

  ),
  mainPanel(
    verbatimTextOutput("decim"),
    verbatimTextOutput("multip")
  )
))


server <- shinyServer(function(input, output) {

  decim_factor <- reactive({
    num_input <- input$index
    if(num_input>=20)                      {decim_factor <- 3} 
    else if(10<=num_input & num_input<20)  {decim_factor <- 2}
    else                                    decim_factor <- 1
    as.numeric(decim_factor)
  })
  #I was trying to calculte the 'multiplier' only if 'decim_factor' changes its value
  #and not if it is updated
  #
  #I tried to use observeEvent, depending on decim_factor() but it didn't work
  #If I put the observeEvent function as comment it works, but the multiplier
  #updates every time I change the numeric_input
  observeEvent(decim_factor(),{
    multiplier <- reactive({
      decim_factor()*10
    })
  })

  #The outputs come with da Sis.time() just to check if the values are updated
  output$decim <- renderText({paste("Decim = ",decim_factor(), ";",Sys.time())})
  output$multip <- renderText({paste("Muliplier =", multiplier(), ";",Sys.time())})
}) 

shinyApp(ui,server)

Upvotes: 1

Views: 3126

Answers (1)

NicE
NicE

Reputation: 21425

Even when the value of your decim_factor does not change, you reset it in the reactive so it triggers the observeEvent that updates the multiplier. If you want to prevent this, you could do:

server <- shinyServer(function(input, output) {
        values <- reactiveValues(decim_factor=1,multiplier=10)

        observeEvent(input$index,{
                num_input <- input$index
                if(num_input>=20 & values$decim_factor != 3) {values$decim_factor <- 3} 
                else if(10<=num_input & num_input<20 & values$decim_factor != 2 )  {values$decim_factor <- 2}
                else if(10>num_input & values$decim_factor != 1) values$decim_factor <- 1
        })

        observeEvent(values$decim_factor,{
                values$multiplier <- values$decim_factor*10
        })

        output$decim <- renderText({paste("Decim = ",values$decim_factor, ";",Sys.time())})
        output$multip <- renderText({paste("Muliplier =", values$multiplier, ";",Sys.time())})
})

The if/else in the first observeEvent only update values$decim_factor when the value needs to be changed.

Upvotes: 2

Related Questions