Shiny 闪亮与动态图包

Shiny 闪亮与动态图包,shiny,dygraphs,Shiny,Dygraphs,我正在尝试使用闪亮的动态图。我试图生成一个图表,根据输入变量显示时间序列数据。数据样本如下所示: date product sold 1 2015-01-01 a 1 2 2015-01-01 b 20 3 2015-02-01 a 2 4 2015-02-01 b 15 5 2015-03-01 a 3 6 2015-03-01 b 10 7 2015-04-

我正在尝试使用闪亮的动态图。我试图生成一个图表,根据输入变量显示时间序列数据。数据样本如下所示:

     date      product sold
1  2015-01-01       a    1
2  2015-01-01       b   20
3  2015-02-01       a    2
4  2015-02-01       b   15
5  2015-03-01       a    3
6  2015-03-01       b   10
7  2015-04-01       a    4
8  2015-04-01       b    5
9  2015-05-01       a    5
10 2015-05-01       b    1
我想要的是一个带有产品变量复选框控件的时间序列图(查看产品a、b或两者)

用户界面和服务器代码为(shinydashboard软件包):

库(闪亮)
图书馆(动态图)
图书馆(dplyr)
图书馆(xts)

ui在这部分代码中,您必须小心:

data_f <- filter(data_products,
               product==input$type)

已编辑

如果您希望在选择
a
b
时有两行,则必须更改数据格式-必须从长到宽。这样做的原因是,您可以使用
xts
轻松创建一个双变量时间序列,
dygraph
将绘制两条单独的线

使用Hadley Wickham提供的
Reformae2
软件包可以轻松实现从长到宽的转换

# Copy data from your example
data_products <- read.table(con<-file("clipboard"),header=T)
data_products$product <- as.factor(data_products$product)
# Reshape 
data_products <- dcast(data_products, date ~ product)
由于数据的新性质,您必须稍微更改服务器端的代码。我在代码中留下了注释

ui <- dashboardPage(
  skin = "black",
  dashboardHeader(title = "title"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Results", tabName = "Result1", icon = icon("th"))
    )),
  dashboardBody(
    tabItems(

      tabItem(tabName = "Result1",
              fluidRow(
                dygraphOutput("Graph")
              ),

              sidebarPanel(
                uiOutput("output1")
              )
      )
    )
  )
)

server <- function(input, output) {

  output$Graph <- renderDygraph({
    req(input$type) # require that input$type is available

    # Due to the wide format we have to select columns
    data_f <- data_products[, c("date", input$type)]
    # univariate or bivariate time series
    xts(data_f[-1], as.Date(data_f$date, format = "%Y-%m-%d")) %>%
      dygraph()
  })

  output$output1 <- renderUI({
    # Since we now have data in wide format, the choices are 
    # the names of columns (expect date)
    checkboxGroupInput("type", "Choose product", 
                       choices = names(data_products)[-1])
  })
}

shinyApp(ui, server)

ui请告诉我们您认为哪种方法是最好的方法,以及由此产生的错误。谢谢,它很有效!还有一件事。如果我现在在复选框中同时选择“a”和“b”,则只会显示一行,而不是两行。。。我知道这些都是初学者的问题,但我是新手。谢谢。我更新了上面的帖子。事实证明,解决方案非常简单,因为它只需要更改数据的格式。这太棒了!我找到的最好答案是——令人惊讶的是,在shiny+动态图上没有太多(特别是在这个多选问题上)。谢谢
# Copy data from your example
data_products <- read.table(con<-file("clipboard"),header=T)
data_products$product <- as.factor(data_products$product)
# Reshape 
data_products <- dcast(data_products, date ~ product)
        date a  b
1 2015-01-01 1 20
2 2015-02-01 2 15
3 2015-03-01 3 10
4 2015-04-01 4  5
5 2015-05-01 5  1
ui <- dashboardPage(
  skin = "black",
  dashboardHeader(title = "title"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Results", tabName = "Result1", icon = icon("th"))
    )),
  dashboardBody(
    tabItems(

      tabItem(tabName = "Result1",
              fluidRow(
                dygraphOutput("Graph")
              ),

              sidebarPanel(
                uiOutput("output1")
              )
      )
    )
  )
)

server <- function(input, output) {

  output$Graph <- renderDygraph({
    req(input$type) # require that input$type is available

    # Due to the wide format we have to select columns
    data_f <- data_products[, c("date", input$type)]
    # univariate or bivariate time series
    xts(data_f[-1], as.Date(data_f$date, format = "%Y-%m-%d")) %>%
      dygraph()
  })

  output$output1 <- renderUI({
    # Since we now have data in wide format, the choices are 
    # the names of columns (expect date)
    checkboxGroupInput("type", "Choose product", 
                       choices = names(data_products)[-1])
  })
}

shinyApp(ui, server)