In my app user can provide "subsetting" expression (which consist of mix: variables, names stored in variable - vide args_input
and subset_args_input
variables) as arguments to subset
/[
functions:
df_data <- data.frame("a" = c(1,1,2),
"b" = c(3,4,4),
"c" = c(5,6,7))
global_data <- list("column_to_extract" = c("b"))
args_input <- "list(df_data, df_data$a %in% c(1), global_data[['column_to_extract']])"
My expectation is to get as same result as executing:
do.call("[", list(df_data, df_data$a %in% c(1), global_data[['column_to_extract']]))
by executing this:
do.call("[", eval(parse(text=args_input)))
Also, I've tried something similar with subset
:
subset_args_input <- "list(quote(df_data), quote(a %in% c(1)), quote(global_data[['column_to_extract']]))"
do.call("subset", eval(parse(text=subset_args_input)))
- Both approaches work, but need
eval
+parse
, which are not safe in case user is allowed to pass any string. Question - could the same results be achieved withouteval
+parse
combo? - Could you (R users:) please have a look at above snippets - are there any unnecessary things/something against R philosophy?