繁体   English   中英

ggvis plot 干扰 R shiny 应用程序的初始呈现

[英]ggvis plot interferes with initial rendering of an R shiny app

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

此外,如果我们在没有 ggvis plot 的选项卡上启动应用程序,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(...)将 plot 分配为对 output 的反应。

只应使用bind_shiny将 ggvis 图形连接到 shiny 应用程序:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM