简体   繁体   English

dygraphs中dyEvents的日期向量输入:R有光泽

[英]date vector input for dyEvents in dygraphs: R shiny

I have vector of dates which can be chosen from a drop-down box where multiple dates can be chosen . 我有一个日期向量,可以从一个下拉框中选择,可以选择多个日期 By selecting those dates event lines will appear at the dates which has been chosen. 通过选择这些日期,事件行将出现在已选择的日期。 However I see that the dyEvent is not taking a vector of dates.When date in dyEvent is replaced by a vector event lines do not appear. 但是我看到dyEvent没有采用日期的向量。当dyEvent中的日期被向量事件行替换时,不会出现。 I have provided the code below. 我提供了以下代码。 Any help would be greatly appreciated. 任何帮助将不胜感激。

ui.R
library(shiny)
library(dygraphs)
shinyUI(fluidPage(
  titlePanel("Tool"),
  sidebarLayout(
    sidebarPanel(
      uiOutput("choosedates")
      ),
    mainPanel(
      dygraphOutput("plot")
      )
  )
  ))


server.R
library(shiny)
library(dygraphs)
shinyServer(function(input, output) {
  Data<-data.frame(Time=seq(as.Date("7/29/2012","%m/%d/%Y"),as.Date("8/7/2012","%m/%d/%Y"),by="1 day"),Volume=c(100,150,120,300,250,50,100,120,80,100))
  output$choosedates<-renderUI({
    selectInput("dates","Choose dates to be marked by event line",as.character(Data$Time),multiple=TRUE)
  })
  library(xts)
  Dataxts<-reactive({
    xts(Data$Volume,order.by=as.Date(Data$Time,"%m/%d/%Y"))
  })
  output$plot<-renderDygraph({
    dygraph(Dataxts(), main = "Visualization")%>%
      dyEvent(date =as.character(input$dates), "Dummy", labelLoc = "bottom",color = "red", strokePattern = "dashed")%>%
      dyAxis("y", label = "Units sold in millions")%>%
      dyOptions(axisLabelColor = "Black",digitsAfterDecimal = 2,drawGrid = FALSE)
  })
  })

I don't have any prior experience with the package dygraphs so I can't guarantee this is the simplest approach, but I was able to get the application to function correctly with the code below. 我没有任何关于包dygraphs经验,所以我无法保证这是最简单的方法,但我能够通过下面的代码使应用程序正常运行。


ui.R ui.R

library(shiny)
library(dygraphs)
##
shinyUI(fluidPage(

  titlePanel("Tool"),

  sidebarLayout(

    sidebarPanel(
      uiOutput("choosedates")
    ),

    mainPanel(
      dygraphOutput("plot")
    )
  )
))

server.R server.R

library(shiny)
library(dygraphs)
library(xts)
##
shinyServer(function(input, output) {

  Data <- data.frame(
    Time=seq(as.Date("7/29/2012","%m/%d/%Y"),
             as.Date("8/7/2012","%m/%d/%Y"),
             by="1 day"),
    Volume=c(100,150,120,300,250,
             50,100,120,80,100))

  output$choosedates <- renderUI({
    selectInput(
      "dates",
      "Choose dates to be marked by event line",
      as.character(Data$Time),
      multiple=TRUE)
  })

  Dataxts <- reactive({
    xts(
      Data$Volume,
      order.by=as.Date(Data$Time,"%m/%d/%Y"))
  })
  ##
  getDates <- reactive({
    as.character(input$dates)
  })

  addEvent <- function(x,y) {
    dyEvent(
      dygraph=x,
      date=y,
      "Dummy", 
      labelLoc = "bottom",
      color = "red", 
      strokePattern = "dashed")
  }

  basePlot <- reactive({ 
    if (length(getDates()) < 1) {
      dygraph(
        Dataxts(),
        main="Visualization") %>%
        dyAxis(
          "y", 
          label = "Units sold in millions") %>%
        dyOptions(
          axisLabelColor = "Black",
          digitsAfterDecimal = 2,
          drawGrid = FALSE)
    } else {
      dygraph(
        Dataxts(),
        main="Visualization") %>%
        dyAxis(
          "y", 
          label = "Units sold in millions") %>%
        dyOptions(
          axisLabelColor = "Black",
          digitsAfterDecimal = 2,
          drawGrid = FALSE) %>%
        dyEvent(
          dygraph=.,
          date=getDates()[1],
          "Dummy", 
          labelLoc = "bottom",
          color = "red", 
          strokePattern = "dashed")
    }
  })
  ##
  output$plot <- renderDygraph({

    res <- basePlot()
    more_dates <- getDates()
    if (length(more_dates) < 2) {
      res
    } else {
      Reduce(function(i,z){
        i %>% addEvent(x=.,y=z)
      }, more_dates[-1], init=res)
    }

  })

})

The major changes are the functions between the two commented lines ( ## ). 主要变化是两个注释行( ## )之间的功能。 I made a reactive function getDates() that just returns the current selection of dates - I'm not even sure if this is necessary but this is generally what I do with user input. 我做了一个反应函数getDates()getDates()返回当前的日期选择 - 我甚至不确定这是否有必要,但这通常是我对用户输入所做的。 More importantly, I created a (non-reactive) function addEvent that was purposely defined in such a way that is convenient to use with the higher-order function Reduce - ie the x argument represents the previous state (the dygraph object being accumulated), and the y argument is the modification we make to the previous state (the line we are adding). 更重要的是,我创建了一个(非反应性)函数addEvent ,它以一种方便的方式定义,方便与高阶函数Reduce - 即x参数表示先前的状态( dygraph对象被累积), y参数是我们对前一个状态(我们添加的行)的修改。

basePlot() is mostly just a helper function to avoid errors that would most likely be produced when the application first starts (and no date input is selected). basePlot()主要只是一个辅助函数,以避免在应用程序首次启动时很可能产生的错误(并且没有选择日期输入)。 If no dates are chosen, it outputs the base dygraph , and if one or more are chosen, it outputs the base graph plus only the first date line. 如果没有选择日期,则输出基础dygraph ,如果选择了一个或多个,则输出基本图形加上第一个日期行。

To return a final plot object (in output$plot ), we create the base plot ( res ), and check for the existence of multiple date inputs. 要返回最终的绘图对象(在output$plot ),我们创建基本绘图( res ),并检查是否存在多个日期输入。 If two or more lines are requested by the user, we use Reduce and the UDF addEvent to accumulate modifications (date lines), using res as the initial value, and the 2nd through last date inputs as the modifying values. 如果用户请求两行或更多行,我们使用Reduce和UDF addEvent来累积修改(日期行),使用res作为初始值,将第二个到最后一个日期输入作为修改值。


Here are a few screen shots of the running applications: 以下是正在运行的应用程序的一些屏幕截图:

Startup: 启动: 在此输入图像描述


First input: 第一输入: 在此输入图像描述


Multiple inputs: 多输入: 在此输入图像描述

I think it's a better solution, at least, shorter: 我认为这是一个更好的解决方案,至少更短:

p <- dygraph(df_sub)
for (i in 1:dim(df_sub)[1]){
p <- p %>% dyEvent(date = df_sub$Date[i], label='xyz', labelLoc='bottom')
}

by: user2996185 in Sep 29 '15 at 7:06 作者:user2996185于2015年9月29日7:06

https://stackoverflow.com/a/32837723/6253668 https://stackoverflow.com/a/32837723/6253668

dynamically add events to dygraph r 动态地向dygraph r添加事件

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

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