Skip to content

Commit 3d9074c

Browse files
authored
Merge pull request #39 from JulienBlasco/path-argument
Evolution of the `path` argument
2 parents 89511db + 34bbba1 commit 3d9074c

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# tablexlsx (WIP)
22

3-
* (fix) `toxlsx()` no longer fails when the `object` argument is the result of a computation #18
4-
* provide meaningful error message if merge cols don't exist #20
3+
* (fix) `toxlsx()` no longer fails when the `object` argument is the result of a computation (#18)
4+
* provide meaningful error message if merge cols don't exist (#20)
5+
* `path` can now be supplied as a file name with full path instead of a directory name (#29)
56

67
# tablexlsx 1.0.0
78

R/toxlsx.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#' See examples gallery : <https://ddotta.github.io/tablexlsx/articles/aa-examples.html>
99
#'
1010
#' @param object data.frame to be converted to excel
11+
#' @param path path to save excel file (either a directory name or a file name with full path)
1112
#' @param tosheet list of sheet names for each element of object.
1213
#' If omitted, sheets are named by default "Sheet 1", "Sheet 2"...
1314
#' @param title list of title for each element of object
@@ -23,8 +24,7 @@
2324
#' @param mergecol list of character vectors that indicate the columns for which we want to merge the modalities
2425
#' @param bygroup list of character vectors indicating the names of the columns by which to group
2526
#' @param groupname list of booleans indicating whether the names of the grouping variables should be written
26-
#' @param path path to save excel file
27-
#' @param filename name for the excel file ("Export" by default)
27+
#' @param filename name for the excel file ("Export" by default). Ignored if `path` is a file name.
2828
#' @param asTable logical indicating if data should be written as an Excel Table (FALSE by default)
2929
#' @param automaticopen logical indicating if excel file should open automatically (FALSE by default)
3030
#'
@@ -40,6 +40,7 @@
4040
#' @export
4141
#'
4242
toxlsx <- function(object,
43+
path,
4344
tosheet = list(),
4445
title = list(),
4546
columnstyle = list("default" = NULL),
@@ -49,7 +50,6 @@ toxlsx <- function(object,
4950
mergecol = NULL,
5051
bygroup = list(),
5152
groupname = FALSE,
52-
path,
5353
filename = "Export",
5454
asTable = FALSE,
5555
automaticopen = FALSE) {
@@ -254,18 +254,22 @@ toxlsx <- function(object,
254254

255255
}
256256

257+
if (dir.exists(path)) {
258+
full_path <- file.path(path, paste0(filename,".xlsx"))
259+
} else {
260+
full_path <- path
261+
if (filename != "Export") warning("The supplied filename was overriden by the supplied path.")
262+
}
263+
257264
# Save workbook
258265
openxlsx::saveWorkbook(
259266
wb,
260-
file.path(
261-
path,
262-
paste0(filename,".xlsx")
263-
),
267+
full_path,
264268
overwrite = TRUE
265269
)
266270

267271
Sys.sleep(0.01)
268-
cli_alert_success("\nYour Excel file '{filename}.xlsx' is available in the folder '{path}'")
272+
cli_alert_success("\nYour Excel file '{basename(full_path)}' is available in the folder '{dirname(full_path)}'")
269273

270274
# Open workbook automatically if automaticopen is TRUE
271275
if (isTRUE(automaticopen)) {

man/toxlsx.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)