繁体   English   中英

无法在Shiny App中使用ggvis绘图

[英]Cannot plot with ggvis in Shiny App

经过数小时的修补和谷歌搜索,我想我可能终将走到死路。 我正在学习Shiny R,并尝试使用ggvis库创建散点图。 到目前为止,我有以下代码:

app.R:

library(shiny)
library(ggvis)
library(dplyr)

# Define UI for application
ui <- fluidPage(
         p("hello world"),
         ggvisOutput("plot"),
         p("hello world")
)

# Define server logic
server <- function(input, output, session) {

   #Plot a scatter plot for employment counts
   output$plot <- reactive({

      year <- c(2011, 2012, 2013, 2014, 2015, 2016)
      plotData <- c(123456, 1234567, 234561, 213465, 465791, 222222)

      dataDF <- data.frame(Year = year, Data = plotData)
      layer_points(ggvis(dataDF, ~Year, ~Data))

   })
}

# Knit the ui and server, then run the application 
shinyApp(ui = ui, server = server)

我猜这应该很简单,但是运行此程序却没有任何图。 它只是两次显示hello world ,中间有一个空格。 我仅使用Rstudio复制了此代码,而没有使用Shiny App插件,因此看起来很不错! 我在这里遵循代码,显然我试图简化工作。 我也一直在关注此链接 ,很遗憾,没有运气。

有人有什么想法吗? 非常感谢您的帮助。

?ggvis::ggvisOutput

服务器端

交互式运行ggvis绘图时,它会自动绘制,因为它会触发默认的打印方法。 在闪亮的应用程序中,您需要 使用 bind_shiny 将图显式呈现给特定的占位符

p %>% bind_shiny("plot")

因此,您可以执行bind_shiny(layer_points(ggvis(dataDF, ~Year, ~Data)), "plot")

或使用%>%

data.frame(Year = year, Data = plotData) %>%
  ggvis(~Year, ~Data) %>%
  layer_points() %>%
  bind_shiny("plot")

请注意,您无需将其置于反应状态。

暂无
暂无

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

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