反应切换数据集和不均匀数据集的子设置

时间:2014-07-25 22:05:43

标签: r shiny googlevis

我在google中查看过使用类似但不同的geovis数据集的人的例子,我没有找到我的特殊问题。

问题是当在数据集之间切换时,如果我切换到的数据集没有相同的geovis输入,那么应用程序将冻结。

下面的

是一个可重现的例子,当试图在美国的“Side”或“State”之间切换时,两个数据集中都没有,geovis会被吸引到绘制的内容中。

佛罗里达州之所以有效,是因为它存在于两个数据集中,但切换时其他所有内容都会冻结应用程序。

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

  )
  ))

1 个答案:

答案 0 :(得分:0)

我通过实验找到了答案。我只需要观察选择哪个数据集并创建一个在选择新数据时刷新的标题。此刷新强制gvis图也刷新并显示正确的数据,而不会冻结或停止程序。这可能是关于做事的一种方式,但它现在适用于我。