Reputation: 15
Ok, I am struggling a bit to find the proper way to describe my issue. Sorry if it is a dumb question, I am still learning.
I have a series of small data.frames named regularly "CountX_YYYY" (X stands for a specific case, YYYY stands for a given year). I have written a function that starts by getting a certain type (X) of them, binding them all and cleaning the id (year), like the one below:
library(dplyr)
analysis <- function(case_pattern){
cases <- mget(ls(pattern = case_pattern)) %>%
bind_rows(.id = "id") %>%
rename(year = id) %>%
mutate(year = str_sub(year,-4))
return(cases)
}
Where the case_pattern
used is, for example, "^Count1"
It is a fairly simple code, and if I run the body of the function manually, as done below, it works normally:
case_pattern <- "^Count1"
cases <- mget(ls(pattern = case_pattern)) %>%
bind_rows(.id = "id") %>%
rename(year = id) %>%
mutate(year = str_sub(year,-4))
However, if I do the exact same thing by calling the function:
case_pattern <- "^Count1"
analysis(case_pattern)
It throws out the following error:
Error: This tidyselect interface doesn't support predicates yet.
i Contact the package author and suggest using `eval_select()`.
(Below, the traceback, I dont know if it helps)
29.
stop(fallback)
28.
signal_abort(cnd)
27.
abort(c("This tidyselect interface doesn't support predicates yet.",
i = "Contact the package author and suggest using `eval_select()`."))
26.
as_indices_sel_impl(out, vars = vars, strict = strict, data)
25.
walk_data_tree(new, data_mask, context_mask)
24.
reduce_sels(node, data_mask, context_mask, init = init)
23.
eval_c(expr, data_mask, context_mask)
22.
walk_data_tree(expr, data_mask, context_mask)
21.
vars_select_eval(vars, expr, strict, data = x, name_spec = name_spec,
uniquely_named = uniquely_named, type = type)
20.
withCallingHandlers(expr, simpleError = function(cnd) {
abort(conditionMessage(cnd), parent = cnd)
})
19.
instrument_base_errors(expr)
18.
doTryCatch(return(expr), name, parentenv, handler)
17.
tryCatchOne(expr, names, parentenv, handlers[[1L]])
16.
tryCatchList(expr, classes, parentenv, handlers)
15.
tryCatch(instrument_base_errors(expr), vctrs_error_subscript = function(cnd) {
cnd$subscript_action <- subscript_action(type)
cnd$subscript_elt <- "column"
cnd_signal(cnd) ...
14.
with_subscript_errors(vars_select_eval(vars, expr, strict, data = x,
name_spec = name_spec, uniquely_named = uniquely_named, type = type),
type = type)
13.
eval_select_impl(x, names, {
{
sel
} ...
12.
rename_impl(NULL, .vars, quo(c(...)), strict = .strict)
11.
tidyselect::vars_rename(names(.data), !!!enquos(...))
10.
rename.data.frame(., year = id)
9.
rename(., year = id)
8.
function_list[[i]](value)
7.
freduce(value, `_function_list`)
6.
`_fseq`(`_lhs`)
5.
eval(quote(`_fseq`(`_lhs`)), env, env)
4.
eval(quote(`_fseq`(`_lhs`)), env, env)
3.
withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
2.
mget(ls(pattern = case_pattern)) %>% bind_rows(.id = "id") %>%
rename(year = id) %>% mutate(year = str_sub(year, -4))
1.
analysis(case_pattern)
I have not been able to identify/understand this error, but it seems to me that both ways should give the same output. Isn't?
Anyway, thanks in advance!
Upvotes: 1
Views: 79
Reputation: 887118
The issue seems to be stemming from the envir
in the ls
and mget
because mget
inside the function is looking for the objects inside that function env
library(dplyr)
analysis <- function(case_pattern) {
mget(ls(pattern = case_pattern)) %>%
bind_rows(.id = 'id')
}
Count1 <- head(mtcars)
Count2 <- head(mtcars)
analysis("^Count\\d+$")
#data frame with 0 columns and 0 rows
We can specify the envir
to .GlobalEnv
and it should work
analysis <- function(case_pattern) {
mget(ls(pattern = case_pattern, envir = .GlobalEnv), envir = .GlobalEnv) %>%
bind_rows(.id = 'id')
}
analysis("^Count\\d+$")
# id mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4...1 Count1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag...2 Count1 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710...3 Count1 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive...4 Count1 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#Hornet Sportabout...5 Count1 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
# ...
Upvotes: 3