从函数输入中接收列表元素的名称

发布于 2025-02-08 19:40:56 字数 1122 浏览 1 评论 0原文

通过一个.yml文件,我将许多文件路径加载到不同的数据帧。 它们是在命名列表中结构的。在这里,一个非常简化的版本:

settings_list <- list(survey = list(df1 = "path",
                                df2 = "path2",
                                df3 = "path3",
                                df4 = "path4"))

结构:

> str(settings_list)
List of 1
  $ survey:List of 3
..$ df1: chr "path"
..$ df2: chr "path2"
..$ df3: chr "path3"
..$ df4: chr "path4"

由于DFS的结构没有太大差异,因此我写了一个功能,它需要一个数据框架并清理它。

foo_cleaning(path = settings_list$survey$df3)

然后,我将使用purrr :: map()在settings_list $调查的所有元素上循环

我要添加到函数的内容是将清洁的DF保存为单独的对象,并从列表到全局env的名称。 (因此,在这种情况下,一个名为“ DF3”的对象)。我知道,我可以简单地将它们全部存储在列表中,但是由于不同的原因,我也想在我的功能中包含此选项。

我基本上具有实施的代码, 但是,我只是在努力从功能输入中提取正确的名称。

names(settings_list$survey$df3)
NULL #desired output: "df3"

在将名称应用于列表的较高级别时,将为我提供名称的整个向量,但 但是,我如何仅提取与settings_list $ usize $ df3相对应的名称?

names(settings_list$survey)
[1] "df1" "df2" "df3" "df4"

另外,如何将函数input settings_list $ usize $ df3转换为字符串,以便可以使用Regex提取其最后一部分?

Via a .yml file I load in a number of file paths to different data frames.
They are structured in a named list. Here a very simplified version:

settings_list <- list(survey = list(df1 = "path",
                                df2 = "path2",
                                df3 = "path3",
                                df4 = "path4"))

With the structure:

> str(settings_list)
List of 1
  $ survey:List of 3
..$ df1: chr "path"
..$ df2: chr "path2"
..$ df3: chr "path3"
..$ df4: chr "path4"

As the dfs don't differ too much in their structure, I have written a function, that takes a single data frame and cleans it.

foo_cleaning(path = settings_list$survey$df3)

I would then use purrr::map() to loop over all elements of settings_list$survey

What I would like to add to the function is the option to save the cleaned df as a separate object with the name from the list to the global env. (so in this case an object named "df3"). I know, I could simply store them all in a list, but for different reasons, I would also like to include this option in my function.

I basically have the code for this implemented,
however, I'm only struggling to extract the correct name from the function input.

names(settings_list$survey$df3)
NULL #desired output: "df3"

While applying names to the higher level of the list would give me the whole vector of names,
but how could I extract only the name that corresponds to settings_list$survey$df3?

names(settings_list$survey)
[1] "df1" "df2" "df3" "df4"

Alternatively, how could I convert the function input settings_list$survey$df3 as a string so that I could use regex to extract the last part of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2025-02-15 19:40:56

可以使用match.call

foo_cleaning <- function(x) {
  y <- as.list(match.call())$x
  result <- as.character(y)[length(as.character(y))]
  cat("Item", result, "requested")
}

foo_cleaning(settings_list$survey$df3)
#> Item df3 requested

但是,这不太可能与map之类的东西很好地工作,因为map t通过使用$符号传递所有命名元素来工作。实际上,如果您使用MAP尝试此功能,则可以得到:

purrr:::map(settings_list$survey, foo_cleaning)
#> Item i requested
#> Item i requested
#> Item i requested
#> Item i requested

这样做的更好方法是允许您的功能获取对象及其名称。例如:

foo_cleaning <- function(x, x_nm) {
  cat("Item", x_nm, "requested\n")
  # Do cleaning with x
}

如果您认为这似乎毫无意义,请考虑您可以清洁所有数据帧,并通过执行函数中使用它们的名称:

purrr::map2(settings_list$survey, names(settings_list$survey), foo_cleaning)
#> Item df1 requested
#> Item df2 requested
#> Item df3 requested
#> Item df4 requested

顺便说一句,直接以您建议的方式为全局环境分配一个变量,在用户中非常有问题 - 插入R代码,因为夸大用户的其他变量非常容易,并且对于R函数而言并不是预期的行为。

You could use match.call

foo_cleaning <- function(x) {
  y <- as.list(match.call())$x
  result <- as.character(y)[length(as.character(y))]
  cat("Item", result, "requested")
}

foo_cleaning(settings_list$survey$df3)
#> Item df3 requested

However, this is unlikely to work well with something like map, because map doesn't work by passing all the named elements using $ notation. In fact, if you try this function with map you get:

purrr:::map(settings_list$survey, foo_cleaning)
#> Item i requested
#> Item i requested
#> Item i requested
#> Item i requested

A better way to do this would be to allow your function to take an object and its name. For example:

foo_cleaning <- function(x, x_nm) {
  cat("Item", x_nm, "requested\n")
  # Do cleaning with x
}

If you think this seems pointless, consider that you can clean all the data frames and have their names used inside the function by doing:

purrr::map2(settings_list$survey, names(settings_list$survey), foo_cleaning)
#> Item df1 requested
#> Item df2 requested
#> Item df3 requested
#> Item df4 requested

Incidentally, directly assigning a variable to the global environment in the way you suggest is very problematic in user-facing R code, as it would be very easy to over-write a user's other variables, and is not expected behaviour for an R function.

Created on 2022-06-17 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文