ggvis plot 会干扰 R shiny 应用程序的初始渲染

ggvis plot interferes with initial rendering of an R shiny app

我有一个类似于下面的应用程序,我试图在其中根据输入渲染一些 html 并显示 ggvis 图。该图似乎干扰了 htmlOutput() 因为它不会在应用程序首次加载时出现。当输入发生变化时,它最终会按预期呈现。

此外,如果我们在没有 ggvis 图的选项卡上启动应用程序,html 从一开始就按预期呈现。

library(shiny)
library(ggvis)

ui <- fluidPage(
  sliderInput("slider", label = "slider", min = 1, max = 5, value = 3),
  htmlOutput("text"),
  tabsetPanel(
    # selected = "tab b", # if I uncomment this line, the htmlOutput works as expected
    tabPanel(title = "tab a", ggvisOutput("plot")),
    tabPanel(title = "tab b", "Static text")
  )
)

server <- function(input, output, session) {
  output$plot <- reactive({
    mtcars %>% ggvis(~mpg, ~hp) %>% layer_points() %>% bind_shiny("plot")
  })
  output$text = renderUI({
    paste0("The slider value is ", input$slider)
  })
}

shinyApp(ui = ui, server = server)

那么,在这个应用程序中,为什么 htmlOutput() 在应用程序加载时没有出现?如何让它在应用程序启动时出现?另外,这是某种错误吗?

这里的问题是,您通过 output$plot <- reactive(...).

将绘图指定为对输出的反应

只应使用 bind_shiny 将 ggvis 图形连接到闪亮的应用程序:

library(shiny)
library(ggvis)
library(datasets)

ui <- fluidPage(
  sliderInput("slider", label = "slider", min = 1L, max = nrow(mtcars), value = nrow(mtcars), step = 1L),
  htmlOutput("text"),
  tabsetPanel(
    # selected = "tab b", # if I uncomment this line, the htmlOutput works as expected
    tabPanel(title = "tab a", ggvisOutput("plot")),
    tabPanel(title = "tab b", "Static text")
  )
)

server <- function(input, output, session) {
  observe({
    mtcars[1L:input$slider,] %>% ggvis(~mpg, ~hp) %>% layer_points() %>% bind_shiny("plot")
  })
    
  output$text = renderUI({
    paste0("The slider value is ", input$slider)
  })
}

shinyApp(ui = ui, server = server)