tests/attachLocally.R

library("R.utils")

# A named list
x <- list(a=1, b=base::letters)
keys <- attachLocally(x)
stopifnot(identical(keys, names(x)))
for (key in keys) stopifnot(identical(get(key), x[[key]]))


# A list with "empty" names
x <- list(a=1, b=base::letters, "noname", "another one")
keys <- attachLocally(x)
stopifnot(identical(keys, setdiff(names(x), "")))
for (key in keys) stopifnot(identical(get(key), x[[key]]))

# An environment
x <- list(a=1, b=base::letters)
env <- list2env(x)
stopifnot(identical(ls(envir=env), names(x)))
keys <- attachLocally(env)
stopifnot(identical(keys, ls(envir=env)))
for (key in keys) stopifnot(identical(get(key), env[[key]]))

# A data.frame
df <- data.frame(a=1, b=base::letters, stringsAsFactors=FALSE)
keys <- attachLocally(df)
stopifnot(identical(keys, names(df)))
for (key in keys) stopifnot(identical(get(key), df[[key]]))

Try the R.utils package in your browser

Any scripts or data that you put into this service are public.

R.utils documentation built on Nov. 18, 2023, 1:09 a.m.