We have worked to make it easy to embed Trelliscope displays in R
Markdown documents. It is as easy as calling
view_trelliscope()
on a Trelliscope data frame. However,
there are a few things to keep in mind.
First, you can only embed Trelliscope outputs in HTML-formatted output types that aren’t self-contained. We have tested the following output formats:
rmarkdown::html_vignette
rmarkdown::html_document
rmarkdown::slidy_presentation
revealjs::revealjs_presentation
Second, your output chunk needs to have a name. This will be used to determine where to write the output of the display.
Third, you need to leave the path
option of
as_trelliscope_df()
blank. Trelliscope will automatically
determine the path to use, which will be a relative path inside your R
Markdown output directory.
Additionally, you might consider setting some chunk options to make
the output look better. For example, it is a good idea to set
out.width="100%"
and hard-code the height
(out.height="___px"
) to control the size of the display. We
have also added support for a scale
chunk option that can
be used to scale the output UI so that it doesn’t appear so large when
rendered in confined spaces.
Below is an example of a chunk that will embed a Trelliscope display in an R Markdown document with these considerations in mind.
---
title: "Trelliscope RMarkdown Embedding"
output:
html_document:
self_contained: false
---
# Mars Rover Images
```{r mars_trelliscope, out.width="100%", out.height="700px", scale=0.5}
library(trelliscope)
d <- as_trelliscope_df(mars_rover, name = "mars rover")
view_trelliscope(d)
```
Another option for embedding Trelliscope displays in R Markdown documents is to pre-render them and host them somewhere on the internet and then stick an iframe tag in your R Markdown document.
Similar to R Markdown, when embedding in Quarto, you can set necessary output options in your code block to help a display render in the way you would like. For example:
Trelliscope is a great alternative to Shiny for a wide range of interactive visualization applications. In any situation where you are considering building a Shiny app that provides the user with many controls that produce a visual output, you should consider using Trelliscope instead by producing a data frame of visualizations corresponding to every combination of user input values. Trelliscope displays are easier to build, maintain, and share than shiny apps, and provide the benefit of being able to view many outputs simultaneously, as well as exploring the input parameter space interactively.
It can be the case, however, that you might want to build a Shiny app that allows a user to provide inputs for building a Trelliscope display, or in which you would like a user to be able to view a Trelliscope display. If the latter (no dynamic inputs required for creating the display), we advise to simply build the display up-front and embed it in an iframe somewhere in the Shiny app. For building and displaying dynamically-generated Trelliscope displays, we provide an example below.
In this example, we create a simple app that allows a user to choose
a dataset (either mars_rover
or none), provide a
description for the Trelliscope display, and then view the resulting
display.
library(trelliscope)
library(shiny)
tr_dir <- tempfile()
dir.create(tr_dir)
add_trelliscope_resource_path("trelliscope", tr_dir)
ui <- fluidPage(
titlePanel("Trelliscope Embed in Shiny"),
sidebarLayout(
sidebarPanel(
selectInput(
"dataset",
"Choose a dataset:",
choices = c("mars_rover", "[no data]")),
textInput("description", "Description:", "[display description]")
),
mainPanel(
trelliscopeOutput("trelliscope", style = "height: 800px")
)
)
)
server <- function(input, output) {
output$trelliscope <- renderTrelliscope({
if (input$dataset == "mars_rover") {
d <- as_trelliscope_df(mars_rover,
name = "mars rover",
description = input$description,
path = file.path(tr_dir, "test"),
jsonp = FALSE
)
d
} else {
NULL
}
})
}
shinyApp(ui = ui, server = server)
For those with experience in Shiny, most of the above should be
straightforward. One main function to call out is
add_trelliscope_resource_path()
, which allows you to
specify a path that the app can look in to find your Trelliscope
displays. In the UI, you can use trelliscopeOutput()
to
specify where a Trelliscope display will be rendered. In the server, you
use renderTrelliscope()
to write create the display. You
must place the display in the path you used with
add_trelliscope_resource_path()
. The object returned by
renderTrelliscope()
should be a Trelliscope data frame.
Trelliscope will take care of writing the display to the specified path
and rendering it in the UI.
Note that when we create our Trelliscope data frame in
renderTrelliscope()
, we use jsonp = FALSE
. If
you are using Shiny locally, you can likely stick with the default of
jsonp = TRUE
. However, if you are deploying your Shiny app
to a server, we have noticed that services like shinyapps.io and Posit
Connect do not like the data files written out by Trelliscope to be
stored as jsonp so you will need to set jsonp = FALSE
.