3

Is there a way to have the data associated with and html output file generated via Rmarkdown be loaded dynamically (e.g., via javascript or ajax) when opening the html output?

For instance, I have this simple Rmarkdown file which produces a plot (in svg) and a table:

---
title: "test"
output: 
    html_document:
        self_contained: false
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(dev = "svglite", fig.ext = ".svg")
library(svglite)
library(DT)
```

```{r, echo=FALSE}
DATA <- data.frame(a = 1:5, b = 6:10)
plot(DATA)
datatable(DATA)
```

Is there a way to feed the data to the html file so that when the data changes and I open it again the plot and table are updated automatically without having to render the Rmarkdown script?

1 Answer 1

4
+50

Yes, this is possible. You can use local data or data stored somewhere--like Google Sheets.

Step 1) Add runtime: shiny to your YAML. This does not make this "shiny", but does make it dynamic.

Updated YAML:

---
title: "test"
output: 
    html_document:
        self_contained: false
runtime: shiny
---

Step 2) Use a function to call your data.

```{r giveMe,echo=F}

giveMeData <- function(){
  read.csv("./../_data/carData.csv")
}

dataGiven <- giveMeData()
```

Whether in the Viewer pane in RStudio or in your browser, you only need to refresh the view or page after you update the data.

To test this the only other code I used is the following:

<!--- inline R code to show the data --->

The column names are `r names(dataGiven)`.
There are `r nrow(dataGiven)` observations in this data.


```{r itsGiven, echo=FALSE}
# plot the data
plot(dataGiven)
datatable(dataGiven)
```

enter image description here

I changed the column name to "Displacement" and refreshed (not reran, run, knit, just refresh)

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

This is pretty cool! But note that ""rmarkdown will no longer compile your document into a static file. Instead it will “run” the document as a live Shiny app" (source). That means you won't get a html output and you won't be able to view the "document" when the shiny server isn't running.
If you change the data, you will need to execute R functions again. Essentially, shiny keeps you connected to R, so the code can be executed. If you only used widgets-based R objects (like data.table or networkD3), you might be able to link a live data source. You could write JS functions in RMarkdown for that purpose. However, you would have to make it so that no the HTML required no R functions to execute to make excluding R work. By the time you created this type of HTML script, it would probably be easier to create it outside of R Markdown.
Thanks! However, I was looking for a solution that can standalone, that is, without shiny.
I would agree with @Kat Essentially, you are asking for 100% client side computations and to my knowledge that is not what Rmarkdown was designed for. You would have to extend your Rmarkdown document with so much js that you might just go ahead and write in js directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.