This function adds a ggplot object to the ggfigdone database. It can also be utilized to update an existing figure using its figure ID.

fd_add(
  name,
  g = last_plot(),
  fdObj = fd_get_db(),
  width = 10,
  height = 10,
  units = "cm",
  dpi = 200,
  overwrite = F,
  id = uuid::UUIDgenerate()
)

Arguments

name

A character string representing the figure name.

g

A ggplot object.

fdObj

An object of class fdObj.

width

A numeric value specifying the width of the canvas.

height

A numeric value specifying the height of the canvas.

units

A character string indicating the units of the canvas.

dpi

A numeric value denoting the dpi of the canvas.

overwrite

A logical value. If set to TRUE, the function will overwrite the figure if it already exists. If set to FALSE, the function will terminate with an error message.

id

A character string representing the figure ID. If not provided, the function will generate a random ID. Alternatively, an existing ID can be provided to update the corresponding figure.

Value

An object of class fdObj.

Examples

library(ggplot2)
#> Want to understand how all the pieces fit together? Read R for Data
#> Science: https://r4ds.hadley.nz/

## Initial ggfigdone database using `fd_init`
db_dir = file.path(tempdir(), "fd_add_exp")
fo = fd_init(db_dir, rm_exist = TRUE)
#> The database version is up-to-date.

## Draw a ggplot figure
g = ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()

## Add the figure to the database
fd_add(g = g, name  = "fig1", fo)
#> Automatic saving the ggfigdone data to the disk ...
#> [1] TRUE

## Add the same figure with a different name
fd_add(g = g, name  = "fig2", fo)
#> Automatic saving the ggfigdone data to the disk ...
#> [1] TRUE

## Show the updated ggfigdone database
print(fo)
#> ##########
#> ## ggfigdone database: /tmp/RtmpG9xoyp/fd_add_exp
#> ## Number of figures: 2
#> ## Last updated date: 2024-08-16 14:28:38