-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
getParseData() - for first-file objects - PR#16756 thanks to Duncan M…
…urdoch git-svn-id: https://svn.r-project.org/R/trunk@84538 00db46b3-68df-0310-9c12-caf00c1e9a41
- Loading branch information
maechler
committed
Jun 14, 2023
1 parent
575840c
commit 990fe3d
Showing
11 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Package: parseDataEx | ||
Title: Example Showing PR_16756 | ||
Version: 0.1-0 | ||
Author: Martin Maechler, Kirill Mueller | ||
Maintainer: R Core <[email protected]> | ||
Description: Dummy Package Showing PR_16756. | ||
See <https://bugs.r-project.org/show_bug.cgi?id=16756>, | ||
also <https://rpubs.com/krlmlr/getParseData> | ||
License: GPL-3 | ||
KeepSource: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export(f, g, h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
f <- function() { | ||
if (FALSE) # never .. | ||
TRUE | ||
## i.e., we will always return an invisible() NULL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## a function w/o "{ .. }" .. this is all about keeping parse data: | ||
g <- function(x) # comment g() | ||
if(x) TRUE | ||
|
||
## with "{ .. }" | ||
h <- function(x) { # argument x | ||
if(is.atomic(x)) | ||
x # atomic vector | ||
else list(if(length(x)) x[[1]], # <-- "nothing" when length zero | ||
le = length(x), cl = class(x)) # something else | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
pkg <- "parseDataEx" | ||
|
||
options(keep.source.pkgs = TRUE, # default: FALSE | ||
keep.parse.data.pkgs = TRUE, # " " | ||
keep.parse.data = TRUE, keep.source = TRUE) # just in case | ||
|
||
library(pkg, character.only=TRUE) | ||
|
||
## This is *NOT* about parse data !! -------> see ./test-parseD.R <----- , using getParseData() | ||
|
||
stopifnot(exprs = { | ||
is.null(f()) | ||
identical(g(1), TRUE) | ||
is.null( g(0)) | ||
}) | ||
|
||
getSrcStuff <- function(x, unique=TRUE, full.names=FALSE, first=TRUE) { | ||
list(filename = getSrcFilename(x, full.names=full.names, unique=unique), | ||
dir = getSrcDirectory(x, unique=unique), | ||
srcref = getSrcref(x), | ||
loc = { | ||
whs <- eval(formals(getSrcLocation)$which) | ||
## sapply(): all integer(1); keep nms: | ||
sapply(whs, \(wh) getSrcLocation(x, which=wh, first=first)) | ||
}) | ||
} | ||
|
||
nms <- ls(paste0("package:", pkg)) | ||
|
||
srcObj <- lapply(setNames(,nms), get, envir=asNamespace(pkg)) | ||
srcAll <- lapply(srcObj, getSrcStuff) | ||
|
||
# shows all functions in the original formatting incl comments: | ||
(srcrefs <- lapply(srcAll, `[[`, "srcref")) | ||
(sfiles <- lapply(srcrefs, attr, "srcfile")) | ||
origs <- lapply(sfiles, `[[`, "original") | ||
|
||
stopifnot(exprs = { | ||
identical(sapply(srcAll, `[[`, "filename"), | ||
c(f = "a.R", g = "b.R", h = "b.R")) | ||
sapply(sfiles, is.environment) # all TRUE | ||
sapply(origs, is.environment) | ||
length(unique(origs)) == 1L # the same concatenated file | ||
class(orig <- origs[[1]]) == c("srcfilecopy", "srcfile") | ||
}) | ||
## class(.) does *not* contain "environment" ==> as.list() does *not* work: | ||
str(as.list.environment(orig)) | ||
|
||
str(srcAll) | ||
## (could test quite a bit more) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
pkg <- "parseDataEx" | ||
|
||
options(keep.source.pkgs = TRUE, # {default: FALSE} | ||
keep.parse.data.pkgs = TRUE, # {default: FALSE} | ||
keep.parse.data = TRUE, keep.source = TRUE) # just in case | ||
|
||
sessionInfo() | ||
library(pkg, character.only=TRUE) | ||
packageDescription(pkg) | ||
|
||
## now, the same 123 for all: the same *concatenated* *.R files --> the *same* 1 file | ||
nrow(f.pd <- getParseData(f)) # gave 0 in R <= 4.3.1 | ||
nrow(g.pd <- getParseData(g)) # was ok already | ||
nrow(h.pd <- getParseData(h)) # (ditto) | ||
|
||
head(f.pd) | ||
str(f.pd) | ||
(eq.f.g <- all.equal(f.pd, g.pd))# one difference: srcfile->filename | ||
stopifnot(exprs = { | ||
nrow(g.pd) > 120 | ||
identical(nrow(f.pd), nrow(g.pd)) | ||
identical(g.pd, h.pd) # because they are in the *same* source file ../R/b.R | ||
is.character(eq.f.g) | ||
length(eq.f.g) == 1 | ||
sapply(c("srcfile", "filename"), grepl, x = eq.f.g, fixed = TRUE) | ||
}) | ||
f_srcref <- getSrcref(f) | ||
f_srcfile <- attr(f_srcref, "srcfile") | ||
ls(f_srcfile) | ||
f_srcfile$original | ||
ls(f_srcfile$original) | ||
|
||
g_srcfile <- attr(getSrcref(g), "srcfile") | ||
|
||
(f.fn <- f_srcfile$filename) | ||
(g.fn <- g_srcfile$filename) | ||
|
||
basename3 <- function(f) sub(paste0("^", dirname(dirname(dirname(f)))), "", f) | ||
pkgR <- function(f) paste0("/",pkg,"/R/", f) | ||
|
||
basename3(attr(h.pd, "srcfile")$filename) | ||
|
||
stopifnot(exprs = { | ||
identical(attr(f.pd, "srcfile"), f_srcfile) | ||
is.environment(f_srcfile) | ||
basename3(f.fn) == pkgR("a.R") | ||
basename3(g.fn) == pkgR("b.R") | ||
is.environment(f_srcfile$original) | ||
"parseData" %in% names(f_srcfile$original) | ||
"parseData" %in% names(g_srcfile$original) | ||
## both `original` are identical {<==> the concatenated *.R files}: | ||
identical(f_srcfile$original, | ||
g_srcfile$original) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters