Dashboard in shinydashboard

376 views
Skip to first unread message

Rajarshi Bhadra

unread,
Feb 20, 2015, 6:16:53 AM2/20/15
to shiny-...@googlegroups.com
I am using shinydashboard for the first time and it is brilliant. However I am stuck at an odd problem. I have the following code which is running on my browser. However when deployed on shinyapps.io it simply refuses to work.I have provided the code below. The dashboard is intended to do 3 things:  
1. Visualize dependent variable  
2.Automatically mark spikes with date dummies on graph with red vertical lines  
3.See the independent variables and dummy variables selected   


The code is as follows

    ui.R
    library(shiny)
    library(shinydashboard)
    library(dygraphs)
    dashboardPage(
      dashboardHeader(title="Dashboard"),
      dashboardSidebar(
        sidebarMenu(
          menuItem("Dashboard",tabName="dashboard",icon=icon("dashboard"))
        )
      ),
      dashboardBody(
        tabItems(
          tabItem(tabName = "dashboard",
                  fluidRow(
                    column(12,
                           box(title = "Plot Dependant", status = "primary", solidHeader = TRUE,
                               collapsible = TRUE,
                               dygraphOutput("final_plot",width = "100%", height = "300px"),width=8),
                           box(title="Model Specifications",status="warning",solidHeader= TRUE,
                               collapsible= TRUE,
                               uiOutput("mg"),width=4
                           )),
                    column(12,
                           tabBox(title="Independants and Dummies",
                                  tabPanel("Independants",verbatimTextOutput("modelvars")),
                                  tabPanel("Dummies",verbatimTextOutput("modeldummies")),width=8
                           ),
                           box(title = "Inputs", status = "warning", solidHeader = TRUE,
                               collapsible = TRUE,
                               uiOutput("dependant"),
                               uiOutput("independant"),
                               uiOutput("dummies"),
                               sliderInput("spikes","Magnitude of strictness of crtiteria for spike",min=1,max=5,value=3,step=1),
                               sliderInput("dips","Magnitude of strictness of crtiteria for dips",min=1,max=5,value=3,step=1),width=4)
                           
                    ))
                  
          )
          
        )
      ))


    server.R
    
    library(shiny)
    library(stats)
    library(dplyr)
    library(dygraphs)
    
    ##
    library(shinydashboard)
    function(input, output) {
      
      raw_init<-data.frame(wek_end_fri=c("06Jul2012","13Jul2012","20Jul2012","27Jul2012","03Aug2012","06Jul2012","13Jul2012","20Jul2012","27Jul2012","03Aug2012"),
                 Var1=c(468.9,507.1,447.1,477.1,452.6,883113.7,814778.0,780691.2,793416.6,833959.6),
                 Var2=c(538672.6,628451.4,628451.4,628451.4,359115.8,54508.8,56036.1,57481.0,58510.0,59016.7),       
                 MG= c("Cat1","Cat1","Cat1","Cat1","Cat1","Cat1","Cat1","Cat1","Cat1","Cat1","Cat2","Cat2","Cat2","Cat2","Cat2","Cat2","Cat2","Cat2","Cat2","Cat2")
                 )
    
    
      #Select Category
      output$mg<-renderUI({
        selectInput("Category","Select Category",c("Cat1","Cat2"))
      })
      raw_init_filter<-reactive({
        filter(raw_init,MG == input$Category)
      })
      
      #Interpret Date
      raw_init_date<-reactive({
        mutate(raw_init_filter(),wek_end_fri=as.Date(wek_end_fri,"%d%b%Y"))
      })
      
      #Get variable Names
      Variable_list<-reactive({
        colnames(raw_init_date())
      })
      #Get potential dummy list
      Dummy_List<-reactive({
        raw_init_date()$wek_end_fri
      })
      #Load dependant
      output$dependant<-renderUI({
        selectInput("deplist","Select Dependant Variable",Variable_list(),selected="Var1")
      })
      #load independant
      output$independant<-renderUI({
        selectInput("indeplist","Select Independant Variable",Variable_list(),multiple=TRUE)
      })
      #Sepereate out Dependant
      dep<-reactive({
        raw_init_date()[input$deplist]
      })
      
      #Spike detection
      plot_data<-reactive({
        data.frame(Time=raw_init_date()$wek_end_fri,dep())
      })
      plot_data_mut<-reactive({
        f <- plot_data()
        colnames(f)[colnames(f)==input$deplist] <- "Volume"
        f
      })
      dep_vec<-reactive({
        as.vector(plot_data_mut()$Volume)
      })
      #Calculating mean
      dep_mean<-reactive({
        mean(dep_vec())
      })
      dep_sd<-reactive({
        sd(dep_vec())
      })
      transformed_column<-reactive({
        (dep_vec()-dep_mean())/dep_sd()
      })
      detected_index_spike<-reactive({
        which(transformed_column()>input$spikes/2)
      })
      detected_index_trough<-reactive({
        which(transformed_column()<(input$dips/(-2)))
      })
      detected_index<-reactive({
        c(detected_index_spike(),detected_index_trough())
      })
      detected_dates<-reactive({
        raw_init_date()$wek_end_fri[detected_index()]
      })
      
      output$dummies<-renderUI({
        validate(
          need(raw_init, 'Upload Data to see controls and results')
        )
        selectInput("dummies","Suggested Dummy Variable",as.character(Dummy_List()),selected=as.character(detected_dates()),multiple=TRUE)
      })
      indlist<-reactive({
        data.frame(Independant_Variables=input$indeplist)
      })
      output$modelvars<-renderPrint({
        indlist()
      })
      dumlist<-reactive({
        data.frame(Dummies=paste("Dummy_",as.character(format(as.Date(input$dummies,"%Y-%b-%d"),"%d%b%y")),sep=""))
      })
      output$modeldummies<-renderPrint({
        dumlist()
      })
      
      
      
      #-----------------------------------------------------------------------------------------#
      library(xts)
      plot_data_xts<-reactive({
        xts(dep(),order.by=as.Date(raw_init_filter()$wek_end_fri,"%d%b%Y"))
      })
      
      ##
      getDates <- reactive({
        as.character(input$dummies)
      })
      addEvent <- function(x,y) {
        dyEvent(
          dygraph=x,
          date=y,
          "", 
          labelLoc = "bottom",
          color = "red", 
          strokePattern = "dashed")
      }
      basePlot <- reactive({ 
        if (length(getDates()) < 1) {
          dygraph(
            plot_data_xts(),
            main="Initial Visualization and dummy detection") %>%
            dyAxis(
              "y", 
              label = "Volume") %>%
            dyOptions(
              axisLabelColor = "Black",
              digitsAfterDecimal = 2,
              drawGrid = FALSE)
        } else {
          dygraph(
            plot_data_xts(),
            main="Initial Visualization and dummy detection") %>%
            dyAxis(
              "y", 
              label = "Volume") %>%
            dyOptions(
              axisLabelColor = "Black",
              digitsAfterDecimal = 2,
              drawGrid = FALSE) %>%
            dyEvent(
              dygraph=.,
              date=getDates()[1],
              "", 
              labelLoc = "bottom",
              color = "red", 
              strokePattern = "dashed")
        }
      })
      ##
      
      output$final_plot <- renderDygraph({
        
        res <- basePlot()
        more_dates <- getDates()
        if (length(more_dates) < 2) {
          res
        } else {
          Reduce(function(i,z){
            i %>% addEvent(x=.,y=z)
          }, more_dates[-1], init=res)
        }
        
      })
      
      
      
      
      
      
    }






