Shinyapp works locally, but not when deployed to shinyapps.io

246 views
Skip to first unread message

karenbl...@koverse.com

unread,
Jun 27, 2018, 12:52:21 PM6/27/18
to shinyapps.io Users
Hi.  I'm having trouble with my shinyapp when deployed to shinyapps.io.  You can find the app at https://karenblakemore.shinyapps.io/arules-shinyapp/.   

It works great, locally, but not on shinyapps.io.  You should be able to click on the 'Load Data Set' button and immediately see a bar chart under the 'ItemFreq' tab.  Instead, the bar chart is only shown after several clicks of the tabs along the top, then clicking 'ItemFreq'.  It seems like this is a timing issue, where click events are being missed.

Once the data set is loaded, the user should be able to change the info along the left hand side, then click the 'Generate Association Rules' button.  Then, click on one of the tabs along the top, say 'Scatter', which will pop-up a new tab with an interactive scatter plot.

There was one warning during the deployment.  Perhaps this is related to the failure:

Application successfully deployed to https://karenblakemore.shinyapps.io/arules-shinyapp/

Warning message:

In doTryCatch(return(expr), name, parentenv, handler) :

  unable to load shared object '/Library/Frameworks/R.framework/Resources/modules//R_X11.so':

  dlopen(/Library/Frameworks/R.framework/Resources/modules//R_X11.so, 6): Library not loaded: /opt/X11/lib/libSM.6.dylib

  Referenced from: /Library/Frameworks/R.framework/Resources/modules//R_X11.so

  Reason: image not found


Here is the ui.R file:

library(shiny)

vars <- 5
supp <- 0.1
conf <- 0.5

shinyUI(pageWithSidebar(
  headerPanel('Association Rules'),
  sidebarPanel(
    selectInput('dataset', 'Data Set:', c('titanic' = 'titanic',
                'hospital_readmission' = 'hospital_readmission',
                'PYDATAV3' = 'PYDATAV3')),
    actionButton('load', 'Load Data Set'), br(), br(),

    conditionalPanel(
      condition = "input.samp=='Sample'",
      numericInput("nrule", 'Number of Rules', 5), br()
    ),

    conditionalPanel(
      condition = "input.lhsv=='Subset'",
      uiOutput("choose_lhs"), br()
    ),

    conditionalPanel(
      condition = "input.rhsv=='Subset'",
      uiOutput("choose_rhs"), br()
    ),

    conditionalPanel(
      condition = "input.mytab=='grouped'",
      sliderInput('k', label='Choose # of rule clusters', min=1, max=150, step=1, value=15), br()
    ),

    conditionalPanel(
      condition = "input.mytab %in%' c('scatter', 'twoKeyScatter', 'grouped', 'graph', 'paracoord', 'matrix', 'threeDMatrix', 'datatable')",
      radioButtons('samp', label='Sample', choices=c('All Rules', 'Sample'), inline=T), br(),
      uiOutput("choose_columns"), br(),
      sliderInput("supp", "Support:", min = 0, max = 1, value = supp , step = 1/10000), br(),
      sliderInput("conf", "Confidence:", min = 0, max = 1, value = conf , step = 1/10000), br(),
      selectInput('sort', label='Sorting Criteria:', choices = c('lift', 'confidence', 'support')), br(), br(),
      numericInput("minL", "Min. items per set:", 2), br(),
      numericInput("maxL", "Max. items per set::", 3), br(),
      radioButtons('lhsv', label='LHS variables', choices=c('All', 'Subset')), br(),
      radioButtons('rhsv', label='RHS variables', choices=c('All', 'Subset')), br(),

      actionButton('generate', 'Generate Association Rules'), br(), br(), br()
#      downloadButton('downloadData', 'Download Rules as CSV')
    )
  ),

  mainPanel(
    tabsetPanel(id='mytab',
                tabPanel('ItemFreq', value='itemFreq', plotOutput("itemFreqPlot", width='100%', height='100%')),
                tabPanel('Scatter', value='scatter', plotOutput("scatterPlot", width='100%', height='100%')),
                tabPanel('Two-Key Scatter', value='twoKeyScatter', plotOutput("twoKeyScatterPlot", width='100%', height='100%')),
                tabPanel('Grouped', value='grouped', plotOutput("groupedPlot", width='100%', height='100%')),
                tabPanel('Graph', value='graph', plotOutput("graphPlot", width='100%', height='100%')),
                tabPanel('Parallel Coordinates', value='paracoord', plotOutput("paracoordPlot", width='100%', height='100%')),
                tabPanel('Matrix', value='matrix', plotOutput("matrixPlot", width='100%', height='100%')),
                tabPanel('3D Matrix', value='threeDMatrix', plotOutput("threeDMatrixPlot", width='100%', height='100%'))
#                tabPanel('Data Table', value='datatable', dataTableOutput("rulesDataTable"))
    )
  )
))

The server.R file. 

library(shiny)
library(data.table)
library(arules)
library(arulesViz)

shinyServer(function(input, output) {

  cat(file=stderr(), 'Entered server\n')

  dataset <- eventReactive(input$load, {
    cat(file=stderr(), 'Entered load Reactive\n')
    filename <- paste('./data/', input$dataset, '_features.csv', sep='')
    data <- read.csv(filename, na.strings=c(''), check.names=FALSE)
    cat(file=stderr(), 'Loaded features file ', input$dataset, '\n')
    data[] <- lapply(data, factor)  # all columns must be of type factor
    data
  })

  output$choose_columns <- renderUI({
    checkboxGroupInput("cols", "Choose variables:", choices  = colnames(dataset()), selected = colnames(dataset())[1:5])
  })

  output$choose_lhs <- renderUI({
    checkboxGroupInput("colsLHS", "Choose LHS variables:", choices  = input$cols, selected = input$cols[1])
  })

  output$choose_rhs <- renderUI({
    checkboxGroupInput("colsRHS", "Choose RHS variables:", choices  = input$cols, selected = input$cols[1])
  })

  rules <- eventReactive(input$generate, {
    filename <- paste('data/', input$dataset, '_features.csv', sep='')
    ds <- read.csv(filename, na.strings=c(''), check.names=FALSE)
    ds[] <- lapply(ds, factor)  # all columns must be of type factor
    tr <- as(ds, 'transactions')

    arAll <- apriori(tr, parameter=list(support=input$supp, confidence=input$conf, minlen=input$minL, maxlen=input$maxL))

    if(input$rhsv=='Subset' & input$lhsv!='Subset'){
      varsR <- character()
      for(i in 1:length(input$colsRHS)){
        tmp <- with(ds, paste(input$colsRHS[i], '=', levels(as.factor(get(input$colsRHS[i]))), sep=''))
        varsR <- c(varsR, tmp)
      }
      ar <- subset(arAll, subset=rhs %in% varsR)

    } else if(input$lhsv=='Subset' & input$rhsv!='Subset') {
      varsL <- character()
      for(i in 1:length(input$colsLHS)){
        tmp <- with(ds, paste(input$colsLHS[i], '=', levels(as.factor(get(input$colsLHS[i]))), sep=''))
        varsL <- c(varsL, tmp)
      }
      ar <- subset(arAll, subset=lhs %in% varsL)

    } else if(input$lhsv=='Subset' & input$rhsv=='Subset') {
      varsL <- character()
      for(i in 1:length(input$colsLHS)){
        tmp <- with(ds, paste(input$colsLHS[i], '=', levels(as.factor(get(input$colsLHS[i]))), sep=''))
        varsL <- c(varsL, tmp)
      }
      varsR <- character()
      for(i in 1:length(input$colsRHS)){
        tmp <- with(ds, paste(input$colsRHS[i], '=', levels(as.factor(get(input$colsRHS[i]))), sep=''))
        varsR <- c(varsR, tmp)
      }
      ar <- subset(arAll, subset=lhs %in% varsL & rhs %in% varsR)

    } else {
      ar <- arAll
    }

    # Rule length
    nR <- ifelse(input$samp == 'All Rules', length(ar), input$nrule)
    ar <- sort(ar, by=input$sort)[1:nR]
  })

  ## Item Frequency Plot ##########################
  output$itemFreqPlot <- renderPlot({
    trans <- as(dataset(), 'transactions')
    itemFrequencyPlot(trans, support=.25, cex.names=0.8)
  }, height=800, width=800)

  ## Scatter Plot ##########################
  output$scatterPlot <- renderPlot({
    plot(rules(), method='scatterplot', engine='htmlwidget')
  }, height=800, width=800)

  ## Two-Key Scatter Plot ##########################
  output$twoKeyScatterPlot <- renderPlot({
    plot(rules(), method='two-key plot', engine='htmlwidget')
  }, height=800, width=800)

  ## Grouped Plot #########################
  output$groupedPlot <- renderPlot({
    plot(rules(), method='grouped', control=list(k=input$k))
  }, height=800, width=800)

  ## Graph Plot ##########################
  output$graphPlot <- renderPlot({
    plot(rules(), method='graph', engine='htmlwidget')
  }, height=800, width=800)

  ## Parallel Coordinates Plot ###################
  output$paracoordPlot <- renderPlot({
    plot(rules(), method='paracoord')
  }, height=800, width=800)

  ## Matrix Plot ###################
  output$matrixPlot <- renderPlot({
    plot(rules(), method='matrix', engine='htmlwidget', shading=c('lift'))
  }, height=800, width=800)

  ## 3D Matrix Plot ###################
  output$threeDMatrixPlot <- renderPlot({
    plot(rules(), method='matrix', engine='3d', shading=c('lift'))
  }, height=800, width=800)

  ## Rules Data Table ##########################
#  output$rulesDataTable <- renderDataTable({
#    p <- inspectDT(rules())
#    htmlwidgets::saveWidget(p, "rules.html", selfcontained = FALSE)
#    browseURL("rules.html")
#  })

  ## Download data to csv ########################
#  output$downloadData <- downloadHandler(
#    filename = 'arules_data.csv',
#    content = function(file) {
#      write.table(as(rules(), 'data.frame'), file, row.names=FALSE, sep=',')
#    }
#  )
})

The PYDATAV3_features.csv:

a,b,c

11,14,18

10,13,16

12,12,17

12,14,18

11,14,18

11,12,16

11,14,18

11,12,16

12,12,17

11,12,16

12,12,17

12,13,18

11,13,17

11,13,17

10,12,16

11,12,16

10,13,16

10,12,16

11,14,18

12,13,18

11,12,16

11,14,18

11,13,17

11,14,18

12,13,18

10,14,17

12,12,17

10,12,16

12,13,18

12,12,17

10,13,16

12,14,18

11,14,18

10,12,16

10,12,16

10,14,17

11,12,16

10,12,16

12,14,18

12,13,18

11,14,18

12,13,18

10,13,16

11,14,18

11,12,16

12,13,18

10,14,17

12,13,18

11,12,16

10,14,17

11,13,17

11,13,17

12,14,18

10,12,16

10,13,16

12,13,18

12,12,17

12,14,18

12,13,18

10,13,16

11,12,16

11,12,16

11,13,17

10,12,16

12,14,18

10,14,17

12,12,17

10,13,16

11,14,18

10,14,17

12,13,18

11,14,18

10,12,16

Joshua Spiewak

unread,
Jun 27, 2018, 1:38:38 PM6/27/18
to shinyapps.io Users
There are a great many Javascript console errors that you should investigate.

karenbl...@koverse.com

unread,
Jun 27, 2018, 1:49:31 PM6/27/18
to shinyapps.io Users
Thanks for taking a look, Joshua.  Are the console errors in the Logs that can be downloaded from the application dashboard?  These all look okay to me.  I also see these when I run the app locally.

Joshua Spiewak

unread,
Jun 27, 2018, 1:52:40 PM6/27/18
to shinyapps.io Users
They are Javascript console error messages, so accessing that depends upon what browser you are using.

karenbl...@koverse.com

unread,
Jun 27, 2018, 2:04:00 PM6/27/18
to shinyapps.io Users
Oh.  I see the Javascript errors in the developer tools console, now.

karenbl...@koverse.com

unread,
Jun 27, 2018, 4:10:54 PM6/27/18
to shinyapps.io Users
Thanks, Jason.  I fixed the Javascript error and have gotten farther.  

I think my last problem is that arulesViz plots with engine='htmlwidget' do not work.  Run locally, these will 'pop up' a new browser tab and display the visualization there.  When deployed to shinyapps.io, the plot does nothing with no error messages to the console.

Here is an example of the server code that is not working:

  output$scatterPlot <- renderPlot({
    plot(rules(), method='scatterplot', engine='htmlwidget')
  }, height=800, width=800)

Any ideas? 
Reply all
Reply to author
Forward
0 new messages