R Integration
GoFigr’s R package (gofigR) provides automatic figure capture for ggplot2, base R graphics, and more.
Compatibility
Section titled “Compatibility”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.
Installation
Section titled “Installation”# From CRANinstall.packages("gofigR")
# Or from GitHub (development version)library(devtools)devtools::install_github("gofigr/gofigR")Configuration
Section titled “Configuration”Run the configuration wizard once:
library(gofigR)gfconfig()This saves your credentials to ~/.gofigr.
Basic Usage
Section titled “Basic Usage”Enable GoFigr
Section titled “Enable 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")Publishing Plots
Section titled “Publishing Plots”Use the publish() function:
library(ggplot2)
# Create a plotp <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + ggtitle("Weight vs MPG")
# Publish itpublish(p, "Weight vs MPG")
# Or use the pipep %>% publish("Weight vs MPG")Base R Graphics
Section titled “Base R Graphics”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.
R Markdown
Section titled “R Markdown”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")```Shiny Integration
Section titled “Shiny Integration”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.
Common Issues
Section titled “Common Issues”Duplicate Heatmaps with pheatmap
Section titled “Duplicate Heatmaps with pheatmap”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 immediatelypublish(hm, "My heatmap") # Only GoFigr version appears