Skip to content

Commit

Permalink
Fix for #65
Browse files Browse the repository at this point in the history
  • Loading branch information
dbosak01 committed May 5, 2024
1 parent 1dcb7e6 commit e42ac6b
Show file tree
Hide file tree
Showing 19 changed files with 340 additions and 27 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
@@ -1,7 +1,7 @@
Package: fmtr
Type: Package
Title: Easily Apply Formats to Data
Version: 1.6.3
Version: 1.6.4
Author: David Bosak
Maintainer: David Bosak <dbosak01@gmail.com>
Description: Contains a set of functions that can be used to apply
Expand All @@ -13,7 +13,7 @@ Description: Contains a set of functions that can be used to apply
to create a user-defined format, similar to a SAS® user-defined format.
License: CC0
Encoding: UTF-8
URL: https://fmtr.r-sassy.org
URL: https://fmtr.r-sassy.org, https://github.com/dbosak01/fmtr
BugReports: https://github.com/dbosak01/fmtr/issues
Depends:
R (>= 3.6.0),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -44,9 +44,11 @@ export(is.flist)
export(is.format)
export(justification)
export(read.fcat)
export(read.flist)
export(value)
export(widths)
export(write.fcat)
export(write.flist)
import(common)
import(crayon)
import(stats)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
@@ -1,3 +1,8 @@
# fmtr 1.6.4

* Allow single item vector lookup.
* Added `read.flist()` and `write.flist()` functions.

# fmtr 1.6.3

* Improved documentation and examples.
Expand Down
4 changes: 2 additions & 2 deletions R/fapply.R
Expand Up @@ -200,7 +200,7 @@ fapply <- function(x, format = NULL, width = NULL, justify = NULL) {
if (is.vector(format) & is.list(format) == FALSE) {

# If format is a single string, call format_vector to deal with it
if (length(format) == 1)
if (length(format) == 1 & length(names(format)) == 0)
ret <- format_vector(x, format)
else {

Expand Down Expand Up @@ -513,7 +513,7 @@ flist_row_apply <- function(lst, vect) {

} else {

if (length(vect) %% length(lst) != 0 )
if (length(vect) %% length(lst$formats) != 0 )
message("NOTE: format list is not a multiple of input vector")

fmts <- rep(lst$formats, length.out = length(vect))
Expand Down
113 changes: 113 additions & 0 deletions R/flist.R
Expand Up @@ -375,6 +375,119 @@ as.data.frame.fmt_lst <- function(x, row.names = NULL, optional = FALSE, ...) {
}



# Read and Write flist ----------------------------------------------------



#' @title Write a formatting list to the file system
#' @description The \code{write.flist} function writes a formatting list
#' to the file system. By default, the formatting list will be written to the
#' current working directory, using the variable name as the file name. These
#' defaults can be overridden using the appropriate parameters. The catalog
#' will be saved with a file extension of ".flist".
#'
#' Note that the formatting list is saved as an RDS file. The ".flist" file
#' extension only serves to distinguish the format catalog from other RDS
#' files.
#' @param x The formatting list to write.
#' @param dir_path The directory path to write the catalog to. Default is the
#' current working directory.
#' @param file_name The name of the file to save the catalog as. Default is
#' the name of the variable that contains the formatting list. The ".flist" file
#' extension will be added automatically.
#' @return The full path of the saved formatting list.
#' @family flist
#' @examples
#' # Create formatting list
#' fl <- flist(f1 = "%5.1f",
#' f2 = "%6.2f",
#' type = "row")
#'
#' # Get temp directory
#' tmp <- tempdir()
#'
#' # Save formatting list to file system
#' pth <- write.flist(fl, dir_path = tmp)
#'
#' # Read from file system
#' fr <- read.flist(pth)
#'
#' # Create sample data
#' dat <- c(12.3844, 292.28432)
#'
#' # Use formats in the catalog
#' fapply(dat, fr)
#' # [1] " 12.4" "292.28"
#'
#' @export
write.flist <- function(x, dir_path = getwd(), file_name = NULL) {

if (is.null(file_name))
file_name <- deparse(substitute(x, env = environment()))

pth <- file.path(dir_path, paste0(file_name, ".flist"))


if (file.exists(pth))
file.remove(pth)

saveRDS(x, pth)


log_logr("Saved formatting list to '" %p% pth %p% "'")

return(pth)
}


#' @title Read a formatting list from the file system
#' @description The \code{read.flist} function reads a formatting list
#' from the file system. The function accepts a path to the formatting list,
#' reads the list, and returns it.
#'
#' Note that the formatting list is saved as an RDS file. The ".flist" file
#' extension only serves to distinguish the formatting list from other RDS
#' files.
#' @param file_path The path to the formatting list.
#' @return The formatting list as an R object.
#' @family flist
#' @examples
#' # Create formatting list
#' fl <- flist(f1 = "%5.1f",
#' f2 = "%6.2f",
#' type = "row")
#'
#' # Get temp directory
#' tmp <- tempdir()
#'
#' # Save formatting list to file system
#' pth <- write.flist(fl, dir_path = tmp)
#'
#' # Read from file system
#' fr <- read.flist(pth)
#'
#' # Create sample data
#' dat <- c(12.3844, 292.28432)
#'
#' # Use formats in the catalog
#' fapply(dat, fr)
#' # [1] " 12.4" "292.28"
#' @export
read.flist <- function(file_path) {

ret <- readRDS(file_path)

log_logr("Read formatting list from '" %p% file_path %p% "'")

if (log_output()) {
log_logr(ret)
print(ret)
}
return(ret)
}


#' @title Print a formatting list
#' @param x The formatting list to print
#' @param ... Follow-on parameters to the print function
Expand Down
4 changes: 3 additions & 1 deletion man/as.data.frame.fmt_lst.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/as.flist.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/as.flist.data.frame.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/as.flist.fcat.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/as.flist.list.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/as.flist.tbl_df.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/flist.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/is.flist.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/print.fmt_lst.Rd

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

59 changes: 59 additions & 0 deletions man/read.flist.Rd

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

69 changes: 69 additions & 0 deletions man/write.flist.Rd

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

0 comments on commit e42ac6b

Please sign in to comment.