2016-08-11 147 views
1

我想用dygraphs闪亮。我尝试生成一个图表,根据输入变量显示时间序列数据。的数据的样品如下:与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-01  a 4 
8 2015-04-01  b 5 
9 2015-05-01  a 5 
10 2015-05-01  b 1 

我想是一个时间序列图与复选框的产品变量控制(见产品A,B或两者)。

用户界面和服务器端的代码是(shinydashboard包):

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


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({ 

    data_f <- filter(data_products, 
       product==input$type) 
    xts(data_f$sold, as.Date(data_f$date, format = "%Y-%m-%d")) %>% 
    dygraph() 
    }) 
    output$output1<-renderUI({ 
    selectizeInput("type","Choose product", 
    choices=levels(data_products$product), multiple=TRUE) 
    }) 
    } 

    shinyApp(ui, server) 

试了几种方法,但总是出现错误。提前感谢您的任何建议。

+0

向我们展示您认为这是您的最佳方法以及您得到的错误。 –

回答

0

你必须在这部分代码小心:

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

在这个例子中,这取决于你的选择input$type可以包含0个,1个或2个元素。如果它包含一个元素"a""b"那么一切都很好,但在其他情况下,你会得到错误或警告。

如果您尚未在您的Widget中选择任何值,input$type将返回NULL。因此,逻辑比较将会失败,你将会得到错误。为避免这种情况发生,在使用缺少的输入之前,可以使用reqvalidate函数,这些函数可以理解为“要求输入可用”。 Here你可以阅读更多有关处理缺失输入的信息。

如果选择既"a""b"product==input$type会返回一个警告,因为==不适合多重比较工作。取而代之的是将其改为%in%

既然你想有一个复选框,我改变selectInputcheckboxGroupInput


完整的示例:

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

# I pasted your example data to exces and then readed it into R with these 
# two lines of the code. It seems that product has to be a factor, because you 
# use 'levels(data_products$product)' 
# data_products <- as.data.frame(read_excel("~/Downloads/data.xlsx"))[-1] 
# data_products$product <- as.factor(data_products$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 

    data_f <- filter(data_products, 
        product %in% input$type) # use "%in%" instead of "==" 
    xts(data_f$sold, as.Date(data_f$date, format = "%Y-%m-%d")) %>% 
     dygraph() 
    }) 
    output$output1 <- renderUI({ 
    # selectizeInput("type","Choose product", 
    #    choices=levels(data_products$product), multiple=TRUE) 
    checkboxGroupInput("type", "Choose product", 
         choices = levels(data_products$product), 
         selected = levels(data_products$product)) 
    }) 
} 

shinyApp(ui, server) 

EDITED

如果您想在选择ab时选择两行,则必须更改数据的格式 - 您必须从较长到较宽。原因是你可以很容易地创建xts的双变量时间序列和dygraph将绘制两个单独的行。

Hadley Wickham的reshape2包可以轻松实现从长到宽。

# 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) 
+0

谢谢,它的工作原理!还有一件事。如果我现在在复选框中选择“a”和“b”,只会出现一行,而不是两个......我知道这些是初学者问题,但我是R和Shiny的新手。谢谢。 – TomasD

+0

我更新了我的帖子。事实证明,解决方案非常简单,因为它只需要更改数据的格式 –

+1

非常感谢您的帮助! – TomasD