Tareef Kawaf

unread,
Feb 20, 2015, 9:31:43 AM2/20/15
to shiny-...@googlegroups.com
Hi Rajarshi,
Glad you are enjoying shinydashboard..  shinyapps.io issues should really go to the shinyapps-users discussion list ( https://groups.google.com/forum/#!forum/shinyapps-users).  
I would suggest you post the output of shinyapps::showLogs() and look in your javascript console to see if there are any errors there that could provide a clue.

Best,

Tareef

Rajarshi Bhadra

unread,
Feb 20, 2015, 10:09:40 AM2/20/15
to shiny-...@googlegroups.com
Hi Tarif,
I will post future questions in the forum as you mentioned.
Here is the log file you told me to post

Thank you



 
2015-02-20T06:31:01.491269+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.491271+00:00 shinyapps[32588]: the standard browser to display its output.
2015-02-20T06:31:01.491272+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.491273+00:00 shinyapps[32588]: See the googleVis package vignettes for more details,
2015-02-20T06:31:01.491274+00:00 shinyapps[32588]: or visit http://github.com/mages/googleVis.
2015-02-20T06:31:01.491275+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.491276+00:00 shinyapps[32588]: To suppress this message use:
2015-02-20T06:31:01.491277+00:00 shinyapps[32588]: suppressPackageStartupMessages(library(googleVis))
2015-02-20T06:31:01.491278+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.501568+00:00 shinyapps[32588]: Loading required package: xts
2015-02-20T06:31:01.491270+00:00 shinyapps[32588]: Note, the plot method of googleVis will by default use
2015-02-20T06:31:01.523402+00:00 shinyapps[32588]: Attaching package: ‘zoo’
2015-02-20T06:31:01.523403+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.523679+00:00 shinyapps[32588]: The following objects are masked from ‘package:base’:
2015-02-20T06:31:01.523682+00:00 shinyapps[32588]:     as.Date, as.Date.numeric
2015-02-20T06:31:01.523683+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.555928+00:00 shinyapps[32588]: Loading required package: TTR
2015-02-20T06:31:01.523681+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.523400+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.506129+00:00 shinyapps[32588]: Loading required package: zoo
2015-02-20T06:31:01.666423+00:00 shinyapps[32588]: Version 0.4-0 included new data defaults. See ?getSymbols.
2015-02-20T06:31:01.966868+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.966872+00:00 shinyapps[32588]: Attaching package: ‘dplyr’
2015-02-20T06:31:01.966874+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.967150+00:00 shinyapps[32588]: The following objects are masked from ‘package:xts’:
2015-02-20T06:31:01.967152+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.967153+00:00 shinyapps[32588]:     first, last
2015-02-20T06:31:01.967154+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.969166+00:00 shinyapps[32588]: The following object is masked from ‘package:stats’:
2015-02-20T06:31:01.969168+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.969169+00:00 shinyapps[32588]:     filter
2015-02-20T06:31:01.969170+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.974380+00:00 shinyapps[32588]: The following objects are masked from ‘package:base’:
2015-02-20T06:31:01.974382+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.974383+00:00 shinyapps[32588]:     intersect, setdiff, setequal, union
2015-02-20T06:31:01.974384+00:00 shinyapps[32588]: 
2015-02-20T06:31:01.977805+00:00 shinyapps[32588]: Loading required package: KFAS
2015-02-20T06:31:02.049549+00:00 shinyapps[32588]: Error in UseMethod("filter_") : 
2015-02-20T06:31:02.049552+00:00 shinyapps[32588]:   no applicable method for 'filter_' applied to an object of class "NULL"
2015-02-20T06:31:02.050572+00:00 shinyapps[32588]: Error in UseMethod("filter_") : 
2015-02-20T06:31:02.050574+00:00 shinyapps[32588]:   no applicable method for 'filter_' applied to an object of class "NULL"
2015-02-20T06:31:02.055271+00:00 shinyapps[32588]: Error in UseMethod("filter_") : 
2015-02-20T06:31:02.055272+00:00 shinyapps[32588]:   no applicable method for 'filter_' applied to an object of class "NULL"
2015-02-20T06:31:29.436254+00:00 shinyapps[32588]: Error in xts::periodicity(data) : 
2015-02-20T06:31:29.436259+00:00 shinyapps[32588]:   can not calculate periodicity of 1 observation
2015-02-20T06:31:29.673669+00:00 shinyapps[32588]: Warning in mean.default(dep_vec()) :
2015-02-20T06:31:29.673673+00:00 shinyapps[32588]:   argument is not numeric or logical: returning NA
2015-02-20T06:31:31.981842+00:00 shinyapps[32588]: Error in xts::periodicity(data) : 
2015-02-20T06:31:31.981846+00:00 shinyapps[32588]:   can not calculate periodicity of 1 observation
2015-02-20T06:31:34.901129+00:00 shinyapps[32588]: Error in xts::periodicity(data) : 
2015-02-20T06:31:34.901132+00:00 shinyapps[32588]:   can not calculate periodicity of 1 observation

Rajarshi Bhadra

unread,
Feb 20, 2015, 12:53:04 PM2/20/15
to shiny-...@googlegroups.com
Hi Tareef,

Tried that it seems that the xts is getting blank dataframe when in shinyapps.io but not when in console. This seems to be the main problem

Thanks 
Reply all
Reply to author
Forward
0 new messages