Skip to content

Commit

Permalink
Data NULL #173.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbosak01 committed Nov 5, 2024
1 parent a610957 commit 6086a7d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 11 deletions.
39 changes: 32 additions & 7 deletions R/datastep.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ e$output <- list()
#' other techniques for processing your data. While there is no
#' built-in restriction on the number of rows, performance of the
#' \code{datastep} can become unacceptable with a large number of rows.
#' @param data The data to step through.
#' @param data The data to step through. This parameter is required. Valid
#' values are a data frame or a NULL. In the case of a NULL, you must include
#' at least one \code{output()} function in the steps.
#' @param steps The operations to perform on the data. This parameter is
#' specified as a set of R statements contained within
#' curly braces. If no steps are desired, pass empty curly braces.
Expand Down Expand Up @@ -530,6 +532,26 @@ e$output <- list()
#' # 2 A05 23 F G01 Group1 1 1
#' # 3 A02 20 M G02 Group2 1 1
#' # 4 A04 11 M G03 Group3 1 1
#'
#' # Example #8: Data NULL
#'
#' # Create new dataset using output() functions.
#' res <- datastep(NULL,
#' {
#' ID <- 10
#' Item <- "Pencil"
#' output()
#'
#' ID <- 20
#' Item <- "Scissors"
#' output()
#' })
#'
#' # View results
#' res
#' # ID Item
#' # 1 10 Pencil
#' # 2 20 Scissors
#' @import dplyr
#' @export
datastep <- function(data, steps, keep = NULL,
Expand All @@ -548,14 +570,18 @@ datastep <- function(data, steps, keep = NULL,
log = TRUE,
subset = NULL) {

if (!"data.frame" %in% class(data))
stop("input data must be inherited from data.frame")
if (!is.null(data)) {
if (!"data.frame" %in% class(data))
stop("input data must be inherited from data.frame")
}


if (!is.null(retain)) {
if (!"list" %in% class(retain))
stop("retain parameter value must be of class 'list'")

} else {
data <- data.frame()
}

if (!is.null(attrib)) {
Expand Down Expand Up @@ -587,16 +613,15 @@ datastep <- function(data, steps, keep = NULL,
merge_by <- tryCatch({if (typeof(merge_by) %in% c("character", "NULL")) merge_by else omby},
error = function(cond) {omby})


# Capture number of starting columns
startcols <- ncol(data)

# Put code in a variable for safe-keeping
code <- substitute(steps, env = environment())

# Determine if there is an output function
hout <- has_output(deparse(code))

# Capture number of starting columns
startcols <- ncol(data)

# Give warning if there are no rows and no output()
if (hout == FALSE & nrow(data) == 0) {
warning("Input dataset has no rows.")
Expand Down
2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ articles:
libr-faq: libr-faq.html
libr-management: libr-management.html
libr: libr.html
last_built: 2024-11-05T01:57Z
last_built: 2024-11-05T03:56Z
urls:
reference: https://libr.r-sassy.org/reference
article: https://libr.r-sassy.org/articles
26 changes: 24 additions & 2 deletions docs/reference/datastep.html

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

24 changes: 23 additions & 1 deletion man/datastep.Rd

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

35 changes: 35 additions & 0 deletions tests/testthat/test-datastep.R
Original file line number Diff line number Diff line change
Expand Up @@ -1833,3 +1833,38 @@ test_that("ds52: subset clause works.", {
expect_equal(mean(df$cyl), 8)

})


test_that("ds36: output works with NULL dataset.", {

d1 <- datastep(NULL, {

bork <- 1
fork <- "one"
output()

bork <- 2
fork <- "two"
output()

})

# print(d1)
# print(attributes(d1))
# print(attributes(d1$fork))
d1

expect_equal(nrow(d1), 2)
expect_equal(ncol(d1), 2)
expect_equal(names(d1), c("bork", "fork"))
expect_equal(d1[[1, 1]], 1)
# if ("factor" %in% class(d1[[2, 2]]))
# expect_equal(d1[[2, 2]], 2)
# else
# expect_equal(d1[[2, 2]], "two")

expect_error(datastep(NULL, {a <- 1 }))

})


0 comments on commit 6086a7d

Please sign in to comment.