d3treeR / d3treeR

R htmlwidget for d3.js treemaps
MIT License
65 stars 24 forks source link

Error when running on Shiny server #19

Open ThoDuyNguyen opened 9 years ago

ThoDuyNguyen commented 9 years ago

ui.R:

library(shiny)
library(DT)
library(treemap)
library(d3treeR)
library(parsetR)

shinyUI(fluidPage(

    # Application title
    titlePanel("Test"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            actionButton("actionButton",label = "Process")
        ),

        # Show a plot of the generated distribution
        mainPanel(
            tabsetPanel(
                tabPanel("Data", DT::dataTableOutput("dataTable")),
                tabPanel("Visualization 1", d3tree2Output("d3Tree", width = "100%", height="600px")),
                tabPanel("Visualization 2", parsetOutput("parset", width = "100%", height = "600px"))
            )
        )
    )
))

server.R:

library(shiny)
library(DT)
library(treemap)
library(d3treeR)
library(dplyr)
library(parsetR)
library(htmlwidgets)

shinyServer(function(input, output) {
    groupData <-  eventReactive(input$actionButton,{
        group <-
            structure(
                list(
                    location = structure(
                        c(
                            1L, 1L, 1L, 2L, 2L, 3L,
                            3L, 4L, 5L, 5L, 6L, 6L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L,
                            11L, 11L, 11L, 11L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L, 15L,
                            16L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 18L
                        ), .Label = c(
                            "ATC",
                            "CN-DAINAM", "CN-GIALAI", "CN-LIENDANH", "CN-LONGAN", "CN-NGHEAN",
                            "DC-BACGIANG", "DC-BENTRE", "DC-CONGTHANH", "DC-DAINAM", "DC-DATLOC",
                            "DC-HAIDUONG", "DC-HUNGYEN", "DC-SSMOI", "DC-THIENNAM", "HN-BRANCH",
                            "HP-BRANCH", "SM-THAIBINH"
                        ), class = "factor"
                    ), level = structure(
                        c(
                            1L,
                            2L, 3L, 1L, 2L, 1L, 2L, 6L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 3L, 1L,
                            2L, 3L, 1L, 2L, 1L, 2L, 3L, 4L, 1L, 2L, 1L, 2L, 3L, 4L, 1L, 2L,
                            1L, 1L, 2L, 3L, 6L, 1L, 2L, 3L, 3L, 4L, 5L, 6L
                        ), .Label = c("1",
                                      "2", "3", "4", "5", "6"), class = "factor"
                    ), amount = c(
                        13L,
                        62L, 20L, 29L, 81L, 3L, 10L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 9L,
                        2L, 10L, 3L, 2L, 21L, 27L, 26L, 355L, 7L, 1L, 6L, 11L, 4L, 5L,
                        6L, 3L, 8L, 34L, 1L, 44L, 135L, 1L, 2L, 13L, 9L, 2L, 5L, 5L,
                        4L, 4L
                    )
                ), class = "data.frame", row.names = c(NA,-45L), .Names = c("location",
                                                                            "level", "amount")
            )
        return(group)
    })

    output$dataTable <- DT::renderDataTable(
        datatable(
            groupData(),filter = "top", rownames = FALSE,
            extensions = 'ColVis',
            options = list(
                dom = 'C<"clear">lfrtip', autoWidth = TRUE,
                lengthMenu = list(c(50, 100,-1), c('50', '100','All'))
            )
        ) %>%
            formatStyle(c('level'),
                        backgroundColor = styleInterval(
                            c(1,2,3,4,5,6),
                            c('brown','red',"lime","pink",
                              "yellow","cyan", "purple")
                        ))
    )

    output$d3Tree <- renderD3tree2(d3tree2(
        treemap(
            groupData(),
            index = c("location", "level"),
            vSize = "amount",
            vColor = "amount",
            type = "value"
        )
        ,rootname = "lohoi"
    ))

    output$parset <- renderParset(parset(
        as.data.frame(groupData()), dimensions = c("level", "location"),
        value = htmlwidgets::JS("function(d){return d.amount}"),
        tension = 0.5
    ))
})

The app runs fine on RStudio.

But when I deployed on Shiny server, it raised an error: Log on shiny server:

Listening on http://127.0.0.1:53770

Attaching package: ‘DT’

The following objects are masked from ‘package:shiny’:

    dataTableOutput, renderDataTable

