Search code examples
rshinygooglevis

reactive switching datasets and subseting for uneven datasets


I have looked in google for examples of people using similar but different datasets with geovis and I have not found my particular problem.

The problem is when switching between datasets, if the dataset that I switch to does not have the same inputs for the geovis, then the application will freeze.

below is a reproducable example when trying to switch between a "Side" or "State" from the united states that is not in both datasets, the geovis gets caught up in what to draw.

Florida works because it is in both datasets, but everything else freezes the app when you switch over.

dataset1<-data.frame(Side=c("East Coast","East Coast","West Coast"),
                     State=c("Florida","Virginia","California"),
                     foods=c("oranges","potatoes","Bananas"),
                     time=c("10-01-2013","10-02-2013","10-1-2013"),
                     quantity=c(1,2,3))

dataset2<-data.frame(Side=c("East Coast","East Coast"),
                     State=c("Florida","Maryland"),
                     foods=c("oranges","Carrots"),
                     time=c("10-01-2013","10-02-2013"),
                     quantity=c(1,3))

dataset1$time<-as.Date(dataset1$time,"%m-%d-%Y")
dataset2$time<-as.Date(dataset2$time,"%m-%d-%Y")


library(googleVis)
library(shiny)
library(dplyr)

runApp(
  list(server = function(input, output,session) {
    ###get dataset
    tab2.dataset<-reactive({
      switch(input$tab2dataset,
             "Dataset1" = dataset1,
             "Dataset2" = dataset2)
    })

    ###observe selected dataset and pick appropriate side for tab2
    observe({
      tab2side <- if (is.null(input$tab2dataset)) character(0) else {
        tab2.dataset() %.%
          `$`('Side') %.%
          unique() %.%
          sort()
      }
      stillSelected <- isolate(input$tab2side[input$tab2side %in% tab2side])
      updateSelectInput(session, "tab2side", choices = tab2side,
                        selected = stillSelected)
    })

    ###observe selected side from dataset and return state for tab2
    observe({
      tab2state <- if (is.null(input$tab2side)) character(0) else {
        tab2.dataset() %.%
          filter(Side %in% input$tab2side,
                 is.null(input$tab2side) | Side %in% input$tab2side) %.%
          `$`('State') %.%
          unique() %.%
          sort()
      }
      stillSelected <- isolate(input$tab2state[input$tab2state %in% tab2state])
      updateSelectInput(session, "tab2state", choices = tab2state,
                        selected = stillSelected)
    })

    output$mychart<-renderGvis({

      tab2sub<-tab2.dataset()
      tab2sub<-subset(tab2sub,Side==input$tab2side)
      tab2sub<-subset(tab2sub,State==input$tab2state)
      if(input$tab2dataset=="Dataset1"){  

        tab2sub<-transform(tab2sub, obs_id  = as.integer(interaction(Side,State,foods, 
                                                                     time,drop=TRUE)))
        tab2sub<-tab2sub[!duplicated(tab2sub$obs_id),]

      }
      if(input$tab2dataset=="Dataset2"){
        tab2sub<-transform(tab2sub, obs_id  = as.integer(interaction(Side,State,foods, 
                                                                     time,drop=TRUE)))
        tab2sub<-tab2sub[!duplicated(tab2sub$obs_id),]
      } 
      tab2sub<-unique(tab2sub)


      if(is.null(tab2sub) | nrow(tab2sub)==0){

      }else{

        myStateSettings <-'
    {"xZoomedDataMin":1199145600000,"colorOption":"2",
    "duration":{"timeUnit":"Y","multiplier":1},"yLambda":1,
    "yAxisOption":"4","sizeOption":"_UNISIZE",
    "iconKeySettings":[],"xLambda":1,"nonSelectedAlpha":0,
    "xZoomedDataMax":1262304000000,"iconType":"LINE",
    "dimensions":{"iconDimensions":["dim0"]},
    "showTrails":false,"uniColorForNonSelected":false,
    "xAxisOption":"_TIME","orderedByX":false,"playDuration":15000,
    "xZoomedIn":false,"time":"2010","yZoomedDataMin":0,
    "yZoomedIn":false,"orderedByY":false,"yZoomedDataMax":100}
    '
        print(tab2sub)
        gvisMotionChart(tab2sub, idvar="foods", timevar="time",options=list(state=myStateSettings))
      }

    })
  }
  , ui =   navbarPage("test",
      tabPanel("bob",
         sidebarPanel(
           selectInput("tab2dataset", "Data:", choices=c("Dataset1","Dataset2"),multiple=FALSE),
           conditionalPanel("input.tab2dataset",selectizeInput("tab2side", "Side:",c("None Selected"=""),multiple=TRUE,options = list(maxItems = 1))),
           conditionalPanel("tab2side",selectizeInput("tab2state", "State:", c("None selected"=""),multiple=TRUE,options = list(maxItems = 1))),width=3
         ),
         mainPanel(
           htmlOutput("mychart")
         )
      )

  )
  ))

Solution

  • I found the answer through experimentation. I just needed to observe which dataset was being selected and create a title that would be refresh when the new data is selected. This refresh forces the gvis plot to also refresh and display the correct data without freezing up or stopping the program. This is probably a round about way of doing things, but it works for me right now.