Title: | Manage & Modify 'ggplot' Figures using 'ggfigdone' |
---|---|
Description: | When you prepare a presentation or a report, you often need to manage a large number of 'ggplot' figures. You need to change the figure size, modify the title, label, themes, etc. It is inconvenient to go back to the original code to make these changes. This package provides a simple way to manage 'ggplot' figures. You can easily add the figure to the database and update them later using CLI (command line interface) or GUI (graphical user interface). |
Authors: | Wenjie SUN [aut, cre] |
Maintainer: | Wenjie SUN <[email protected]> |
License: | GPL-3 |
Version: | 0.1.2 |
Built: | 2024-10-16 06:02:52 UTC |
Source: | https://github.com/wenjie1991/ggfigdone |
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 = FALSE, id = uuid::UUIDgenerate() )
fd_add( name, g = last_plot(), fdObj = fd_get_db(), width = 10, height = 10, units = "cm", dpi = 200, overwrite = FALSE, id = uuid::UUIDgenerate() )
name |
A character string representing the figure name. |
g |
A ggplot object. |
fdObj |
An object of class |
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. |
An object of class fdObj
.
library(ggplot2) ## Initial ggfigdone database using `fd_init` db_dir = file.path(tempdir(), "fd_add_exp") fo = fd_init(db_dir, rm_exist = TRUE) ## 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) ## Add the same figure with a different name fd_add(g = g, name = "fig2", fo) ## Show the updated ggfigdone database print(fo)
library(ggplot2) ## Initial ggfigdone database using `fd_init` db_dir = file.path(tempdir(), "fd_add_exp") fo = fd_init(db_dir, rm_exist = TRUE) ## 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) ## Add the same figure with a different name fd_add(g = g, name = "fig2", fo) ## Show the updated ggfigdone database print(fo)
This function is designed to update the size of the figure canvas.
fd_canvas( id, fdObj = fd_get_db(), width = fdObj$env[[id]]$canvas_options$width, height = fdObj$env[[id]]$canvas_options$height, units = fdObj$env[[id]]$canvas_options$units, dpi = fdObj$env[[id]]$canvas_options$dpi )
fd_canvas( id, fdObj = fd_get_db(), width = fdObj$env[[id]]$canvas_options$width, height = fdObj$env[[id]]$canvas_options$height, units = fdObj$env[[id]]$canvas_options$units, dpi = fdObj$env[[id]]$canvas_options$dpi )
id |
A character string representing the figure ID. |
fdObj |
An object of class |
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 measurement for the canvas, such as "cm", "in", "mm", or "px". |
dpi |
A numeric value denoting the dots per inch (DPI) of the canvas. |
No return value, changes are made directly to the ggfigdone database.
Get the default ggfigdone database
fd_get_db()
fd_get_db()
An object of class fdObj
representing the default ggfigdone database.
This function generates a folder that serves as a database for ggfigdone.
fd_init(dir, recursive = TRUE, rm_exist = FALSE, set_default = TRUE, ...)
fd_init(dir, recursive = TRUE, rm_exist = FALSE, set_default = TRUE, ...)
dir |
A character string specifying the directory path. |
recursive |
A logical value. If TRUE, the function will create the directory along with any necessary parent directories if they do not already exist. If FALSE, the function will create the directory only if its parent directory already exists. |
rm_exist |
A logical value. If TRUE, the function will remove the content in the directory if it already exists. If FALSE, the function will ask the user whether to remove the content in the directory. |
set_default |
A logical value. If TRUE, the function will set the database as the default database. |
... |
Additional arguments to be passed to |
An object of class fdObj
.
library(ggplot2) ## create ggfigdone database in a temporary directory db_dir = file.path(tempdir(), "fd_init") ## Initate the ggfigdone database fd_init(db_dir, rm_exist = TRUE)
library(ggplot2) ## create ggfigdone database in a temporary directory db_dir = file.path(tempdir(), "fd_init") ## Initate the ggfigdone database fd_init(db_dir, rm_exist = TRUE)
This function loads the ggfigdone database from the disk.
fd_load(dir, auto_database_upgrade = TRUE, set_default = TRUE)
fd_load(dir, auto_database_upgrade = TRUE, set_default = TRUE)
dir |
A character string representing the directory path. |
auto_database_upgrade |
A logical value. If TRUE, the function will automatically upgrade the database to the latest version.
If FALSE, you need to manually save the data using the |
set_default |
A logical value. If TRUE, the function will set the database as the default database. |
An object of class fdObj
.
library(ggplot2) ## create ggfigdone database in a temporary directory db_dir = file.path(tempdir(), "fd_load") fd_init(db_dir, rm_exist = TRUE) ## Load the ggfigdone database fd_load(db_dir)
library(ggplot2) ## create ggfigdone database in a temporary directory db_dir = file.path(tempdir(), "fd_load") fd_init(db_dir, rm_exist = TRUE) ## Load the ggfigdone database fd_load(db_dir)
This function provides a List or data.frame of figures along with their associated parameters.
fd_ls(fdObj = fd_get_db()) fd_df(fdObj = fd_get_db())
fd_ls(fdObj = fd_get_db()) fd_df(fdObj = fd_get_db())
fdObj |
An instance of the |
The parameters include:
id: The unique identifier for the figure
name: The name of the figure
created_date: The date the figure was created
updated_date: The date the figure was last updated
width: The width of the canvas
height: The height of the canvas
units: The units of measurement for the canvas
dpi: The dots per inch (DPI) of the canvas
file_name: The name of the file
plot_labels: The labels used in the plot
A List/data.frame containing the figures along with their respective parameters.
This function merges two ggfigdone databases. The function will update the figures in the 'to' database with the figures in the 'from' database. If there is a figure with the same ID in both databases, the function will keep the figure with the latest updated date or created date.
fd_merge(from, to = fd_get_db(), replace = "updated_date")
fd_merge(from, to = fd_get_db(), replace = "updated_date")
from |
An object of class |
to |
An object of class |
replace |
A character string specifying the method to keep the figure with the unique ID. It can be either "updated_date" or "created_date". |
An object of class fdObj
with the merged database.
library(ggplot2) ## create ggfigdone database in a temporary directory db_dir1 = file.path(tempdir(), "db1") db_dir2 = file.path(tempdir(), "db2") fo1 = fd_init(db_dir1, rm_exist = TRUE) fo2 = fd_init(db_dir2, rm_exist = TRUE) ## 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", fdObj = fo1) fd_add(g = g + theme_classic(), name = "fig2", fdObj = fo2) ## Merge the databases fo_merge = fd_merge(from = fo1, to = fo2, replace = "updated_date") ## Show the updated ggfigdone database print(fo_merge) ##
library(ggplot2) ## create ggfigdone database in a temporary directory db_dir1 = file.path(tempdir(), "db1") db_dir2 = file.path(tempdir(), "db2") fo1 = fd_init(db_dir1, rm_exist = TRUE) fo2 = fd_init(db_dir2, rm_exist = TRUE) ## 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", fdObj = fo1) fd_add(g = g + theme_classic(), name = "fig2", fdObj = fo2) ## Merge the databases fo_merge = fd_merge(from = fo1, to = fo2, replace = "updated_date") ## Show the updated ggfigdone database print(fo_merge) ##
This function removes a figure from the ggfigdone database.
fd_rm(id, fdObj = fd_get_db())
fd_rm(id, fdObj = fd_get_db())
id |
A character string representing the figure ID. |
fdObj |
An object of class |
No return value, changes are made directly to the ggfigdone database.
This function saves the ggfigdone data to the disk.
By default, when using the fd_load
funciton to load the databse, the data will be automatically saved to the disk when changes are made.
But if you set the auto_database_upgrade
argument to FALSE
in fd_load
, you need to manually save the data using this function.
fd_save(fdObj = fd_get_db(), do_lock = TRUE)
fd_save(fdObj = fd_get_db(), do_lock = TRUE)
fdObj |
An object of class |
do_lock |
A logical value. If TRUE, the function will lock the database file when saving the data. |
No return value, changes are made directly to the ggfigdone database.
This function initiates a server for ggfigdone, which can be accessed through a web browser. The web application enables users to manage and modify ggplot figures with ease. Users have the ability to:
Update the ggplot code by adding new components.
Adjust the figure size.
Download the figure as a PDF.
Download the data used to create the figure.
fd_server(dir, host = "0.0.0.0", port = 8080, auto_open = TRUE)
fd_server(dir, host = "0.0.0.0", port = 8080, auto_open = TRUE)
dir |
The directory of the ggfigdone database. |
host |
The host on which the server will run; the default is '0.0.0.0'. |
port |
The port on which the server will run; the default is 8080. |
auto_open |
A logical value indicating whether the server should be opened in a web browser; the default is TRUE. |
By default the function will open a web browser to access the server.
You can configure the web browser by setting the options:
options(browser = "firefox") # Set Firefox as the default
No return value, the function is called for its side effects.
Set the default ggfigdone database
fd_set_db(fdObj)
fd_set_db(fdObj)
fdObj |
An object of class |
No return value, the default ggfigdone database is set to an environment variable.
This function keeps the figure name unique by removing the older figures with the same name. Users can specify whether to keep the figure with the latest updated date or the latest created date. If a figure is created without changing, the created date and updated date are the same.
fd_unique(fdObj = fd_get_db(), by = "updated_date")
fd_unique(fdObj = fd_get_db(), by = "updated_date")
fdObj |
An object of class |
by |
A character string specifying the method to keep the figure with the unique name. It can be either "updated_date" or "created_date". |
An object of class fdObj
.
library(ggplot2) ## create ggfigdone database in a temporary directory db_dir = file.path(tempdir(), "fd_unique") fo = fd_init(db_dir, rm_exist = TRUE) ## 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", fdObj = fo) ## Add the another figure with the same name fd_add(g = g + theme_classic(), name = "fig1", fdObj = fo) ## Keep the figure with the latest created date fd_unique(fdObj = fo, by = "created_date") ## Show the updated ggfigdone database print(fo)
library(ggplot2) ## create ggfigdone database in a temporary directory db_dir = file.path(tempdir(), "fd_unique") fo = fd_init(db_dir, rm_exist = TRUE) ## 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", fdObj = fo) ## Add the another figure with the same name fd_add(g = g + theme_classic(), name = "fig1", fdObj = fo) ## Keep the figure with the latest created date fd_unique(fdObj = fo, by = "created_date") ## Show the updated ggfigdone database print(fo)
This function updates a figure using a ggplot expression.
fd_update_fig(id, expr, fdObj = fd_get_db())
fd_update_fig(id, expr, fdObj = fd_get_db())
id |
A character string of the figure id |
expr |
A character string of the ggplot expression |
fdObj |
An object of class |
A character string of the status
ggfigdone: Manage & Modify ggplot figures easily
When preparing a presentation or report, it is often necessary to manage a substantial number of ggplot figures. Adjustments such as changing the figure size, modifying titles, labels, and themes may be required. Returning to the original code to implement these changes can be inconvenient. This package offers a straightforward method for managing ggplot figures. Figures can be easily added to the database and subsequently updated using either a GUI (graphical user interface) and/or CLI (command line interface).
Maintainer: Wenjie SUN [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/wenjie1991/ggfigdone/issues