From 724dc21d7171a47a62bc21826f81f08456cbc3ab Mon Sep 17 00:00:00 2001 From: Wenjie SUN Date: Wed, 14 Aug 2024 14:01:35 +0200 Subject: [PATCH] [Feat] Add fd_add syntax suggar by adding last plot to default ggfigdone database as default --- DESCRIPTION | 2 +- NAMESPACE | 2 ++ NEWS.md | 7 ++++++ R/lib.R | 56 ++++++++++++++++++++++++++++++++++---------- README.md | 18 ++++++++------ man/fd_add.Rd | 12 +++++----- man/fd_canvas.Rd | 2 +- man/fd_get_db.Rd | 11 +++++++++ man/fd_init.Rd | 4 +++- man/fd_load.Rd | 4 +++- man/fd_ls.Rd | 4 ++-- man/fd_merge.Rd | 4 ++-- man/fd_rm.Rd | 2 +- man/fd_save.Rd | 2 +- man/fd_set_db.Rd | 14 +++++++++++ man/fd_unique.Rd | 2 +- man/fd_update_fig.Rd | 2 +- 17 files changed, 110 insertions(+), 38 deletions(-) create mode 100644 NEWS.md create mode 100644 man/fd_get_db.Rd create mode 100644 man/fd_set_db.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 942c05b..b3ed37e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ggfigdone Title: Manage & Modify ggplot figures using ggfigdone -Version: 0.1.0 +Version: 0.1.1 Authors@R: person("Wenjie", "SUN", , "sunwjie@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-3100-2346")) diff --git a/NAMESPACE b/NAMESPACE index 507d6cf..dac168e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ S3method(print,fdObj) export(fd_add) export(fd_canvas) export(fd_df) +export(fd_get_db) export(fd_init) export(fd_load) export(fd_ls) @@ -12,6 +13,7 @@ export(fd_merge) export(fd_rm) export(fd_save) export(fd_server) +export(fd_set_db) export(fd_unique) export(fd_update_fig) import(ggplot2) diff --git a/NEWS.md b/NEWS.md new file mode 100644 index 0000000..212d0ed --- /dev/null +++ b/NEWS.md @@ -0,0 +1,7 @@ +## Version 0.1.1 + +- Feature: Add syntax sugar for `fd_add` function, which allows user add last plot to default database without specifying the ggplot obj and database name. + +## Version 0.1.0 + +Initial release diff --git a/R/lib.R b/R/lib.R index 9c0e882..e0c3ccd 100644 --- a/R/lib.R +++ b/R/lib.R @@ -1,3 +1,28 @@ +.db_store <- function() { + .last_db = NULL + + list( + get = function() .last_db, + set = function(x) .last_db <<- x + ) +} +.db <- .db_store() + +#' Set the default ggfigdone database +#' +#' @param fdObj An object of class `fdObj` to be set as the default ggfigdone database. +#' @export +fd_set_db = function(fdObj) { + .db$set(fdObj) +} + +#' Get the default ggfigdone database +#' +#' @export +fd_get_db = function() { + .db$get() +} + #' Merge ggfigdone databases #' #' This function merges two ggfigdone databases. @@ -5,7 +30,7 @@ #' 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. #' #' @param from An object of class `fdObj` that will be merged from. -#' @param to An object of class `fdObj` that will be merged to. +#' @param to An object of class `fdObj` that will be merged to. The default value is the default ggfigdone database. #' @param replace A character string specifying the method to keep the figure with the unique ID. It can be either "updated_date" or "created_date". #' @return An object of class `fdObj` with the merged database. #' @export @@ -31,7 +56,7 @@ #' print(fo_merge) #' #' ## -fd_merge = function(from, to, replace = "updated_date") { +fd_merge = function(from, to = fd_get_db(), replace = "updated_date") { if (!inherits(from, "fdObj") || !inherits(to, "fdObj")) { stop("The 'from' and 'to' arguments should be objects of class 'fdObj'") @@ -102,7 +127,7 @@ fd_merge = function(from, to, replace = "updated_date") { #' ## Show the updated ggfigdone database #' print(fo) #' -fd_unique = function(fdObj, by = "updated_date") { +fd_unique = function(fdObj = fd_get_db(), by = "updated_date") { fd_update(fdObj) lock = lock(file.path(fdObj$dir, "/db.lock"), exclusive = TRUE) unique_names = unique(sapply(fdObj$env, function(x) x$name)) @@ -191,7 +216,7 @@ fd_update = function(fdObj_loc, do_lock = TRUE) { #' @param do_lock A logical value. If TRUE, the function will lock the database file when saving the data. #' #' @export -fd_save = function(fdObj, do_lock = TRUE) { +fd_save = function(fdObj = fd_get_db(), do_lock = TRUE) { message("Automatic saving the ggfigdone data to the disk ...") if (do_lock) { lock = lock(file.path(fdObj$dir, "/db.lock"), exclusive = TRUE) @@ -287,6 +312,7 @@ fd_plot = function(fdObj, id, do_lock = TRUE) { #' @param 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. #' @param 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. #' @param ... Additional arguments to be passed to \code{\link[ggfigdone]{fd_load}} function. +#' @param set_default A logical value. If TRUE, the function will set the database as the default database. #' @return An object of class `fdObj`. #' @examples #' library(ggplot2) @@ -297,7 +323,7 @@ fd_plot = function(fdObj, id, do_lock = TRUE) { #' fd_init(db_dir, rm_exist = TRUE) #' #' @export -fd_init = function(dir, recursive = TRUE, rm_exist = FALSE, ...) { +fd_init = function(dir, recursive = TRUE, rm_exist = FALSE, set_default = TRUE, ...) { ## check if the dir is empty if (!dir.exists(dir)) { dir.create(dir, recursive = recursive) @@ -336,7 +362,7 @@ fd_init = function(dir, recursive = TRUE, rm_exist = FALSE, ...) { writeLines("v1", file.path(dir, "version.txt")) - fd_load(dir, ...) + fd_load(dir, set_default = set_default, ...) } #' Load the ggfigdone database @@ -346,6 +372,7 @@ fd_init = function(dir, recursive = TRUE, rm_exist = FALSE, ...) { #' @param dir A character string representing the directory path. #' @param 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 \code{\link[ggfigdone]{fd_save}} function. +#' @param set_default A logical value. If TRUE, the function will set the database as the default database. #' @return An object of class `fdObj`. #' @examples #' library(ggplot2) @@ -357,7 +384,7 @@ fd_init = function(dir, recursive = TRUE, rm_exist = FALSE, ...) { #' fd_load(db_dir) #' #' @export -fd_load = function(dir, auto_database_upgrade = TRUE) { +fd_load = function(dir, auto_database_upgrade = TRUE, set_default = TRUE) { ## Check if the directory exists if (!dir.exists(dir)) { stop("Directory does not exist") @@ -399,6 +426,9 @@ fd_load = function(dir, auto_database_upgrade = TRUE) { class(obj) = "fdObj" + if (set_default) { + fd_set_db(obj) + } obj } @@ -437,7 +467,7 @@ fd_load = function(dir, auto_database_upgrade = TRUE) { #' print(fo) #' #' @export -fd_add = function(g, name, fdObj, +fd_add = function(name, g = last_plot(), fdObj = fd_get_db(), width = 10, height = 10, units = "cm", @@ -524,7 +554,7 @@ print.fdObj = function(x, ...) { #' @param fdObj An instance of the `fdObj` class. #' @return A List/data.frame containing the figures along with their respective parameters. #' @export -fd_ls = function(fdObj) { +fd_ls = function(fdObj = fd_get_db()) { fd_update(fdObj) lapply(names(fdObj$env), function(id) { @@ -548,7 +578,7 @@ fd_ls = function(fdObj) { #' @rdname fd_ls #' @export -fd_df = function(fdObj) { +fd_df = function(fdObj = fd_get_db()) { fd_update(fdObj) data.table::rbindlist(lapply(names(fdObj$env), function(id) { plot_labels = fdObj$env[[id]]$g_updated$labels @@ -576,7 +606,7 @@ fd_df = function(fdObj) { #' @param id A character string representing the figure ID. #' @param fdObj An object of class `fdObj`. #' @export -fd_rm = function(id, fdObj) { +fd_rm = function(id, fdObj = fd_get_db()) { fd_update(fdObj) if (id %in% names(fdObj$env)) { lock = lock(file.path(fdObj$dir, "/db.lock"), exclusive = TRUE) @@ -600,7 +630,7 @@ fd_rm = function(id, fdObj) { #' @param fdObj An object of class `fdObj` #' @return A character string of the status #' @export -fd_update_fig = function(id, expr, fdObj) { +fd_update_fig = function(id, expr, fdObj = fd_get_db()) { return_val = NULL lock = lock(file.path(fdObj$dir, "/db.lock"), exclusive = TRUE) fd_update(fdObj, do_lock = FALSE) @@ -641,7 +671,7 @@ fd_update_fig = function(id, expr, fdObj) { #' @export fd_canvas = function( id, - fdObj, + 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, diff --git a/README.md b/README.md index f433bbd..51b74e0 100644 --- a/README.md +++ b/README.md @@ -33,22 +33,28 @@ remotes::install_github("wenjie1991/ggfigdone") First, you need to initialize the database and add figures to it. +Next time, you only need to load the database to add more figures or update the existing figures. + ```r library(ggfigdone) +library(ggplot2) ## Initial ggfigdone database using `fd_init` ## The database location is `./fd_dir` +## Load existing database using `fd_load("./fd_dir")` fo = fd_init("./fd_dir") ## 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, overwrite = T) +## The last plot is added to the last initialized/loaded database +fd_add(name = "fig1") ## Add the same figure with a different name -fd_add(g = g, name = "fig2", fo, overwrite = T) +## You can specify the ggplot object and database name +fd_add(g = g, name = "fig2", fo) ## The hard disk database is automatically updated, no need to save operations. ``` @@ -58,13 +64,11 @@ fd_add(g = g, name = "fig2", fo, overwrite = T) Then you can start the server and open the browser to manage the figures. ```r -## Load the database -fo = fd_load("./fd_dir") - -## Start the server and open the browser +## To start the server, provide the database location fd_server("./fd_dir") -``` +## Open the browser and go to http://localhost:8080/index.html +``` ## Contribution diff --git a/man/fd_add.Rd b/man/fd_add.Rd index 659cf5a..fe6940f 100644 --- a/man/fd_add.Rd +++ b/man/fd_add.Rd @@ -5,11 +5,11 @@ \title{Add a ggplot object to the ggfigdone database} \usage{ fd_add( - g, name, - fdObj, - width = 5, - height = 5, + g = last_plot(), + fdObj = fd_get_db(), + width = 10, + height = 10, units = "cm", dpi = 200, overwrite = F, @@ -17,10 +17,10 @@ fd_add( ) } \arguments{ -\item{g}{A ggplot object.} - \item{name}{A character string representing the figure name.} +\item{g}{A ggplot object.} + \item{fdObj}{An object of class \code{fdObj}.} \item{width}{A numeric value specifying the width of the canvas.} diff --git a/man/fd_canvas.Rd b/man/fd_canvas.Rd index b94c5b6..5507878 100644 --- a/man/fd_canvas.Rd +++ b/man/fd_canvas.Rd @@ -6,7 +6,7 @@ \usage{ fd_canvas( id, - fdObj, + 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, diff --git a/man/fd_get_db.Rd b/man/fd_get_db.Rd new file mode 100644 index 0000000..9b79eb9 --- /dev/null +++ b/man/fd_get_db.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lib.R +\name{fd_get_db} +\alias{fd_get_db} +\title{Get the default ggfigdone database} +\usage{ +fd_get_db() +} +\description{ +Get the default ggfigdone database +} diff --git a/man/fd_init.Rd b/man/fd_init.Rd index 1fb6958..786cd50 100644 --- a/man/fd_init.Rd +++ b/man/fd_init.Rd @@ -4,7 +4,7 @@ \alias{fd_init} \title{Initiates the ggfigdone database} \usage{ -fd_init(dir, recursive = TRUE, rm_exist = FALSE, ...) +fd_init(dir, recursive = TRUE, rm_exist = FALSE, set_default = TRUE, ...) } \arguments{ \item{dir}{A character string specifying the directory path.} @@ -13,6 +13,8 @@ fd_init(dir, recursive = TRUE, rm_exist = FALSE, ...) \item{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.} +\item{set_default}{A logical value. If TRUE, the function will set the database as the default database.} + \item{...}{Additional arguments to be passed to \code{\link[ggfigdone]{fd_load}} function.} } \value{ diff --git a/man/fd_load.Rd b/man/fd_load.Rd index 05d1d21..a114af6 100644 --- a/man/fd_load.Rd +++ b/man/fd_load.Rd @@ -4,13 +4,15 @@ \alias{fd_load} \title{Load the ggfigdone database} \usage{ -fd_load(dir, auto_database_upgrade = TRUE) +fd_load(dir, auto_database_upgrade = TRUE, set_default = TRUE) } \arguments{ \item{dir}{A character string representing the directory path.} \item{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 \code{\link[ggfigdone]{fd_save}} function.} + +\item{set_default}{A logical value. If TRUE, the function will set the database as the default database.} } \value{ An object of class \code{fdObj}. diff --git a/man/fd_ls.Rd b/man/fd_ls.Rd index f4526a1..e4447e3 100644 --- a/man/fd_ls.Rd +++ b/man/fd_ls.Rd @@ -5,9 +5,9 @@ \alias{fd_df} \title{List the figures} \usage{ -fd_ls(fdObj) +fd_ls(fdObj = fd_get_db()) -fd_df(fdObj) +fd_df(fdObj = fd_get_db()) } \arguments{ \item{fdObj}{An instance of the \code{fdObj} class.} diff --git a/man/fd_merge.Rd b/man/fd_merge.Rd index ae14d90..42802c4 100644 --- a/man/fd_merge.Rd +++ b/man/fd_merge.Rd @@ -4,12 +4,12 @@ \alias{fd_merge} \title{Merge ggfigdone databases} \usage{ -fd_merge(from, to, replace = "updated_date") +fd_merge(from, to = fd_get_db(), replace = "updated_date") } \arguments{ \item{from}{An object of class \code{fdObj} that will be merged from.} -\item{to}{An object of class \code{fdObj} that will be merged to.} +\item{to}{An object of class \code{fdObj} that will be merged to. The default value is the default ggfigdone database.} \item{replace}{A character string specifying the method to keep the figure with the unique ID. It can be either "updated_date" or "created_date".} } diff --git a/man/fd_rm.Rd b/man/fd_rm.Rd index e8353e5..adda0b2 100644 --- a/man/fd_rm.Rd +++ b/man/fd_rm.Rd @@ -4,7 +4,7 @@ \alias{fd_rm} \title{Remove a figure} \usage{ -fd_rm(id, fdObj) +fd_rm(id, fdObj = fd_get_db()) } \arguments{ \item{id}{A character string representing the figure ID.} diff --git a/man/fd_save.Rd b/man/fd_save.Rd index 4cfee21..6333d16 100644 --- a/man/fd_save.Rd +++ b/man/fd_save.Rd @@ -4,7 +4,7 @@ \alias{fd_save} \title{Update the ggfigdone database changes \strong{to the disk}} \usage{ -fd_save(fdObj, do_lock = TRUE) +fd_save(fdObj = fd_get_db(), do_lock = TRUE) } \arguments{ \item{fdObj}{An object of class \code{fdObj}.} diff --git a/man/fd_set_db.Rd b/man/fd_set_db.Rd new file mode 100644 index 0000000..4b7bcfe --- /dev/null +++ b/man/fd_set_db.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lib.R +\name{fd_set_db} +\alias{fd_set_db} +\title{Set the default ggfigdone database} +\usage{ +fd_set_db(fdObj) +} +\arguments{ +\item{fdObj}{An object of class \code{fdObj} to be set as the default ggfigdone database.} +} +\description{ +Set the default ggfigdone database +} diff --git a/man/fd_unique.Rd b/man/fd_unique.Rd index 10f6d21..67cb6c7 100644 --- a/man/fd_unique.Rd +++ b/man/fd_unique.Rd @@ -4,7 +4,7 @@ \alias{fd_unique} \title{Keep figure name unique by removing older figures with the same name} \usage{ -fd_unique(fdObj, by = "updated_date") +fd_unique(fdObj = fd_get_db(), by = "updated_date") } \arguments{ \item{fdObj}{An object of class \code{fdObj}.} diff --git a/man/fd_update_fig.Rd b/man/fd_update_fig.Rd index 27caf91..452510a 100644 --- a/man/fd_update_fig.Rd +++ b/man/fd_update_fig.Rd @@ -4,7 +4,7 @@ \alias{fd_update_fig} \title{Update a figure using ggplot expression} \usage{ -fd_update_fig(id, expr, fdObj) +fd_update_fig(id, expr, fdObj = fd_get_db()) } \arguments{ \item{id}{A character string of the figure id}