rshinyggvis

Dynamic name for ggvisoutput in Shiny


Suppose I have the following code in server:

df%>%
  ggvis(~ratio, ~height, fill = ~ps, key := ~id) %>% 
  layer_bars() %>%
  bind_shiny('rds', 'ui_rds')

And the following in ui:

fluidRow( box(title = "RD",width = 6, ggvisOutput('rds')))

The question is if the name of the output is variable and change in time, How can I set this in ui?

In the other words, if the code of server would be like the following:

x <<- "some value which will be changed reactively"
df%>%
  ggvis(~ratio, ~height, fill = ~ps, key := ~id) %>% 
  layer_bars() %>%
  bind_shiny(x, paste('ui_',x))

What is supposed to be the code of ui?


Solution

  • I don't see why you would want to do this, but renderUI is always a possibility to deal with dynamic IDs.

    In my example below, the plot_id agument in ggvis::bind_shiny will be created by a textInput. renderUI/uiOutput is used to "channel" the plot to a single id "plot_wrapped"

    library(ggvis)
    library(shiny)
    
    shinyApp(
      fluidPage(
        textInput("plot_id", value = "some_id",
                  "choose plot id (avoid spaces and special characters)"),
        uiOutput("plot_wrapped")
      ),
      function(input, output, session){
        observe({
          mtcars %>% ggvis(~mpg, ~wt) %>% layer_points() %>%
            bind_shiny(input$plot_id)
        })
    
        output$plot_wrapped <- renderUI({
          ggvisOutput(input$plot_id)
        })
      }
    )
    

    As mentioned above tough, I suggest not to use dynamic IDs in shiny unless it is really necessary.