-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.R
66 lines (57 loc) · 1.45 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#' Save a file to Excel
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' Save a tibble/data.frame to an excel `.xlsx` file. The file will automatically
#' with a save_dtime in the format of 20201109_132416 for November 11th, 2020
#' at 1:24:16PM.
#'
#' @details
#' - Requires a tibble/data.frame to be passed to it.
#'
#' @param .data The tibble/data.frame that you want to save as an `.xlsx` file.
#' @param .file_name the name you want to give to the file.
#'
#' @return
#' A saved excel file
#'
#' @export
#'
save_to_excel <- function(.data, .file_name) {
# Checks
if(!is.data.frame(.data)){
stop(call. = FALSE, "(.data) is not a data.frame/tibble. Please supply.")
}
if(is.na(.file_name)){
stop(call. = FALSE, "(.file_name) was not provided. Please supply.")
}
data_tbl <- tibble::as_tibble(.data)
# Save Dir
file_path <- utils::choose.dir()
# File Name
file_name <- .file_name
file_date <- base::Sys.Date() %>%
stringr::str_replace_all("-","")
file_time <- base::Sys.time() %>%
healthyR::sql_right(5) %>%
stringr::str_replace_all(":","")
file_name <- base::paste0(
"\\"
,file_name
,"_save_dtime_"
, file_date
, "_"
, file_time
,".xlsx"
)
f_pn <- base::paste0(
file_path
, file_name
)
# Save file
writexl::write_xlsx(
x = data_tbl
, path = f_pn
)
}