ramnathv / rCharts

Interactive JS Charts from R
http://rcharts.io
Other
1.19k stars 654 forks source link

Can't open nPlot in both RStudio viewer and browser #617

Open Borye opened 9 years ago

Borye commented 9 years ago

Hi, I am trying your example code. But I can't get anything in both Rstudio or browser. The code is:

require(rCharts)
options(viewer = NULL)                    
haireye = as.data.frame(HairEyeColor)
n1 <- nPlot(Freq ~ Hair, group = 'Eye', type = 'multiBarChart', data = subset(haireye, Sex == 'Male'))
n1$save('n1.html', cdn = TRUE)
n1

The reason I add options(viewer = NULL) is because I found a post which said this command can show the plot directly on browser. And it works fine on others computer. But in my laptop it shows nothing. I have tried to browse the n1.html directly from my folder, but still nothing cames up.

My sessionInfo() is as below:

R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rCharts_0.4.5

loaded via a namespace (and not attached):
[1] grid_3.1.3      lattice_0.20-30 plyr_1.8.1      Rcpp_0.11.5     RJSONIO_1.3-0   tools_3.1.3     whisker_0.3-2   yaml_2.1.13 

Please help me with this. It killing me right now. Thank you.

jpastore001 commented 9 years ago

I have the same issue.

joseah commented 9 years ago

@Borye : Does your n1.html file start like this?

<!doctype HTML>
<meta charset = 'utf-8'>
<html>
  <head>

    <script src='//d3js.org/d3.v3.min.js' type='text/javascript'></script>
    <script src='//dimplejs.org/dist/dimple.v2.1.0.min.js' type='text/javascript'></script>

I'm having the same problem trying to save my dimple plot too, although I can see it in RStudio.

dP$save('plot.html', cdn = TRUE)

but it seems that the html file is not being generated in a proper way. Those lines should be

...
 <script src="http://d3js.org/d3.v3.min.js"></script>
  <script src="http://dimplejs.org/dist/dimple.v2.1.2.min.js"></script>
...

Does anybody have the same problem? Here the information of my session:

> options(width = 120)
> devtools::session_info()
Session info -----------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.2.0 (2015-04-16)
 system   x86_64, linux-gnu           
 ui       RStudio (0.98.1103)         
 language en_US                       
 collate  en_US.UTF-8                 
 tz       <NA>                        

Packages ---------------------------------------------------------------------------------------------------------------
 package    * version   date       source                           
 devtools   * 1.7.0     2015-01-17 CRAN (R 3.2.0
)                   
 digest     * 0.6.8     2014-12-31 CRAN (R 3.2.0)                   
 formatR    * 1.2       2015-04-21 CRAN (R 3.2.0)                   
 highr      * 0.5       2015-04-21 CRAN (R 3.2.0)                   
 htmltools  * 0.2.6     2014-09-08 CRAN (R 3.2.0)                   
 knitr        1.10.5    2015-05-06 CRAN (R 3.2.0)                   
 lattice    * 0.20-31   2015-03-30 CRAN (R 3.1.3)                   
 magrittr   * 1.5       2014-11-22 CRAN (R 3.2.0)                   
 plyr       * 1.8.2     2015-04-21 CRAN (R 3.2.0)                   
 rCharts      0.4.5     2015-05-09 Github (ramnathv/rCharts@389e214)
 Rcpp       * 0.11.6    2015-05-01 CRAN (R 3.2.0)                   
 rjson      * 0.2.15    2014-11-03 CRAN (R 3.2.0)                   
 RJSONIO    * 1.3-0     2014-07-28 CRAN (R 3.2.0)                   
 rmarkdown  * 0.6.1     2015-05-07 CRAN (R 3.2.0)                   
 rstudio    * 0.98.1103 2015-05-08 local                            
 rstudioapi * 0.3.1     2015-04-07 CRAN (R 3.2.0)                   
 stringi    * 0.4-1     2014-12-14 CRAN (R 3.2.0)                   
 stringr    * 1.0.0     2015-04-30 CRAN (R 3.2.0)                   
 whisker    * 0.3-2     2013-04-28 CRAN (R 3.2.0)                   
 yaml       * 2.1.13    2014-06-12 CRAN (R 3.2.0)
