none
Jupyter Notebook for R just doesn't seem to work when try to do ggplot

    Question

  • It looks like Jupyter Notebook for R has problems running ggplot. It either kills the kernel or hangs with error messages.

    I was trying to run the Diabetes example in Microsoft -  DAT203.2x, Principles of Machine Learning course. The following is the code snippet. It used to work earlier in the year. Please help. Thanks in advance.

    --------------------------------------------------

    library("AzureML")
    ws <- workspace('ae24c20fadf145f58c9547b6ee5591a9','bb03457a9f5b49e9b422d0ac6cc08e7d')
    dat <- download.intermediate.dataset(
      ws,
      experiment = "ae24c20fadf145f58c9547b6ee5591a9.f-id.e2f0bd4800b5456cb67392278badec26",
      node_id = "e52e24d8-b4b0-41b5-8a51-9517829fd29f-195",
      port_name = "Results dataset",
      data_type_id = "GenericCSV"
    )
    head(dat)

    bar.plot <- function(x, cut = 200){
      require(ggplot2)
      if(is.factor(diabetes[, x]) | is.character(diabetes[, x]) & (x != 'readmitted')){
        colList = c('readmitted', x)
        diabetes[, colList] = lapply(diabetes[, colList], as.factor)
        sums <- summary(diabetes[, x], counts = n())
        msk <- names(sums[which(sums > cut)])
        tmp <- diabetes[diabetes[, x] %in% msk, colList]
        capture.output(
          if(strsplit(x, '[-]')[[1]][1] == x){
            g <- ggplot(tmp, aes_string(x)) +
              geom_bar() +
              facet_grid(. ~ readmitted) +
              ggtitle(paste('Readmissions by level of', x))
            print(g) 
          } 
        )    
      } 
    }


    violin.plot <- function(x){
      if(is.numeric(diabetes[, x])){
        ggplot(diabetes, aes_string('readmitted', x)) +
          geom_violin() +
          ggtitle(paste('Readmissions by', x))
      }
    }


    box.plot <- function(x){
      if(is.numeric(diabetes[, x])){
        ggplot(diabetes, aes_string('readmitted', x)) +
          geom_boxplot() +
          ggtitle(paste('Readmissions by', x))
      }
    }

    hist.plot <- function(x){
      if(is.numeric(diabetes[, x])){
        capture.output(
          ggplot(diabetes, aes_string(x)) +
            geom_histogram() +
            facet_grid(readmitted ~ .) +
            ggtitle(paste('Readmissions by', x))
        )

     }
    }
    diabetes <- dat 
    col.names = names(diabetes)
    col.names = c(col.names, names(diabetes))
    lapply(col.names, bar.plot) 
    lapply(col.names, box.plot) 
    lapply(col.names, hist.plot)

    Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): there is no package called ‘stringr’
    Traceback:

    1. lapply(col.names, bar.plot)
    2. FUN(X[[i]], ...)
    3. capture.output(if (strsplit(x, "[-]")[[1]][1] == x) {
     .     g <- ggplot(tmp, aes_string(x)) + geom_bar() + facet_grid(. ~ 
     .         readmitted) + ggtitle(paste("Readmissions by level of", 
     .         x))
     .     print(g)
     . })   # at line 9-17 of file <text>
    4. evalVis(expr)
    5. withVisible(eval(expr, pf))
    6. eval(expr, pf)
    7. eval(expr, pf)
    8. print(g)
    9. print.ggplot(g)
    10. ggplot_build(x)
    11. layout$setup(layer_data, plot$data, plot$plot_env, plot$coordinates)
    12. f(..., self = self)
    13. self$facet$train(data)
    14. f(..., self = self)
    15. self$compute_layout(data, self$params)
    16. f(...)
    17. reshape2::add_margins
    18. getExportedValue(pkg, name)
    19. asNamespace(ns)
    20. getNamespace(ns)
    21. tryCatch(loadNamespace(name), error = function(e) stop(e))
    22. tryCatchList(expr, classes, parentenv, handlers)
    23. tryCatchOne(expr, names, parentenv, handlers[[1L]])
    24. value[[3L]](cond)



    In [4]:


    FL

    Monday, December 17, 2018 12:18 AM

All replies

  • Hi, 

    I took some sample code from this website to check for ggplot: https://r4stats.com/examples/graphics-ggplot2/

    I seem to be able to plot using ggplot as shown below, so it does seem to work within the Jupyter notebook.  

    Regards,
    Jaya

    Monday, December 17, 2018 5:08 AM
  • Have you tried the  Diabetes example in Microsoft -  DAT203.2x, Principles of Machine Learning course? It does not work for me. It used to work about a year ago. Thanks.

    FL

    Monday, December 17, 2018 5:36 AM
  • Hi, 

    I do not have access to the Diabetes dataset, hence tried a simple ggplot example. Perhaps, something in the code must be failing, pls check the code line by line to check for where it fails. 

    Regards,
    Jaya

    Tuesday, December 18, 2018 3:36 AM
  • Hi Jaya,

    I got the same error message using both edge and chrome. I was able to run the same code in R studio and generate plots. Error code is below. Thanks. Fred

    Loading required package: ggplot2
    
    Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): there is no package called ‘stringr’
    Traceback:
    
    1. lapply(col.names, bar.plot)
    2. FUN(X[[i]], ...)
    3. capture.output(if (strsplit(x, "[-]")[[1]][1] == x) {
     .     g <- ggplot(tmp, aes_string(x)) + geom_bar() + facet_grid(. ~ 
     .         readmitted) + ggtitle(paste("Readmissions by level of", 
     .         x))
     .     print(g)
     . })   # at line 9-17 of file <text>
    4. evalVis(expr)
    5. withVisible(eval(expr, pf))
    6. eval(expr, pf)
    7. eval(expr, pf)
    8. print(g)
    9. print.ggplot(g)
    10. ggplot_build(x)
    11. layout$setup(layer_data, plot$data, plot$plot_env, plot$coordinates)
    12. f(..., self = self)
    13. self$facet$train(data)
    14. f(..., self = self)
    15. self$compute_layout(data, self$params)
    16. f(...)
    17. reshape2::add_margins
    18. getExportedValue(pkg, name)
    19. asNamespace(ns)
    20. getNamespace(ns)
    21. tryCatch(loadNamespace(name), error = function(e) stop(e))
    22. tryCatchList(expr, classes, parentenv, handlers)
    23. tryCatchOne(expr, names, parentenv, handlers[[1L]])
    24. value[[3L]](cond)
    


    FL

    Tuesday, December 18, 2018 5:36 AM
  • Help !

    FL

    Saturday, January 5, 2019 5:09 AM