0

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)
Simon Woodward
  • 1,946
  • 1
  • 16
  • 24
  • 1
    Perhaps this can help : when Shiny behaves abnormally, the first thing I do is to check if I didn't made the [error to use twice the same output name](https://stackoverflow.com/q/62635457/13513328), for example in two tabs. – Waldi Sep 03 '20 at 19:18
  • Thanks, I checked this. It actually seems that the user$ variables are not getting invalidated, even though I know they're changing. – Simon Woodward Sep 03 '20 at 19:28
  • 1
    could it be that user$ variables aren't invalidated because they're updated in an `isolate`? – Waldi Sep 03 '20 at 19:35
  • They are indeed being updated in an isolate(), but why is this a problem? I thought isolate only prevented reactions to variables *read* within the isolate. But assignments should still work? – Simon Woodward Sep 03 '20 at 19:40
  • Edit: I removed the isolate around the assignment and it still does not react. – Simon Woodward Sep 03 '20 at 19:54
  • 1
    It was just a guess, but you're probably right as shown by the test you did – Waldi Sep 03 '20 at 19:56

1 Answers1

2

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)
SmokeyShakers
  • 3,372
  • 1
  • 7
  • 18