Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an R Markdown vignette containing plotly graphics

Tags:

r

I have an existing R package on CRAN (the rms package) for which I desire to add an html vignette created using R Markdown with RStudio. I see most of what I need in the Writing R Extensions manual and at How to get RStudio to automatically compile R Markdown Vignettes?

What is not obvious is that I want to use plotly functions to create interactive graphics. Self-contained html files using this with RStudio work great but I don't know how to make sure such vignettes work with a CRAN submission, and how to set this up.

like image 260
Frank Harrell Avatar asked Jul 24 '16 16:07

Frank Harrell


People also ask

How do I make an R package vignette?

To create a package vignette in R Markdown, the easiest way is through the RStudio menu File -> New File -> R Markdown -> From Template (see Figure 16.4). Then you select “Package Vignette” from the rmarkdown package, and you will get a vignette template.

Does Plotly work in R markdown?

Embedding R Graphs in RMarkdown files If you are creating R charts in an RMarkdown environment with HTML output (such as RStudio), simply printing a graph you created using the plotly R package in a code chunk will result in an interactive HTML graph in the viewer.

How do I embed a plot in R markdown?

You can embed an R code chunk like this: ```{r} summary(cars) ``` You can also embed plots, for example: ```{r, echo=FALSE} plot(cars) ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Can you use Plotly in R?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.


1 Answers

I have tried the following. I created an R markdown document (test.Rmd) in RStudio and put in the following.

## Testing interactive graphics

```{r}
library(highcharter)
library(ggplot2)

data(diamonds, economics_long, mpg, package = "ggplot2")
hchart(mpg, "scatter", x = displ, y = hwy, group = class)
```

Convert this 'test.Rmd' to 'test.md' and finally to 'test.html' by clicking on the Knit HTML button in RStudio OR by running the following script in the console:

library(knitr)
knit("test.Rmd", tangle=F, encoding = "utf-8")
render("test.md",output_format=html_document())

This generates an html file with interactive graphics.

hicharter-plot

Yes. This is not plotly but highcharter is a nice R package that includes several interactive javascript plotting libraries and it's easy to use. It's also better documented than rCharts for example. The package maintainer is also friendly and responsive to queries. Install highcharter package here:

library(devtools)
install_github("jbkunst/highcharter")
like image 61
rmf Avatar answered Oct 23 '22 17:10

rmf