Skip to content

Commit

Permalink
[Feat] Add fd_add syntax suggar by adding last plot to default ggfigdone
Browse files Browse the repository at this point in the history
database as default
  • Loading branch information
wenjie1991 committed Aug 14, 2024
1 parent d5b802a commit 724dc21
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 38 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-3100-2346"))
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ 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)
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)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
56 changes: 43 additions & 13 deletions R/lib.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
.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.
#' 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.
#'
#' @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
Expand All @@ -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'")
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -399,6 +426,9 @@ fd_load = function(dir, auto_database_upgrade = TRUE) {

class(obj) = "fdObj"

if (set_default) {
fd_set_db(obj)
}

obj
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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) {

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions man/fd_add.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/fd_canvas.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/fd_get_db.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/fd_init.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/fd_load.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/fd_ls.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/fd_merge.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/fd_rm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/fd_save.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/fd_set_db.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 724dc21

Please sign in to comment.