ramnathv / rCharts

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

cannot find rCharts script files with standalone html in rmarkdown #492

Open jrowen opened 10 years ago

jrowen commented 10 years ago

I am using the command below to include an rChart is an rmarkdown standalone html document

...
a$show('inline', include_assets = TRUE, cdn = TRUE)

This generates a pandoc error stating the included js files cannot be found

pandoc.exe: Could not find data file //code.jquery.com/jquery-1.9.1.min.js

I think the fix here is to change add_lib_assets to prepend http: to the script paths and wrap them in single quotes. There also appears to be an additional double quote added to the highcharts-more.js path.

The existing tags are

    <script src='//code.jquery.com/jquery-1.9.1.min.js' type='text/javascript'></script>
    <script src='//code.highcharts.com/highcharts.js' type='text/javascript'></script>
    <script src='//code.highcharts.com/highcharts-more.js"' type='text/javascript'></script>
    <script src='//code.highcharts.com/modules/exporting.js' type='text/javascript'></script>

and after these changes they become

    <script src='http://code.jquery.com/jquery-1.9.1.min.js' type='text/javascript'></script>
    <script src='http://code.highcharts.com/highcharts.js' type='text/javascript'></script>
    <script src='http://code.highcharts.com/highcharts-more.js' type='text/javascript'></script>
    <script src='http://code.highcharts.com/modules/exporting.js' type='text/javascript'></script>

I've tested this change via a custom knitr hook, and the resulting doc is now standalone.

ramnathv commented 10 years ago

If you are using it with pandoc and selfcontained = TRUE, a better option might be to just use

a$show('inline', include_assets = TRUE)

This way pandoc will automatically pick up the dependencies.

Let me know if this works. I will push a fix for the cdn = TRUE case as well, but this should work for now.

jrowen commented 10 years ago

With cdn = FALSE, I'm seeing the error below

pandoc.exe: Failed to retrieve C:/Users/jrowen/Documents/R/win-library/3.1/rCharts/libraries/highcharts/js/jquery-1.9.1.min.js
InvalidUrlException "C:/Users/jrowen/Documents/R/win-library/3.1/rCharts/libraries/highcharts/js/jquery-1.9.1.min.js" "Invalid scheme"
Error: pandoc document conversion failed with error 61

I'm using the preview version of RStudio to create the document. Below are the relevant lines from the md file.

<script type='text/javascript' src=C:/Users/jrowen/Documents/R/win-library/3.1/rCharts/libraries/highcharts/js/jquery-1.9.1.min.js></script>
<script type='text/javascript' src=C:/Users/jrowen/Documents/R/win-library/3.1/rCharts/libraries/highcharts/js/highcharts.js></script>
<script type='text/javascript' src=C:/Users/jrowen/Documents/R/win-library/3.1/rCharts/libraries/highcharts/js/highcharts-more.js></script>
<script type='text/javascript' src=C:/Users/jrowen/Documents/R/win-library/3.1/rCharts/libraries/highcharts/js/exporting.js></script>
ramnathv commented 10 years ago

Ah. I should have guessed that. Let me push the fix for cdn = TRUE. I will try to get it done this week.

jrowen commented 10 years ago

I'm running into the same error when adding controls to a plot, but I'm assuming the same logic is used to add the script tags in all cases.

Even after manually correcting the script path, I'm seeing the controls but not the plot, so if you have any suggestions, I'd appreciate it. Here's a sample rmd file I'm using.

---
title: "rCharts"
output: html_document
---

```{r rcht, message=FALSE, echo=FALSE, results='asis'}   
library(rCharts)
n1 <- rPlot(mpg ~ wt, data = mtcars, color = "gear", type = "point")

n1$addControls("x", value = "wt", values = names(mtcars))
n1$addControls("y", value = "wt", values = names(mtcars))
n1$addControls("color", value = "gear", values = names(mtcars))

n1$addParams(height=600, width="80%")
n1$show('inline', include_assets = TRUE, cdn = TRUE)


A little more background for the requests.  I think a better way to accomplish this is to create a `shiny`app, but I'm running into situations where I need to share analysis via email.  I've found that the standalone html is a good mechanism, and the interactivity of `rCharts` is really useful.  Thanks for the assistance.
ramnathv commented 10 years ago

@jrowen I have NOT yet optimized rCharts for the new rmarkdown workflow. Pandoc is very strict about how it views dependencies, and that makes life difficult. I am not sure if I will be able to come up with a quick solution to this problem, but I will be working on it and will update you.

ramnathv commented 10 years ago

I tried your code. The chart with controls use a slightly different mechanism and haven't been optimized for the rmarkdown workflow. However, the following works for me with rmarkdown.

---
title: "rCharts"
output: html_document
---

```{r rcht, message=FALSE, echo=FALSE, results='asis'}   
library(rCharts)
n1 <- rPlot(mpg ~ wt, data = mtcars, color = "gear", type = "point")
n1$show('inline', include_assets = TRUE, cdn = F)
patilv commented 10 years ago

