简体   繁体   English

无法在Shiny App中使用ggvis绘图

[英]Cannot plot with ggvis in Shiny App

After several hours of tinkering around and Googling, I think I might after all be at a dead end. 经过数小时的修补和谷歌搜索,我想我可能终将走到死路。 I'm learning Shiny R and am trying to create a scatter plot using ggvis library. 我正在学习Shiny R,并尝试使用ggvis库创建散点图。 So far I have the following code: 到目前为止,我有以下代码:

app.R: 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)

This should be quite simple I guess but I get no plots when I run this. 我猜这应该很简单,但是运行此程序却没有任何图。 It just shows hello world twice with a space in between. 它只是两次显示hello world ,中间有一个空格。 I've replicated this using just Rstudio without the Shiny App addons, and it seemed to graph fine! 我仅使用Rstudio复制了此代码,而没有使用Shiny App插件,因此看起来很不错! I'm following the code here and obviously I tried to simplify the work. 我在这里遵循代码,显然我试图简化工作。 I've also been following this link and unfortunately no luck. 我也一直在关注此链接 ,很遗憾,没有运气。

Would anyone have any ideas? 有人有什么想法吗? Really appreciate the help. 非常感谢您的帮助。

From ?ggvis::ggvisOutput : ?ggvis::ggvisOutput

Server-side 服务器端

When you run ggvis plot interactively, it is automatically plotted because it triggers the default print method. 交互式运行ggvis绘图时,它会自动绘制,因为它会触发默认的打印方法。 In shiny apps, you need to explicitly render the plot to a specific placeholder with bind_shiny : 在闪亮的应用程序中,您需要 使用 bind_shiny 将图显式呈现给特定的占位符

p %>% bind_shiny("plot")

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

Or using %>% : 或使用%>%

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

Note you don't need to put this in a reactive. 请注意,您无需将其置于反应状态。

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

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