2016-08-11 150 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