The charts with control code worked for me with an iframe option: n1$save as html and iframe of that file.

ramnathv commented 10 years ago

Does it work when you have the selfcontained option turned on in rmarkdown?

jrowen commented 10 years ago

I wasn't able to get inline to work, as pandoc appears to ignore the iframe tag leaving the external file reference in place.

Another option might be n1$show('iframesrc', cdn = TRUE). Although this would not be completely self-contained, it would include all of the local data within the file and only require an internet connection to access the external script and stylesheets.

I've been able to get this to work by editing the resulting html file to prepend http: to the //ramnathv.github.io/rCharts/libraries/widgets/polycharts/js/polychart2.standalone.js path and increasing the iframe.rChart height parameter in the style.

I'm assuming that the cdn fix will also correct the path for the widgets scripts. Would it also be possible to parameterize the <style>iframe.rChart{ width: 100%; height: 400px;}</style> tag within the iframesrc result?

patilv commented 10 years ago

I am able to get this to work offline (knitting of rmd and output offline)... the default in rmarkdown is apparently the standalone mode... in any case, inserting that didn't alter the output.

title: "tmp" author: "tmp" date: "Wednesday, August 13, 2014"

output: html_document


library(rCharts)
n1 <- rPlot(mpg ~ wt, data = mtcars, color = "gear", type = "point")

n1$addControls("x", value = "wt", values = names(mtcars))
n1$addControls("y", value = "wt", values = names(mtcars))
n1$addControls("color", value = "gear", values = names(mtcars))
n1$save("abc.html",standalone=TRUE)
jrowen commented 10 years ago

@patilv I think your approach works if the desired output is a single rChart. In this situation, I'm looking to include an rChart within a larger html document, and therefore need to capture the output using show().

patilv commented 10 years ago

@jrowen , I am not sure of your objective but I would think the approach would could handle multiple charts. As an example, the following Rmd with two rCharts also works for me (offline).


title: "tmp" author: "tmp" date: "Wednesday, August 13, 2014" output: html_document


Chart 1


library(rCharts)
n1 <- rPlot(mpg ~ wt, data = mtcars, color = "gear", type = "point")

n1$addControls("x", value = "wt", values = names(mtcars))
n1$addControls("y", value = "wt", values = names(mtcars))
n1$addControls("color", value = "gear", values = names(mtcars))
n1$save("abc.html",standalone=TRUE)

<iframe src="abc.html" width="1100" height="600"></iframe>

Chart 2


n2 <- nPlot(mpg ~ wt, data = mtcars, group = "gear", type = "scatterChart")
n2$addControls("x", value = "wt", values = names(mtcars))
n2$addControls("group", value = "gear", values = names(mtcars))
n2$save("def.html",standalone=TRUE)

<iframe src="def.html" width="1100" height="600"></iframe>

reyman commented 9 years ago

Hi, Using this Rcharts script

library(rCharts)
n1 <- rPlot(mpg ~ wt, data = mtcars, color = "gear", type = "point")
n1$show('iframesrc', cdn = TRUE)

into a iosslide presentation using RMarkdown, nothing appear :/

---
title: "presentation"
author: "bla"
date: "22/10/2014"
output:
  ioslides_presentation:
    highlight: pygments
    css: slides.css
runtime: shiny
widescreen: yes
---
ramnathv commented 9 years ago

You need to open the presentation in a local webserver, either using the servr package or python -m SimpleHTTPServer.

patilv commented 9 years ago

Code works - link of deployed shiny app: https://patilv.shinyapps.io/rchartsmarkdownissue/rchartsmarkdownissue.Rmd#1

ramnathv commented 9 years ago

Thanks @patilv for checking this. I will wait for @reyman to confirm that he is able to get this working by opening a web server, before closing this issue.

englianhu commented 8 years ago
suppressMessages(library('rCharts'))

dt1 <- dTable(corpUS, sPaginationType = "full_numbers", iDisplayLength = 10, sScrollX = "100%")

## http://bl.ocks.org/ramnathv/raw/8084330/
dt1$show('iframesrc', cdn = TRUE)
#'@ dt1$show('inline', include_assets = TRUE) #keep loading but unable display anything
#'@ dt1$show('inline', include_assets = TRUE, cdn = TRUE) #blank table frame shown

is works for me. http://bl.ocks.org/ramnathv/raw/8084330/

mylegco commented 8 years ago

So is the plan to close this issue without fixing the inline option? save("abc.html",standalone=TRUE) is working for me, but it's not a great workflow for jekyll.