Shiny 中 ggvisoutput 的动态名称

Dynamic name for ggvisoutput in Shiny

假设我在服务器中有以下代码:

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

以及 ui 中的以下内容:

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

问题是如果输出的名称是可变的并且随时间变化, 如何在 ui?

中进行设置

换句话说,如果服务器的代码是这样的:

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

ui 的代码应该是什么?

我不明白你为什么要这样做,但是 renderUI 总是可以处理动态 IDs。

在我下面的示例中,ggvis::bind_shiny 中的 plot_id 参数将由 textInput 创建。 renderUI/uiOutput用于"channel"剧情到单个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)
    })
  }
)

如上所述,我建议不要在 shiny 中使用动态 ID,除非确实有必要。