L. Blanc
L. Blanc

Reputation: 2310

Shiny ConditionalPanel fails to open on setInputValue update

I'm learning how to use Shiny and ran into a problem.

I'm trying to make a ConditionalPanel open based on calling Shiny.setInputValue('myEvent', 'open', {priority: 'event'});

However, the panel doesn't open. I checked that the javascript event is being fired, so it seems like:

I suspect it has something to do with reactivity, but I'm stuck. Code is below:

library(shiny)
library(htmltools)
library(htmlwidgets)
library(leaflet)
library(leaflet.providers)

places = read.csv("file.csv")

names = places$Name

ui <- fluidPage(
   
    sidebarLayout(
        sidebarPanel(
            width = 3,
        ),
        
        mainPanel(
            width = 9,
            fluidRow(column(12, leafletOutput("mymap", height = 550))),
            conditionalPanel(condition = "input.myEvent == 'open'",
                             fluidRow(verbatimTextOutput("myEvent")))
        )
    )
)

server <- function(input, output) {
   
    output$mymap <- renderLeaflet({
                        
        mapdata = places[places$Name %in% names,]
        
        leaflet() %>%
             addProviderTiles(
                 providers$Esri.OceanBasemap,
                 options = providerTileOptions(noWrap = TRUE)
             ) %>%
            onRender("
                     function(el, x) {
                         this.on('popupopen', function(e) {
                             Shiny.setInputValue('myEvent', 'open', {priority: 'event'});
                         });

                         this.on('popupclose', function(e) {
                             Shiny.setInputValue('myEvent', 'close', {priority: 'event'});
                         });
                     }")%>%
            addMarkers( data = mapdata,
                        lng = mapdata$Longitude,
                        lat = mapdata$Latitude,
                        layerId = mapdata$Name,
                        popup = mapdata$Name,
                        options = popupOptions(closeButton = FALSE)) 

    })
}
shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 89

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

An input value created with Shiny.setInputValue can trigger a conditional panel, as shown by this basic app:

library(shiny)

shinyApp(
  ui = basicPage(
    actionButton(
      "x", "X", onclick = "Shiny.setInputValue('test', true);"
    ),
    conditionalPanel(
      "input.test === true",
      div("Hello")
    )
  ),
  server = function(input, output){
    observe(print(input[["test"]]))
  }
)

A problem in your app is verbatimTextOutput("myEvent"), which renders nothing, since there's no output[["myEvent"]] in the server function. So it's possible that the conditional panel works (since your app relies on a CSV file, we can't try), but this panel is empty anyway.

Upvotes: 1

Related Questions