Skip to content

R Integration

GoFigr’s R package (gofigR) provides automatic figure capture for ggplot2, base R graphics, and more.

gofigR integrates with:

  • R Markdown (knitr)
  • Interactive sessions in RStudio
  • Shiny applications
  • Standalone scripts

Tested with R 4.3.2, but any reasonably recent version should work.

# From CRAN
install.packages("gofigR")
# Or from GitHub (development version)
library(devtools)
devtools::install_github("gofigr/gofigR")

Run the configuration wizard once:

library(gofigR)
gfconfig()

This saves your credentials to ~/.gofigr.


In your setup chunk or at the start of your script:

library(gofigR)
gofigR::enable()

You can optionally specify an analysis name:

gofigR::enable(analysis_name = "My Analysis")

Use the publish() function:

library(ggplot2)
# Create a plot
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
ggtitle("Weight vs MPG")
# Publish it
publish(p, "Weight vs MPG")
# Or use the pipe
p %>% publish("Weight vs MPG")

Wrap base R plotting code in publish():

publish({
plot(pressure, main = "Pressure vs Temperature")
text(200, 50, "Note the non-linear relationship")
}, figure_name = "Pressure Plot")

You can optionally attach data:

publish({
data <- as.matrix(mtcars)
coul <- colorRampPalette(brewer.pal(8, "PiYG"))(25)
heatmap(data, scale = "column", col = coul, main = "Visualizing mtcars")
}, data = mtcars, figure_name = "Cars Heatmap")

The data argument specifies data to associate with the figure—it will appear under “Files” (as .RDS) in GoFigr.


In your R Markdown document:

```{r setup, include=FALSE}
library(gofigR)
gofigR::enable()
```
```{r analysis}
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() %>%
publish("Iris Measurements")
```

Replace plotOutput + renderPlot with gfPlot + gfPlotServer:

library(shiny)
library(gofigR)
gofigR::enable()
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30)
),
mainPanel(
gfPlot("distPlot")
)
)
)
server <- function(input, output) {
gfPlotServer("distPlot", {
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
}, input, figure_name = "Old Faithful Waiting Times")
}
shinyApp(ui = ui, server = server)

Note: Pass input to gfPlotServer to capture Shiny inputs as metadata.


Some plotting functions like pheatmap::pheatmap() both draw immediately and return an object. This can cause duplicates in R Markdown.

Problem:

pheatmap::pheatmap(mat) %>% publish("My heatmap")
# Shows heatmap twice!

Solution:

hm <- pheatmap::pheatmap(mat, silent = TRUE) # Don't draw immediately
publish(hm, "My heatmap") # Only GoFigr version appears