Simon Woodward
Simon Woodward

Reputation: 2026

Shiny renderText not reacting to change in reactiveValues

I have a small and frustrating problem in my app. This object output$timeheader <- renderText() does not react when user$stationtype or user$stationvar change but only when stationname() changes.

I have tried to make a reprex but I can't reproduce the bug. It works as intended in this example. In my app, changes to user$stationtype or user$stationvar don't trigger output$timeheader <- renderText().

library(shiny)

ui <- tagList(
  navbarPage(
    title = "navbarPage", 
    tabPanel(
      title = "tabPanel",
      tags$div(
        absolutePanel(
          tabsetPanel(
            tabPanel(
              title = "title",
              tags$div(
                actionButton("changesel", "Choose a different variable"),
                textInput("map_marker_click", "Enter station name"),
                h4(strong(textOutput("timeheader")))
              )
            )
          )
        )
      )
    )
  )
)

server <- function(input, output, session){
  # collect inputs needed by model
  user <- reactiveValues(
    stationvar = NA,
    stationtype = NA
  )
  # observe changes in ui and pass to user$
  observe({
    input$changesel # observe button click
    ot <- runif(1)
    isolate({
      obsnut <- ifelse(ot > 0.6, "DIN", "NONE") # obs to use
      obsgroup <- ifelse(ot > 0.3, "Streams", "none")
      if (!setequal(obsnut, user$stationvar)){
        cat("user$stationvar <-", obsnut, "\n")
        user$stationvar <- obsnut
      }
      if (!setequal(obsgroup, user$stationtype)){
        cat("user$stationtype <-", obsgroup, "\n")
        user$stationtype <- obsgroup
      }
    }) # end isolate
  })
  # timeheader ####
  output$timeheader <- renderText({
    # req(user$stationtype, user$stationvar)
    # FIXME why doesn't this stupid text show!!! ####
    cat("Time series header:", user$stationtype, stationname(), user$stationvar, "\n")
    if (isTRUE(stationname() > "")){
      "Time series plot:"
    } else if (isTRUE(user$stationvar != "NONE")){
      "Enter station name to view data:"
    } else {
      ""
    }
  })
  ## click on marker to get stationname ####
  stationname <- reactive({
    req(isTRUE(user$stationvar != "NONE"))
    cat("Marker click\n")
    input$map_marker_click
  })
}

shinyApp(ui, server)

Upvotes: 0

Views: 826

Answers (1)

SmokeyShakers
SmokeyShakers

Reputation: 3402

My guess is that it has something to do with the stationame reactive being null due to req. Try:

  stationname <- eventReactive(input$map_marker_click,{
    req(isTRUE(user$stationvar != "NONE"))
    cat("Marker click\n")
    input$map_marker_click
  }, ignoreNULL = F)

Upvotes: 2

Related Questions