I'm building a Shiny app where selectInput
functionality is to be presented both as a traditional HTML select dropdown and as a clickable image map. My current strategy is to link the image map polygons back to the app with a URL parameter added, parse that URL, and update the select. This of course resets the app every time, which is ok but not great, and the UI flicker is not very graceful.
My questions are:
- Is there a better way to capture the image clicks and update the input?
- If I continue with my solution, can I delay the rendering on the
selectInput
so it is not shown until after the URL is parsed so that it doesn't show the default selection for a moment before flickering over to the parameterized selection?
Here's a demo:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("letter_select",
"Pick a letter:",
choices = c('A' = 1, 'B' = 2, 'C' = 3, 'D' = 4))
),
mainPanel(
h3('Or click a letter'),
img(src = 'testpattern.png', usemap = '#image-map'),
HTML('
<map name="image-map">
<area target="_self" title="A" href="?letter=1" coords=0,0,50,0,50,50,0,50" shape="poly">
<area target="_self" title="B" href="?letter=2" coords=50,0,100,0,100,50,50,50" shape="poly">
<area target="_self" title="C" href="?letter=3" coords=0,50,50,50,50,100,0,100" shape="poly">
<area target="_self" title="D" href="?letter=4" coords=50,50,100,50,100,100,50,100" shape="poly">
</map>
')
)
)
)
server <- function(input, output, session) {
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['letter']])) {
updateSelectInput(session, 'letter_select', selected = query[['letter']])
}
})
}
shinyApp(ui = ui, server = server)
With the image being at `www/testpattern.png', shown below.