rr-markdownblogdownreactable

Load table saved with qsave in R Markdown


I have a reactable table that I can display successfully on a web page if I embed the R Script in the R Markdown. However, I want to build the table in a separate R Script and load it into the R Markdown file.

In a separate R Script I build the reactable table.

> class(react_tbl)
[1] "reactable"  "htmlwidget"

The table appears in the RStudio Viewer.

Now I save the table:

qsave(react_tbl, react_table_filename)

To check, I do this in an R Script:

react_tbl <- qread(react_table_filename)

react_tbl

The table appears in the viewer.

Now in R Markdown I do this and publish the page as I normally would using Blogdown > Github > Netlify:

```{r, echo=FALSE, message=FALSE, warning=FALSE}

library("qs")

react_table_filename <- "<path to saved react table.qs>"
react_table <- qread(react_table_filename)
react_table

```

The result on the web page is lots of text:

## $x
## $x$tag
## <Reactable data="{&quot;etf_symbol&quot;:[&quot;CASH.TO&quot;,&quot;CSAV.TO&quot;,&quot;HISA.NE&quot;,&quot;NSAV.NE&quot;,&quot;PSA.T O&quot;],&quot;five_hundred&quot;:...

How do I get the actual reactable table to appear?


Solution

  • In the S3 system in R, special printing is done by print methods specific to the object. As you saw, reactable creates objects with classes "reactable" and "htmlwidget", so R will look for a print.reactable function first, then print.htmlwidget, then print.default, and will use the first one it finds. (In an R Markdown document auto-printing will actually look for knit_print.reactable, knit_print.htmlwidget, then knit_print.default, but that difference is not relevant here.)

    In your R Markdown file, you load the object, but you didn't load the print functions. Adding library(reactable) to the setup of the document should fix things. reactable imports htmlwidgets, so that package (and a few others) will also be loaded automatically, and the print methods will be available.