Example Code: Show Plotly Graph in R Report

Documentation
This topic gives you an example of how you can include an interactive plotly report in a LabKey R report by using knitr, Rmarkdown, and including an explicit dependency so that the event trigger required to render the report will fire at the right time. Use this example to get started, then develop your own reports with plotly graphs.

Install the plotly R Package

For the R scripting engine you'll be using, install the "plotly" R package. You may also need other packages depending on the contents of your actual report. This step only needs to be done once on your R engine.

Learn more about configuring R and installing packages in this topic:

Confirm R Engine Configuration

Confirm that R is configured to use rmarkdown:

  • Select > Site > Admin Console.
  • Under Configuration, click Views and Scripting.
  • Edit the details of your R engine configuration and confirm that the box for Use pandoc and rmarkdown is checked.
Learn more in this topic:

Upload Dependency File

Upload the necessary dependency file to the file browser in the /Shared project. This step also only needs to be done once on your server, enabling many reports to load this dependency using a consistent path.

  • Navigate to the /Shared project.
  • Select > Go To Module > FileContent.
  • Download, then drag and drop this file into the file browser:

Create the R Report with Plotly Graph

Navigate to the folder where you want the graph. Once created you'll be able to display it in a wiki or web part, or share it via direct link.

  • Select > Manage Views.
  • Select Add Report > R Report.
  • Expand the Knitr Options section and select Markdown.
  • In the Dependencies input box, enter:
    /_webdav/Shared/@files/DispatchDOMContentLoaded.js
  • In the Script Source box, paste in the following code:
    ---
    title: "Plotly Example"
    output: html_document
    ---

    ```{r testing}
    library(plotly, warn.conflicts = FALSE)

    fig <- plot_ly(x = ~rnorm(50), type = "box")
    fig <- fig %>% add_trace(x = ~rnorm(50, 1))
    fig
    ```
  • Save the report with the name of your choice, such as "PlotlyDemo".
  • Select > Manage Views (if you didn't automatically land there).
  • Click the name of the report to see it displayed. In this example, you can hover to reveal detailed annotations within the report:

To remove the message headers, add "echo=FALSE, message=FALSE":

---
title: "Plotly Example"
output: html_document
---

```{r testing, echo=FALSE, message=FALSE}
library(plotly, warn.conflicts = FALSE)

fig <- plot_ly(x = ~rnorm(50), type = "box")
fig <- fig %>% add_trace(x = ~rnorm(50, 1))
fig
```

Another Example Plotly Report

The following Plotly report queries a dataset and uses the 'labkey.data' data frame. For details see R Reports: Access LabKey Data.

---
title: "Plotly Example 2"
output: html_document
---

```{r testing, echo=FALSE, message=FALSE}
library(plotly, warn.conflicts = FALSE)

fig <- plot_ly(x = ~labkey.data$cd4, y = ~labkey.data$participantid, type = "box") %>%
layout(title = 'Distribution of CD4 Counts',
plot_bgcolor = "#ffffff",
xaxis = list(title = 'CD4 Counts'),
yaxis = list(title = 'Participant ID'))

fig

A rendering of the plot:

Troubleshooting

When developing a report, you can add a line like the following to an R Report to save the html file version of the output ('fig' in this case) on your local machine (in this case a 'labkey/reports folder'. Include in the above example on line 13 (between the 'fig' and '```' lines):

htmltools::save_html(html = fig, file="C:/labkey/reports/plotlytesting.html")

Related Topics

Was this content helpful?

Log in or register an account to provide feedback


previousnext
 
expand allcollapse all