【问题标题】:using dygraph with shiny使用带闪亮的 dygraph
【发布时间】:2015-07-22 11:12:33
【问题描述】:

我最近开始使用 dygraph,到目前为止我非常喜欢它。我曾尝试在闪亮的情况下使用它,但没有取得多大成功。虽然我的脚本没有产生任何错误,但它也没有产生任何图表!
你有没有机会指引我正确的方向?

这是我的数据示例:

> head(df2)
        date     Variety   Count Price Value Quantity TotalKg
1 2014-11-06 CRIPPS PINK   80-90   204  3670       18     333
2 2014-11-06 CRIPPS PINK 120-135   181 10150       56    1036
3 2014-11-06  CRIPPS RED   80-90   221 26910      122    2257
4 2014-11-06  CRIPPS RED 100-110   205 22910      112    2072
5 2014-11-06  CRIPPS RED 120-135   193 58950      306    5661
6 2014-11-06      TOPRED   80-90   167  7350       44     814

使用 Variety 和 Count 变量,我想绘制一段时间内的价格图表。

这是我的 ui.R

library(dygraphs)
library(shiny)

shinyUI(fluidPage(
  titlePanel("Apples Prices"),

  sidebarLayout(
          sidebarPanel(
            selectInput("productname", "Select your product",
                        choices = levels(df2$Variety)),
            selectInput("count",  "Select your size",
                        choices = levels(df2$Count))),

            mainPanel(
              dygraphOutput("applesgraph"))
          )))

和服务器端:

library(shiny)
library(dygraphs)
library(dplyr)
library(xts)

shinyServer(function(input, output) {

  dfa <- reactive({df2 %>% filter(Variety == input$productname & 
                                    Count == input$count )})

#the first graph which is price over time (input: variety, count, date)
  output$applesgraph <- renderDygraph({
   xts(dfa()$Price, order.by = dfa()$date) %>% dygraph()
  })
})

我能感觉到我对 dplyr 和时间序列对象的方法有误......那我应该什么时候过滤?我尝试了很多组合,但是我总是遇到诸如“不可子集”之类的错误。

提前感谢您能给我的任何光线/方向。

【问题讨论】:

  • 您的应用程序一切正常。您的问题似乎来自您的数据输入。您能否以某种方式提供您正在使用的数据或更具有代表性的样本? (这里只给了一个日期,所以在X轴上用起来不太方便)
  • 你能不能打电话给dput(df2),比如把内容粘贴到pastebin.comstr(df2) 没有帮助
  • 这里是:pastebin.com/iVsLKwAX
  • 如果您需要,这里是整个文件的链接:drive.google.com/a/ifitwala.com/file/d/…
  • 好吧,我不知道该说什么...一切都与您的代码完美配合。当我用你的数据测试它时,我可以看到这些变化和预期的输出变化。您能否尝试更新您的 dygraph、dplyr、shiny 软件包,看看是否能解决问题?

标签: r shiny dygraphs


【解决方案1】:

由于您需要server.R(用于图表)和ui.R(用于输入列表)中的输入数据,我在server.R 中添加了renderUI({...}),在ui.R 中添加了uiOutput(...)

# server.R
library(shiny)
library(dygraphs)
library(dplyr)
library(xts)

shinyServer(function(input, output) {
  data <- read.csv("cleanApples.csv") %>%
    filter(Quantity > 10)

  #the first graph which is price over time (input: variety, count, date)
  output$applesgraph <- renderDygraph({
    if (is.null(input$productname) || is.null(input$count)) return(NULL)
    filtered <- filter(data,
                       Variety == input$productname,
                       Count == input$count )
    xts(filtered$Price, as.Date(filtered$date, format = "%Y-%m-%d")) %>%
      dygraph()
  })

  output$productnames <- renderUI({
    selectInput("productname", "Select your product",
                choices = levels(data$Variety))
  })

  output$counts <- renderUI({
    selectInput("count",  "Select your size",
                choices = levels(data$Count))
  })
})

# ui.R
library(dygraphs)
library(shiny)

shinyUI(fluidPage(
  titlePanel("Apples Prices"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("productnames"),
      uiOutput("counts")
    ),

    mainPanel(
      dygraphOutput("applesgraph"))
  )))

现在可以in shinyapps.io

【讨论】:

  • 感谢@faidherbard,我可以看到它在您的shinyapps 实例上运行良好;-)
  • 不幸的是,我仍然不知道自己做错了什么,但正如您所见,我什么也没做。 frankyd.shinyapps.io/shinyApps4dygraph。我现在真的只是复制粘贴您的代码。现在我也收到了这个警告:Warning in (function (..., deparse.level = 1) : number of columns of result is not a multiple of vector length (arg 1)
  • 非常感谢您提供的所有时间和改进。我确实从你身上学到了很多。不幸的是,问题似乎出在任何闪亮的应用程序和 dygraph 上。甚至他们教程中的那个也不适合我 (rstudio.github.io/dygraphs/shiny.html) 应用程序很好,但没有图表。 :-( 所以我仍在寻找导致我机器上冲突的原因......此外,虽然我们在 shinyapps.io 上有相同的代码,但您的代码显示的是图表而不是我的。无论如何......再次感谢为您所有的时间和努力!
  • 以防万一它可能有用... ```> sessionInfo() R 版本 3.2.0 (2015-04-16) 平台:x86_64-apple-darwin13.4.0(64 位) ) 运行于:OS X 10.10.1 (Yosemite) 语言环境:[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8 附加基础包:[1] stats graphics grDevices utils datasets methods base 其他附加包:[1] dygraphs_0.4.4.1 shiny_0.11.1 opera_1.0.1
猜你喜欢
  • 2021-06-19
  • 2016-07-25
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 2018-11-23
  • 1970-01-01
  • 2016-12-25
相关资源
最近更新 更多