# R

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

## 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

```r
# From CRAN
install.packages("gofigR")

# Or from GitHub (development version)
library(devtools)
devtools::install_github("gofigr/gofigR")
```

## Configuration

Run the configuration wizard once:

```r
library(gofigR)
gfconfig()
```

This saves your credentials to `~/.gofigr`.

***

## Basic Usage

### Enable GoFigr

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

```r
library(gofigR)
gofigR::enable()
```

You can optionally specify an analysis name:

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

### Publishing Plots

Use the `publish()` function:

```r
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")
```

### Base R Graphics

Wrap base R plotting code in `publish()`:

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

You can optionally attach data:

```r
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

In your R Markdown document:

````markdown
```{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

Replace `plotOutput + renderPlot` with `gfPlot + gfPlotServer`:

```r
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

### 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:**

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

**Solution:**

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gofigr.io/integrations/r.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
