Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r2d3 visualization working on Windows, not on Mac #71

Closed
delosh653 opened this issue Jul 19, 2019 · 5 comments
Closed

r2d3 visualization working on Windows, not on Mac #71

delosh653 opened this issue Jul 19, 2019 · 5 comments

Comments

@delosh653
Copy link

Hi, I've run into an issue that r2d3 seems to be acting quite differently on Windows versus Macs. I am trying to render a specific set of r2d3 visualizations in a Shiny app, one of them being a Sankey diagram (in my example code, attached, this is particularly simple, with 1 link and 2 nodes).

On Windows, this visualization appears perfectly in Shiny in an external window (Firefox/Chrome):
image
However, when running the same Shiny app on a Mac, nothing appears. Looking at the developer console, the error "TypeError: data.charCodeAt is not a function" appears, which seems to be from the r2d3-render.js script.

Is there any reason why this would be the case, and how would I fix it? The R code can be found below, and supporting JavaScript files can be found here.

r2d3 version: 0.2.3
Windows 10, Firefox v68.0, Chrome v75.0.3770.142
macOS Sierra v10.12.6, Firefox v67.0.2

#https://stackoverflow.com/questions/3452086/getting-path-of-an-r-script/35842176#35842176
# set working directory - only works in RStudio (with rstudioapi)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

library(r2d3)
library(shiny)

data_to_json <- function(data) {
  jsonlite::toJSON(data, dataframe = "rows", auto_unbox = FALSE, rownames = TRUE, digits = NA)
}

all_sig <- data.frame("name" = c("biol_process", "cell_proc"),
                      "go_name" = c("go1","go2"),
                      "sig" = c(1,.0303),
                      "is_sig"=c(F,T),
                      stringsAsFactors = F)
dat_sankey <- list(
  "nodes" = data.frame("name"=c("biol_process", "cell_proc"), stringsAsFactors = F),
  "links" = data.frame("source"=0,"target"=1,"value"=1)
)
rownames(dat_sankey$links) <- "biol_process"
sig_color <- "#B354B7"
no_sig_color <- "#afb1b5"
font_size <- 16
textcol_bg <- "#000000"
togg_border <- T


ui <- fluidPage(
  d3Output("d3")
)

server <- function(input, output) {
  output$d3 <- renderD3({
    r2d3(data = data_to_json(dat_sankey), script = "js_scripts\\all_tabs.js", 
         d3_version = 5, 
         dependencies = c("js_scripts\\sankey\\d3-sankey.js",
                          "js_scripts\\sankey\\d3-sankey.min.js"),
         options = list("all_sig"=all_sig,
                        "sig_color"=unname(sig_color),
                        "no_sig_color"=no_sig_color,
                        "font_size" = font_size,
                        "which_camp" = "ont_map",
                        textcol_bg = textcol_bg,
                        togg_border = togg_border
         ))
  })
}

shinyApp(ui, server)
@delosh653
Copy link
Author

delosh653 commented Jul 24, 2019

Solution, unfortunately, found. On Macs/Unix-based systems, you can't use the double backslash to refer to file references ("\"). You have to use forward slashes ("/"). So the solution for the above piece of code would be:

 r2d3(data = data_to_json(dat_sankey), script = "js_scripts/all_tabs.js", 
         d3_version = 5, 
         dependencies = c("js_scripts/sankey/d3-sankey.js",
                          "js_scripts/sankey/d3-sankey.min.js"),
         options = list("all_sig"=all_sig,
                        "sig_color"=unname(sig_color),
                        "no_sig_color"=no_sig_color,
                        "font_size" = font_size,
                        "which_camp" = "ont_map",
                        textcol_bg = textcol_bg,
                        togg_border = togg_border
         ))

Closing this issue (though hopefully some woefully unknowing windows user like me finds this helpful!).

@jjallaire
Copy link
Member

Use forward slashes are valid on both Windows & Posix systems so is generally how all path references are composed in R.

@delosh653
Copy link
Author

delosh653 commented Jul 24, 2019

If you're on windows, the above double backslashing reference would work (this is easier to add one backslash when copying references from windows). So this isn't well known for people like me, who work almost exclusively in a windows environment. But good to know for me for the future!

@jjallaire
Copy link
Member

jjallaire commented Jul 24, 2019 via email

@delosh653
Copy link
Author

Wow, great to know about the "file.path" command! Thank you for the tip!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants