From 5e171f3020810cbada1a7cf68ca3822832ea5e23 Mon Sep 17 00:00:00 2001 From: Julien Blasco Date: Mon, 10 Jun 2024 18:57:05 +0200 Subject: [PATCH 1/3] Put path argument in second position --- R/toxlsx.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/toxlsx.R b/R/toxlsx.R index 3afde29..76818fe 100644 --- a/R/toxlsx.R +++ b/R/toxlsx.R @@ -8,6 +8,7 @@ #' See examples gallery : #' #' @param object data.frame to be converted to excel +#' @param path path to save excel file #' @param tosheet list of sheet names for each element of object. #' If omitted, sheets are named by default "Sheet 1", "Sheet 2"... #' @param title list of title for each element of object @@ -23,7 +24,6 @@ #' @param mergecol list of character vectors that indicate the columns for which we want to merge the modalities #' @param bygroup list of character vectors indicating the names of the columns by which to group #' @param groupname list of booleans indicating whether the names of the grouping variables should be written -#' @param path path to save excel file #' @param filename name for the excel file ("Export" by default) #' @param asTable logical indicating if data should be written as an Excel Table (FALSE by default) #' @param automaticopen logical indicating if excel file should open automatically (FALSE by default) @@ -40,6 +40,7 @@ #' @export #' toxlsx <- function(object, + path, tosheet = list(), title = list(), columnstyle = list("default" = NULL), @@ -49,7 +50,6 @@ toxlsx <- function(object, mergecol = NULL, bygroup = list(), groupname = FALSE, - path, filename = "Export", asTable = FALSE, automaticopen = FALSE) { From b41bbbe35173c740627bd1bffc358b3e6e5f32a0 Mon Sep 17 00:00:00 2001 From: Julien Blasco Date: Mon, 10 Jun 2024 19:08:14 +0200 Subject: [PATCH 2/3] If path is not a directory name, use it as file name For backward compatibility, the argument filename is used if path is a directory name. --- R/toxlsx.R | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/R/toxlsx.R b/R/toxlsx.R index 76818fe..55e226d 100644 --- a/R/toxlsx.R +++ b/R/toxlsx.R @@ -254,18 +254,22 @@ toxlsx <- function(object, } + if (dir.exists(path)) { + full_path <- file.path(path, paste0(filename,".xlsx")) + } else { + full_path <- path + if (filename != "Export") warning("The supplied filename was overriden by the supplied path.") + } + # Save workbook openxlsx::saveWorkbook( wb, - file.path( - path, - paste0(filename,".xlsx") - ), + full_path, overwrite = TRUE ) Sys.sleep(0.01) - cli_alert_success("\nYour Excel file '{filename}.xlsx' is available in the folder '{path}'") + cli_alert_success("\nYour Excel file '{basename(full_path)}' is available in the folder '{dirname(full_path)}'") # Open workbook automatically if automaticopen is TRUE if (isTRUE(automaticopen)) { From 34bbba1fc57e15330dadcf3a040b4b6a4f8d732e Mon Sep 17 00:00:00 2001 From: Julien Blasco Date: Mon, 10 Jun 2024 19:16:14 +0200 Subject: [PATCH 3/3] Update documentation and NEWS --- NEWS.md | 5 +++-- R/toxlsx.R | 4 ++-- man/toxlsx.Rd | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 56b77f2..08214cb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,8 @@ # tablexlsx (WIP) -* (fix) `toxlsx()` no longer fails when the `object` argument is the result of a computation #18 -* provide meaningful error message if merge cols don't exist #20 +* (fix) `toxlsx()` no longer fails when the `object` argument is the result of a computation (#18) +* provide meaningful error message if merge cols don't exist (#20) +* `path` can now be supplied as a file name with full path instead of a directory name (#29) # tablexlsx 1.0.0 diff --git a/R/toxlsx.R b/R/toxlsx.R index 55e226d..e0fd009 100644 --- a/R/toxlsx.R +++ b/R/toxlsx.R @@ -8,7 +8,7 @@ #' See examples gallery : #' #' @param object data.frame to be converted to excel -#' @param path path to save excel file +#' @param path path to save excel file (either a directory name or a file name with full path) #' @param tosheet list of sheet names for each element of object. #' If omitted, sheets are named by default "Sheet 1", "Sheet 2"... #' @param title list of title for each element of object @@ -24,7 +24,7 @@ #' @param mergecol list of character vectors that indicate the columns for which we want to merge the modalities #' @param bygroup list of character vectors indicating the names of the columns by which to group #' @param groupname list of booleans indicating whether the names of the grouping variables should be written -#' @param filename name for the excel file ("Export" by default) +#' @param filename name for the excel file ("Export" by default). Ignored if `path` is a file name. #' @param asTable logical indicating if data should be written as an Excel Table (FALSE by default) #' @param automaticopen logical indicating if excel file should open automatically (FALSE by default) #' diff --git a/man/toxlsx.Rd b/man/toxlsx.Rd index 6f3d965..a7f4b0c 100644 --- a/man/toxlsx.Rd +++ b/man/toxlsx.Rd @@ -6,6 +6,7 @@ \usage{ toxlsx( object, + path, tosheet = list(), title = list(), columnstyle = list(default = NULL), @@ -15,7 +16,6 @@ toxlsx( mergecol = NULL, bygroup = list(), groupname = FALSE, - path, filename = "Export", asTable = FALSE, automaticopen = FALSE @@ -24,6 +24,8 @@ toxlsx( \arguments{ \item{object}{data.frame to be converted to excel} +\item{path}{path to save excel file (either a directory name or a file name with full path)} + \item{tosheet}{list of sheet names for each element of object. If omitted, sheets are named by default "Sheet 1", "Sheet 2"...} @@ -48,9 +50,7 @@ If omitted, no footnote3} \item{groupname}{list of booleans indicating whether the names of the grouping variables should be written} -\item{path}{path to save excel file} - -\item{filename}{name for the excel file ("Export" by default)} +\item{filename}{name for the excel file ("Export" by default). Ignored if `path` is a file name.} \item{asTable}{logical indicating if data should be written as an Excel Table (FALSE by default)}