rstudio/shiny

Input IDs not recognized when inside an html content variable as character.

Closed this issue · 4 comments

System details

Browser Version: Chrome 130.0.6723.119

Output of sessionInfo():

R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 26100)  Matrix products: default  locale: [1] LC_COLLATE=English_Australia.utf8  LC_CTYPE=English_Australia.utf8    LC_MONETARY=English_Australia.utf8 [4] LC_NUMERIC=C                       LC_TIME=English_Australia.utf8      attached base packages: [1] stats     graphics  grDevices utils     datasets  methods   base       other attached packages: [1] shinyjs_2.1.0      bslib_0.8.0        ShinyRating_0.0.1  shinyWidgets_0.8.7 googleway_2.7.8    shiny_1.9.1         loaded via a namespace (and not attached):  [1] Rcpp_1.0.13-1     pillar_1.9.0      compiler_4.2.2    later_1.3.2       jquerylib_0.1.4   tools_4.2.2       pkgload_1.4.0      [8] digest_0.6.37     tibble_3.2.1      jsonlite_1.8.9    memoise_2.0.1     lifecycle_1.0.4   pkgconfig_2.0.3   rlang_1.1.4       [15] DBI_1.2.3         cli_3.6.3         rstudioapi_0.17.1 curl_5.2.3        yaml_2.3.10       xfun_0.49         fastmap_1.2.0     [22] xml2_1.3.6        knitr_1.48        vctrs_0.6.5       htmlwidgets_1.6.4 sass_0.4.9        fs_1.6.5          glue_1.8.0        [29] fontawesome_0.5.2 R6_2.5.1          fansi_1.0.6       pool_1.0.4        auth0_0.2.1       magrittr_2.0.3    promises_1.3.0    [36] htmltools_0.5.8.1 mime_0.12         xtable_1.8-4      httpuv_1.6.15     utf8_1.2.4        cachem_1.1.0      crayon_1.5.3

Example application or steps to reproduce the problem

# Minimal, self-contained example app code goes here

library(shiny) 
library(bslib)
library(googleway)

# Define the UI
ui <- fluidPage(
  google_mapOutput("map")
)

# Define the server
server <- function(input, output) {
  
  # Create some dummy data
  data <- data.frame(
    lat = c(-27.469),
    lon = c(153.0251),
    siq = c(85),
    info = as.character(
      tags$div(id = "content",
               radioButtons(inputId = "test1", label = "Location Type:", choices = c("Street" = "street", "Mall" = "mall"), selected = "street", inline = TRUE),
               p("Go to Score"),
               textOutput("txt"),
               conditionalPanel(
                 condition = "input.test1 == 'street' || input.test1 == null",
                 actionButton("star1", "Bttn 1")
               ),
               conditionalPanel(
                 condition = "input.test1 == 'mall'",
                 actionButton("star2", "Bttn 2")
               )
      )
    )
  )
  
  observe({
    print(paste0("check value: ",input$test1))
  })
  
  output$txt <- renderText({
    paste("You chose: ", input$test1)
  })
  
  # Generate the map with markers and info windows
  output$map <- renderGoogle_map({
    google_map(key = "you-api-key") %>%
      googleway::add_markers(data = data, lat = "lat", lon = "lon", info_window = "info")
  })
}

# Run the app
shinyApp(ui, server)

Describe the problem in detail

I have an app that uses google maps via the package googleway. when I add the markers to the map, I add an html content window to them via its property info_window. This property has to be text. The content I put in there contains a radio button and a conditionalpanel that controls the visibility of another object (button). The problem is that conditionalpanel doesn't seem to work and the input radio buttons either as I can't print the value that they hold from the server. Seem to be that the inputIds are not register or something so shiny doesn't know they exist. Is there a way to fix this?

Regards,
Christian

It is possible, but you need some client-side JavaScript to run when the markers are added. It might be best to go through the googleway developers to request this feature. They'd need to call Shiny.bindAll() on the info window element, and the implementation will depend on how they (or the underlying js library) handle the lifecycle of the DOM elements in that window.

Right, thanks @gadenbuie. If I understood correctly, what you’re saying is that the inputs inside the info_window need to be bind to be used, that’s the reason the conditional panel doesn’t know anything about the input element.

Exactly. And that binding has to happen client-side and is probably best handled by the googleway package.

Thanks @gadenbuie. I will close this now and once I have it solved I will share the solution.