Note: the specification for S3 class “AsIs” in package ‘jsonlite’ seems equivalent to one from package ‘RJSONIO’: not turning on duplicate class definitions for this class.
Loading required package: vcdExtra
Loading required package: vcd
Loading required package: grid
Loading required package: gnm

Attaching package: ‘dplyr’

The following object is masked from ‘package:vcdExtra’:

    summarise

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

Error in (function (file = ifelse(onefile, "Rplots.pdf", "Rplot%03d.pdf"),  :
  cannot open file 'Rplots.pdf'

Therefore, the tab which contains d3tree2Output could not render the plot with message:

Error: cannot open file 'Rplots.pdf'

The others tabs (DT and parsetR) were rendered correctly (though parsetR had problem stated here: https://github.com/timelyportfolio/parsetR/issues/2) R on the server is 3.2.1, all the packages using in the example are up to date.

Please let me know how to fix it.

Kind ragards

timelyportfolio commented 9 years ago

I am not sure I can help a whole lot here, since I have no experience with Shiny Server. One thought, but I doubt it will help. Can you add draw = FALSE to treemap()?

This Stack Overflow post appears related. Specifying a aspRatio for treemap seems to be the solution based on my reading of the answers, since there is no way to specify height and width in treemap. Also, there is this Google Groups-shiny but there is no answer or resolution there.

Maybe some background information might help also. d3treeR uses treemap to create the tree structure, and due to the way treemap is designed treemap must draw a plot. So a R graphics device is required, and it seems in your example R/Shiny server is choosing a default pdf graphics device.

Sorry to fumble around. Let me know if any of the above works.

ThoDuyNguyen commented 9 years ago

I will get the feedback as soons as I back to my server.

Kind regards

ThoDuyNguyen commented 9 years ago

@timelyportfolio: I update the new code, if I put draw = FALSE as parameter of treeMap(), there was "unused parameter".

So i use the original code (also test with all d3Tree render methods)

session_info in Shiny Server

list(
    version = "R version 3.2.1 (2015-06-18)", system = "x86_64, linux-gnu", ui = "X11", language = "(EN)", collate = "en_US.UTF-8", tz = NA
) list(
    package = c(
        "assertthat", "chron", "colorspace", "curl", "d3treeR", "data.table", "data.tree", "DBI", "devtools", "digest", "dplyr", "DT", "ggplot2", "git2r", "gnm", "gridBase", "gridSVG", "gtable", "htmltools", "htmlwidgets", "httpuv", "igraph", "jsonlite", "lattice", "lazyeval", "lmtest", "magrittr", "MASS", "Matrix", "memoise", "mime", "munsell", "nnet", "parsetR", "plyr", "proto", "qvcalc", "R6", "RColorBrewer", "Rcpp", "relimp", "reshape2", "RJSONIO", "RPostgreSQL", "rversions", "scales", "shiny", "stringi", "stringr", "treemap", "vcd", "vcdExtra", "XML", "xml2", "xtable", "yaml", "zoo"
    ), `*` = c(
        "", "", "", "", "*", "", "", "*", "*", "", "*", "*", "", "", "*", "", "", "", "", "*", "", "", "", "", "", "", "", "", "", "", "", "", "", "*", "", "", "", "", "", "", "", "", "", "*", "", "", "*", "", "", "*", "*", "*", "", "", "", "", ""
    ), version = c(
        "0.1", "2.3-47", "1.2-6", "0.9.1", "0.1", "1.9.6", "0.2.2", "0.3.1", "1.8.0", "0.6.8", "0.4.3", "0.1", "1.0.1", "0.10.1", "1.0-8", "0.4-7", "1.4-3", "0.1.2", "0.2.6", "0.5", "1.3.3", "1.0.1", "0.9.17", "0.20-31", "0.1.10", "0.9-34", "1.5", "7.3-40", "1.2-1", "0.2.1", "0.4", "0.4.2", "7.3-9", "0.0.1", "1.8.3", "0.3-10", "0.8-9", "2.1.1", "1.1-2", "0.12.1", "1.0-4", "1.4.1", "1.3-0", "0.4", "1.0.2", "0.3.0", "0.12.2", "0.5-5", "1.0.0", "2.3", "1.4-1", "0.6-11", "3.98-1.3", "0.1.1", "1.7-4", "2.1.13", "1.7-12"
    ), date = c(
        "2013-12-06", "2015-06-24", "2015-03-11", "2015-07-04", "2015-10-11", "2015-09-19", "2015-10-11", "2014-09-24", "2015-05-09", "2014-12-31", "2015-09-01", "2015-06-09", "2015-03-17", "2015-05-07", "2015-04-22", "2014-02-24", "2015-01-27", "2012-12-05", "2014-09-08", "2015-06-21", "2015-08-04", "2015-06-26", "2015-09-06", "2015-03-30", "2015-01-02", "2015-06-06", "2014-11-22", "2015-03-21", "2015-06-01", "2014-04-22", "2015-09-03", "2013-07-11", "2015-02-11", "2015-10-11", "2015-06-12", "2012-12-22", "2015-02-03", "2015-08-19", "2014-12-07", "2015-09-10", "2015-02-03", "2014-12-06", "2014-07-28", "2013-03-27", "2015-07-13", "2015-08-25", "2015-08-05", "2015-06-29", "2015-04-30", "2015-06-26", "2015-07-08", "2015-09-17", "2015-06-30", "2015-06-02", "2014-09-12", "2014-06-12", "2015-03-16"
    ), source = c(
        "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "Github (timelyportfolio/d3treeR@8ad4ed3)", "CRAN (R 3.2.1)", "Github (gluc/data.tree@18b31b0)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "Github (timelyportfolio/parsetR@8cd92d0)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.2.1)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)", "CRAN (R 3.1.3)"
    )
)

I also notice that if a level has more than 6 values then the parset only has 6 colors and use black for the rest of values. As we are recommended not to use more than 6 color/shape in a chart so I don't think it's a bug but it would be nice if the parset support more color for more than 6 values of a level.

Kind regards

timelyportfolio commented 9 years ago

The parameter draw=FALSE requires the Github version of treemap. One possibly better way to handle this might be to create the treemap separately with code something like this.

output$d3Tree <- renderD3tree2({
  tf <- tempfile()
  png(tf, height = 400, width=600) #you'll need to be specific with height and width
  tm <- treemap(
    groupData(),
    index = c("location", "level"),
    vSize = "amount",
    vColor = "amount",
    type = "value"
  )
  dev.off()
  d3tree2(tm,rootname = "lohoi")
})

I'll start a separate issue with parsetR to handle more than 6 levels.

Thanks.

ThoDuyNguyen commented 9 years ago

I'll let you know when I have the result.

daattali commented 8 years ago

Any answer? I'm getting the same "cannot open file 'Rplots.pdf'" error only on shiny server, but it's not using this package. I'm assuming it's a similar cause though

bmb000 commented 8 years ago

I was getting this error too and it was indeed fixed by finding a folder on the server that I definitely had permission to write to and running:

output$treeMapPlot <- renderD3tree2({

    data(GNI2010)
    tf <- "/my_temp_file_location"
    png(tf, height = 400, width=600) #you'll need to be specific with height and width

    tm <-  treemap(
      GNI2010,
      index=c("continent", "iso3"),
      vSize="population",
      vColor="GNI",
      type="value",
      draw = FALSE
    )
    dev.off()

    d3tree2( tm,rootname = "World" )
})

It would be nice though if there were a way to run treemap without generating an output that has to be saved to a temporary location. Thank you though! We are really happy to use this!

fersa commented 7 years ago

I am having the same issue with renderValueBox and renderDygraph. Solved it just adding

pdf(NULL)

as suggested in https://github.com/ropensci/plotly/issues/494

gonzalezivan90 commented 7 years ago

I used chown -R shiny:shiny /srv/shiny-server as https://github.com/ropensci/plotly/issues/494 suggests!

a517dogg commented 7 years ago

I had a similar problem with the treemap in flexdashboard. It was drawing the d3treemap under the regular, static treemap. Fixed with the pdf(NULL) recommended by @fersa.

dla06c commented 4 years ago

I know this is an old topic, but I ran into the same issue with an app that was using a ui/server/global setup. The error may be that a plot or similar object created in the global script can't be used by the app do to issues with permissions.

In my case, I had created some functions in the global file that the app would use to generate new plots based on user inputs. To have something showing when the app first loads, I had run the plot generating functions in global with sample data. All I had to do is move the initial plot function runs from the global to the server script, and everything worked.