2016-06-14 72 views
0

hola我被困在某物上。我正在創建一個ggplot2 Shiny應用程序,用戶可以在其中選擇顯示在折線圖中的變量數量。我用selectInput參數多,但應用程序崩潰時選擇了多個變量,得到錯誤:在ggplot閃亮應用中顯示可變數量的線條

Warning: Error in .subset2: subscript out of bounds 

這裏是data

服務器:

library(shiny) 
    library(googlesheets) 
    library(reshape2) 
    library(ggplot2) 
    library(scales) 
    #google authorization, token storage, file acquiztion and assignment 


    myData_off <- melt(ridership_hour_off, id.vars = "time") 
    dat_off <- myData_off[myData_off$time != "Total",] 
    dat_off$time_ <- as.POSIXct(paste(dat_off$time), origin = "7:00 AM", format = "%I:%M %p", tz = "UTC") 
    dcast_off <- dcast(dat_off, time_~variable) 

    shinyServer(function(input, output, session) { 
    observe({ 
     monthBy_off <- input$month 
     trick_off <- dcast_off[[monthBy_off]] 
    output$plot_off <- renderPlot({ 
      O <-ggplot(data = data.frame(x = dcast_off$time_, y = trick_off), aes(x=x,y=y)) + 

      geom_line(size = 2, alpha = 0.75) + 
      geom_point(size =3, alpha = 0.75) + 

      ggtitle("Boarding the Bus Ridership December 2015") + 
      labs(x="Time",y="Count")+ 
      theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=32, hjust=0.5)) + 
      theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=22))+ 
     theme_classic() 
     O 
     }) 

     }) 

    }) 

UI:

library(shiny) 
    vars <- list("December" = "dec", 
      "January" = "jan", 
      "February" = "feb", 
      "March" = "mar", 
      "April" = "apr", 
      "May" = "may", 
      "June" = "jun", 
      "July" = "jul", 
      "August" = "aug", 
      "September" = "sep", 
      "October" = "oct", 
      "November" = "nov") 
    shinyUI(fluidPage(
    headerPanel("Cross Acton Transit"), 
    titlePanel("Data Dashboard"), 
    # Your input selection 
    sidebarPanel(
     selectInput("month", "Select plot", vars, selected = "apr", multiple = TRUE) 
    ), 
    # Show the selected plot 
    mainPanel(
     tabsetPanel(
     tabPanel("Plot", 
       plotOutput("plot_off") 
     ) 
    ) 
    ) 
)) 

謝謝您的任何輸入

+0

從我的研究,我想,也許我需要一個反應的表達,而不是觀察。將它們交換不起作用。 –

+0

你想每月有不同線條的情節嗎? – aosmith

+0

是的,我想要選擇哪些線是和不在圖上。 –

回答

0

錯誤來自dcast_off[[monthBy_off]]只能使用一個值,而input$month包含一個向量因爲multiple=TRUE允許選擇多個月。

您必須使用以下語法:trick_off <- dcast_off[,monthBy_off]刪除該錯誤。

但是,那麼你將不得不修復繪圖錯誤,例如通過繪製每個月不同的顏色。對於這一點,你最好使用長data.frame(在dcast()前):

server <- shinyServer(function(input, output, session) { 
     observe({ 

       trick_off <- dat_off[dat_off$variable %in% input$month,] 
       output$plot_off <- renderPlot({ 
         O <-ggplot(data = trick_off, aes(x=time_,y=value, color=variable)) + 

           geom_line(size = 2, alpha = 0.75) + 
           geom_point(size =3, alpha = 0.75) + 

           ggtitle("Boarding the Bus Ridership December 2015") + 
           labs(x="Time",y="Count")+ 
           theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=32, hjust=0.5)) + 
           theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=22))+ 
           theme_classic() 
         O 
       }) 

     }) 

}) 

enter image description here

+0

這太棒了。因此,輸入$ month從UI獲取輸入並選擇所有對應於輸入的'dat_off $ variable'行。如果它有列是數據框,那麼'data = trick_off'起作用,那麼你可以分配x,y和色彩列?那麼不同的月份是如何分開存儲的?對不起所有的問題,我只是想了解當你編寫'color = variable'時你如何工作/ –

+0

你告訴'ggplot()'有幾個系列。 – HubertL