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

How to use saveWidget to save a crosstalk result as local HTML file? #266

Closed
ghost opened this issue Apr 10, 2017 · 10 comments
Closed

How to use saveWidget to save a crosstalk result as local HTML file? #266

ghost opened this issue Apr 10, 2017 · 10 comments

Comments

@ghost
Copy link

ghost commented Apr 10, 2017

How to use htmlwidgets::saveWidget to save a crosstalk result like
library(crosstalk)

shared_iris <- SharedData$new(iris)
hw <- bscols(
d3scatter(shared_iris, ~Petal.Length, ~Petal.Width, ~Species, width="100%", height=300),
d3scatter(shared_iris, ~Sepal.Length, ~Sepal.Width, ~Species, width="100%", height=300)
)

?? how to save hw as a local HTML file

Thanks!

@jjallaire
Copy link
Collaborator

The easiest thing might just be to place that code in an R Markdown document and render it.

That said, I know there is a way to do it from plain R but can't recall the specifics. @timelyportfolio ?

@jto888
Copy link

jto888 commented Apr 10, 2017 via email

@jjallaire
Copy link
Collaborator

Actually I think you can just do this:

library(crosstalk)

shared_iris <- SharedData$new(iris)
hw <- bscols(
d3scatter(shared_iris, ~Petal.Length, ~Petal.Width, ~Species, width="100%", height=300),
d3scatter(shared_iris, ~Sepal.Length, ~Sepal.Width, ~Species, width="100%", height=300)
)
htmltools::save_html(hw)

@timelyportfolio
Copy link
Collaborator

If that does not work, see rstudio/htmltools#73, and let me know.

@ghost
Copy link
Author

ghost commented Apr 10, 2017

Thanks everybody. save_html works. I tried like:
htmltools::save_html(hw, "foo.html", libdir = "lib"). It has some warnings:
Warning messages:
1: In format(object) : restarting interrupted promise evaluation
2: In get(method, envir = home) :
restarting interrupted promise evaluation
3: In get(method, envir = home) : internal error -3 in R_decompress1
But I did see the html can be opened and showed correctly.

hw$children can return the divs but seems no way to get the dependency scripts.

@jcheng5
Copy link
Collaborator

jcheng5 commented Apr 10, 2017

The error will likely go away if you restart your R process. Every time I've seen that it's because devtools has tried and failed to completely unload an old version of a package.

@jcheng5 jcheng5 closed this as completed Apr 10, 2017
@jcheng5
Copy link
Collaborator

jcheng5 commented Apr 10, 2017

And for the record, I agree with htmltools::save_html() being the answer for this; if it didn't work, I would consider it a bug.

@ismirsehregal
Copy link

ismirsehregal commented Feb 12, 2020

Is there any way to programmatically save a selfcontained HTML file from this code snippet:

library(crosstalk)
library(d3scatter)

shared_iris <- SharedData$new(iris)
hw <- bscols(
  d3scatter(shared_iris, ~Petal.Length, ~Petal.Width, ~Species, width="100%", height=300),
  d3scatter(shared_iris, ~Sepal.Length, ~Sepal.Width, ~Species, width="100%", height=300)
)
htmltools::save_html(hw, "result.html")

By default a lib folder is created.

However, using RStudio's export option "Save as webpage. . . " a standalone HTML file is produced. - How is it done?

htmlwidgets::saveWidget(hw, "result.html", selfcontained = TRUE) results in

Error in .getNamespace(pkg) :
invalid type/length (symbol/0) in vector allocation

which might be expected.

Any hints are very welcome! - Cheers

@danielludolf
Copy link

@ismirsehregal Did you ever figure out how RStudio's "Save as webpage" option creates a sole HTML file?

@ismirsehregal
Copy link

ismirsehregal commented Feb 15, 2022

@danielludolf RStudio requires installing library(markdown) before "Export -> Save as Web Page..." can be used.

However, I've been using htmlwidgets::saveWidget (where possible) and library(rmarkdown):

library(crosstalk)
library(plotly)
library(rmarkdown)
library(datasets)

shared_iris <- SharedData$new(iris)
fig <- bscols(
  plot_ly(shared_iris, x = ~Petal.Length, y = ~Petal.Width, colors = ~Species),
  plot_ly(shared_iris, x = ~Sepal.Length, y = ~Sepal.Width, colors = ~Species)
)
# htmlwidgets::saveWidget(fig, "result.html", selfcontained = TRUE) # this works using plotly::subplot instead of bscols
# also see ?partial_bundle

tempRmd <- tempfile(pattern = "plotly", fileext = ".Rmd")
writeLines('---
output: html_document
params:
  plotlyfig: NA
---

```{r plotlyout, echo=FALSE, message=FALSE, out.width="100%"}
params$plotlyfig
```', con = tempRmd)

rmarkdown::render(tempRmd, output_format = "html_document", output_file = "fig.html", output_options = list(self_contained = TRUE), params = list(plotlyfig = fig))
utils::browseURL(file.path(tempdir(), "/fig.html"))
# shell.exec(tempdir())

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

6 participants