jpastore001 commented 9 years ago

Mine has this:

<link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/nvd3/1.1.15-beta/nv.d3.min.css'>

<script src='//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'></script>
<script src='//d3js.org/d3.v3.min.js' type='text/javascript'></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/nvd3/1.1.15-beta/nv.d3.min.js' type='text/javascript'></script>
<script src='//nvd3.org/assets/lib/fisheye.js' type='text/javascript'></script>
jpastore001 commented 9 years ago

Even when I manually fix the links, I still cannot view the graph in my html file.

timelyportfolio commented 9 years ago

This is a known issue https://github.com/ramnathv/rCharts/issues/497 for RStudio Viewer + Windows + nvd3. If you open in a browser, you should see it as expected.

timelyportfolio commented 9 years ago

Never mind, it appears RStudio now tries to serve the page, so I think all rCharts will not show up correctly unless you do

n1$show(cdn=T)
timelyportfolio commented 9 years ago

On a separate note, nPlot seems to show up fine now in RStudio Viewer, so you should be able to remove the options(viewer = NULL).

MarcinKosinski commented 9 years ago

n1$show(cdn=t) doesnt'work in Rstudio Viewer, not even when I used options(viewer=NULL) and it does not work in shinydashboard neither but it worked on other computer.... I've posted about that here: http://stackoverflow.com/questions/30213917/multibarchart-nvd3-in-rcharts-is-not-displaying

MarcinKosinski commented 9 years ago

When you save html like this:

n1$save('n1-cdn.html', cdn=TRUE)
n1$save('n1-nocdn.html', cdn=FALSE)

The saved with cdn=FALSE looks like to open in web-browser but the one with TRUE does not open. But how to use cdn=FALSE in shinyapp?

jpastore001 commented 9 years ago

Incidentally, this works for me in both browser and Rstudio.

https://gist.github.com/ramnathv/5531732

Seems like a better working example to point people to.

Quares commented 9 years ago

The issue seems to the header f the html file, if you save it using cdn = TRUE, then the header looks something like this:

...
<script src='//d3js.org/d3.v3.min.js' type='text/javascript'></script>
<script src='//dimplejs.org/dist/dimple.v2.1.0.min.js' type='text/javascript'></script>
...

if you save it using cdn = FALSE it will link to your folder locally, so something like this

<script src='C:/Users/[user]/Documents/R/win-library/3.2/rCharts/libraries/

If you edit the header and set it to

...
 <script src="http://d3js.org/d3.v3.min.js"></script>
  <script src="http://dimplejs.org/dist/dimple.v2.1.2.min.js"></script>
...

then everything works fine.

If think the solution is to copy these libraries to your local folder, or somehow set the yaml to give the proper src. The question then is, does anyone know how to set the yaml properly?

MarcinKosinski commented 9 years ago

It might be imposible in shiny app (exactly in shinydahboard). Problem wasn't visible when I run this code on Windows. The problem of not displaying was under ubuntu.

joseah commented 8 years ago

Hi, I ended up modifying the html output plot as follows:

# Save plot
dP$save(paste0(output, '.html'), cdn = TRUE)

# Open html file
html <- readLines(paste0(output, '.html'))

# Identify lines where libraries are used
d3 <- grep(".*/d3.*\\.js.*", html, perl = TRUE)
dimple <- grep(".*/dimple.*\\.js.*", html, perl = TRUE)

# Substitute with web-based libraries
html[d3] <- '<script src="http://d3js.org/d3.v3.min.js"> type="text/javascript"></script>'
html[dimple] <- '<script src="http://dimplejs.org/dist/dimple.v2.1.2.min.js"> type="text/javascript"></script>'

# Save new html plot
con <- file(paste0(output, '_fixed.html'))
writeLines(html, con)
close(con)

It's kind of dirty, but at least now is reproducible. Hope this can be helpful to someone.

mattobrien415 commented 8 years ago

I am also having this problem. When I run

hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")
n1$print("chart3")

I don't get any visual output in RStudio. I am using RStudio version 0.98.1103, and just got the most recent version of rCharts a few minutes ago. Using Ubuntu 14.04...hummm....