Skip to content

Commit 779de2a

Browse files
committed
algorithm tweak
1 parent bebf28e commit 779de2a

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

R/parse_call.R

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,14 @@ parse_fun <- function() {
153153
}
154154

155155
obj <- .p$sf[[.p$pos$fun]][[.p$var]]
156-
157156
# Extract names from x argument
158-
nms <- all.names(call[[.p$var]])
159-
#nms <- unique(c(all.names(call[[.p$var]]), as.character(call[[.p$var]])))
160-
len <- length(call[[.p$var]])
157+
#nms <- all.names(call[[.p$var]])
158+
#len <- length(call[[.p$var]])
159+
160+
# experimental, 2025-02
161+
nms <- unique(c(all.names(call[[.p$var]]), as.character(call[[.p$var]])))
162+
nms <- setdiff(nms, c(.st_env$oper, ""))
163+
len <- length(nms)
161164

162165
if (is.data.frame(obj)) {
163166
if (len == 1) {
@@ -175,7 +178,7 @@ parse_fun <- function() {
175178
}
176179
} else {
177180
# x is data.frame, and call[[.p$var]] > 1
178-
# quite possibly a native-pipe command.
181+
# possibly a native-pipe command.
179182
# iterate over names of the call and extract objects
180183
var_pos <- which(names(call) == .p$var)
181184
obj_components <- list()
@@ -185,10 +188,7 @@ parse_fun <- function() {
185188
for (nm in nms) {
186189
obj_ <- dynGet(x = nm, ifnotfound = NULL, inherits = TRUE)
187190

188-
if (inherits(obj_, "function"))
189-
next
190-
191-
else if (inherits(obj_, c("data.frame", "list")))
191+
if (inherits(obj_, c("data.frame", "list")))
192192
obj_components[[nm]] <- list(class = class(obj_),
193193
content = ls(obj_),
194194
classes = lapply(obj_, class),
@@ -200,17 +200,33 @@ parse_fun <- function() {
200200
content = colnames(obj_),
201201
classes = mode(obj_))
202202

203-
else if (is.null(obj_))
203+
else if (grepl("\\w", nm))
204204
var_candidates %+=% nm
205205
}
206206

207207
if (length(var_candidates) > 0) {
208208
if (any(var_candidates %in% obj_components[[1]]$content)) {
209+
# Check whether the class of the obj_components[[1]] is
210+
# data.frame -- if not, the df might be in a list
211+
if ("data.frame" %in% obj_components[[1]]$class) {
209212
done <- upd_output("df_name", names(obj_components)[1])
210213
done <- upd_output("df_label",
211214
obj_components[[nm]]$label %||%
212215
NA_character_)
213-
if (done) return(TRUE)
216+
} else if ("list" %in% obj_components[[1]]$class) {
217+
ind1 <- which(var_candidates %in% obj_components[[1]]$content)
218+
#ind2 <- which(obj_components[[1]]$content == var_candidates[ind1])
219+
obj_cl <- obj_components[[1]]$classes[[var_candidates[ind1]]]
220+
if ("data.frame" %in% obj_cl) {
221+
dfnm <- paste(names(obj_components)[[1]], var_candidates[ind1],
222+
sep = "$")
223+
done <- upd_output("df_name", dfnm)
224+
if (is.data.frame(obj))
225+
done <- upd_output("df_label", label(obj))
226+
}
227+
}
228+
229+
if (done) return(TRUE)
214230
}
215231

216232
for (v in var_candidates) {
@@ -225,9 +241,12 @@ parse_fun <- function() {
225241
}
226242
}
227243
}
244+
228245
if (isTRUE(.p$do_return))
229246
return(TRUE)
247+
230248
} else if (is.atomic(obj)) {
249+
231250
if (len == 1) {
232251
if (all(c("x", "var") %in% names(call))) {
233252
if (deparse(call[[.p$var]]) != ".") {
@@ -608,6 +627,9 @@ deduce_names <- function() {
608627
candidates %+=% c(tested = nm)
609628
cand_class %+=% class(obj_)[1]
610629
names(cand_class)[length(cand_class)] <- nm
630+
} else if (is.list(obj_)) {
631+
candidates %+=% c(tested = nm)
632+
cand_class %+=% class(obj_)[1]
611633
}
612634
}
613635

@@ -739,6 +761,25 @@ parse_data_str <- function(str) {
739761
done <- upd_output("var_name", colnames(df_)[var_number])
740762
done <- upd_output("var_label", label(df_[[var_number]]))
741763
if (done) return(TRUE)
764+
} else {
765+
# Possibly a list containing data frame(s)
766+
# Try to get item name
767+
obj_ <- get_object(df_nm, "list")
768+
if (is.list(obj_)) {
769+
list_nm <- df_nm
770+
# get the index (number)
771+
ind <- sub(.st_env$re$num_index, "\\4", str, perl = TRUE)
772+
df_nm <- eval(parse(text = paste0("names(", list_nm, "[", ind, "])")))
773+
if (df_nm != "") {
774+
done <- upd_output("df_name", paste(list_nm, df_nm, sep = "$"))
775+
done <- upd_output("df_label", label(obj_[as.integer(ind)]))
776+
} else {
777+
# No name for the list element
778+
done <- upd_output("df_name", str)
779+
done <- upd_output("df_label", label(obj_[as.integer(ind)]))
780+
}
781+
if (done) return(TRUE)
782+
}
742783
}
743784
} else if (grepl(.st_env$re$neg_num_index, str, perl = TRUE)) {
744785
df_nm <- sub(.st_env$re$neg_num_index, "\\1", str, perl = TRUE)
@@ -1023,8 +1064,11 @@ get_object <- function(name, class) {
10231064

10241065
# fallback method 1
10251066
env <- try(pryr::where(name = name), silent = TRUE)
1026-
if (!inherits(env, "try-error"))
1027-
return(get(name, env, mode = "list"))
1067+
if (!inherits(env, "try-error")) {
1068+
obj <- get(name, env, mode = "list")
1069+
if (inherits(obj, class))
1070+
return(obj)
1071+
}
10281072

10291073
# fallback method 2
10301074
obj <- dynGet(x = name, inherits = TRUE, ifnotfound = NA)

0 commit comments

Comments
 (0)