在ggplot闪亮应用

时间:2016-06-14 19:30:54

标签: r ggplot2 shiny

hola我被困在某事上。我正在创建一个ggplot2 Shiny应用程序,用户可以在其中选择折线图中显示的变量数量。我使用了参数Multiple for selectInput,虽然应用程序在选择了多个变量时崩溃,但是收到错误:

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")
        )
      )
    )
  ))

感谢您提供任何意见

1 个答案:

答案 0 :(得分:0)

错误来自dcast_off[[monthBy_off]]input$month仅适用于单个值,而multiple=TRUE包含字符向量,因为trick_off <- dcast_off[,monthBy_off]允许选择超过一个月。

您必须使用以下语法:dcast()才能删除错误。

但是,您必须修复绘图错误,例如通过以不同的颜色绘制每个月。为此,您最好使用long data.frame(在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 }) }) }) 之前):

const

enter image description here