从函数输入中接收列表元素的名称
通过一个.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
match.call
,但是,这不太可能与
map
之类的东西很好地工作,因为map
t通过使用$
符号传递所有命名元素来工作。实际上,如果您使用MAP
尝试此功能,则可以得到:这样做的更好方法是允许您的功能获取对象及其名称。例如:
如果您认为这似乎毫无意义,请考虑您可以清洁所有数据帧,并通过执行函数中使用它们的名称:
顺便说一句,直接以您建议的方式为全局环境分配一个变量,在用户中非常有问题 - 插入R代码,因为夸大用户的其他变量非常容易,并且对于R函数而言并不是预期的行为。
由
You could use
match.call
However, this is unlikely to work well with something like
map
, becausemap
doesn't work by passing all the named elements using$
notation. In fact, if you try this function withmap
you get:A better way to do this would be to allow your function to take an object and its name. For example:
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:
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)