1

hola I'm stuck on something. I am creating a ggplot2 Shiny app, where the user can select the amount of variables that are displayed in the line chart. I used the argument Multiple for selectInput, although the app crashed when more than one variables are selected, getting the error:

Warning: Error in .subset2: subscript out of bounds

Here is the data

server:

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

Thank you for any input

3
  • From my research, I think that maybe I need a reactive expression instead of observe. Swapping them out didn't work. Commented Jun 14, 2016 at 19:51
  • Do you want a plot with different lines per month? Commented Jun 14, 2016 at 20:16
  • yes, I want to choose which lines are and are not on the graph. Commented Jun 14, 2016 at 20:24

1 Answer 1

2

The error comes from dcast_off[[monthBy_off]] which only works with one single value while input$month contains a vector of charater because of multiple=TRUE that allows selecting more than one month.

You'd have to use this syntax : trick_off <- dcast_off[,monthBy_off] to remove the error.

But then you'll have to fix the plotting error, for example by plotting each month in a different color. For this, you'd better use the long data.frame (before the 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

Sign up to request clarification or add additional context in comments.

2 Comments

That's awesome by the way. So, the input$month gets the input from the UI and selects the rows of the dat_off$variable that all correspond to the input. If it has columns it's a dataframe, so data = trick_off works, and there you can assign x, y and colour column wise? So how are the different months separated and stored, if not by the group argument? Sorry for all the questions, I'm just trying to understand how some of this works/
when you write color=variable you tell ggplot() there are several series.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.