Printing plotly objects in a knitr/rmarkdown doc

knitr::opts_chunk$set(
  message = FALSE,
  fig.width = 10,
  fig.height = 4,
  comment = "#>",
  collapse = TRUE
)

Printing anything created via the R package plotly should "just work" in a knitr/rmarkdown document -- including representations of things on your plotly account. However, the default print method that the package provides may not work for your purposes, so this document is designed to help you go beyond those defaults. This is especially useful for objects representing "remote files".

Remote files

For example, if you create a plot or grid (via api_create()), then print the result, you get an HTML iframe pointing to that object on your account.

library(plotly)
d <- api_create(mtcars)
d

As it turns out, the api_create() function returns a bunch of metadata about that file on your account. A nice way to inspect that information is to leverage the jsonedit() function from the listviewer package.

listviewer::jsonedit(d)

Storing this information is a good idea since now you can modify the "remote file" at a later point. Let's rename the file using the "low-level" api() interface.

nm <- paste(sample(LETTERS, 20), collapse = "-")
d2 <- api(
  file.path("files", d$fid), "PATCH", list(filename = nm)
)
identical(d2$filename, nm)

The api_create() function also understands how to "upload" ggplot2/plotly objects to the web platform. Printing in this case will again produce an HTML iframe pointing to the plot as it appears on the platform.

p <- api_create(qplot(1:10, 1:10))
p

The metadata returned for a plot is structured very much like a grid (i.e., what we saw previously), but now we can leverage some attributes unique to a plot, such as image urls.

library(htmltools)
tags$img(src = p$image_urls$default)

Downloading files

You can also download a plot or a grid already hosted on plotly's web platform (assuming they're public, or you have the proper crendentials). When you download a plot, it is converted to an htmlwidget, meaning that when you print it, the plot will render entirely locally (i.e., you don't need internet access for it to render).

p <- api_download_plot(200, "cpsievert")
layout(
  p, title ="An htmlwidget version of <a href='https://plot.ly/~cpsievert/200'>this</a> plot"
)

There is no guarantee a plotly grid will always map back to an R data frame, so api_download_grid() returns the abstract list representation of the data, but will try to convert it to a data frame when printing.

g <- api_download_grid(14681, "cpsievert")
g
# note how the actual data is inside the 'preview' element
listviewer::jsonedit(g)


Try the plotly package in your browser

Any scripts or data that you put into this service are public.

plotly documentation built on Oct. 10, 2021, 1:06 a.m.