简体   繁体   English

如何检查当前会话 R 中加载的文件 .rdata 的名称

[英]How to check name of file .rdata loaded in current session R

I have a folder with many *.rdata files, by clicking on them I directly open a workspace.我有一个包含许多 *.rdata 文件的文件夹,通过单击它们可以直接打开一个工作区。 Because I open many of them at the same time I would like to know the name of .rdata file associated with each R console/workspace.因为我同时打开了很多,所以我想知道与每个 R 控制台/工作区关联的 .rdata 文件的名称。 Is there a way to display the .rdata filename in the windows top bar, or at least check it with a command?有没有办法在窗口顶部栏中显示 .rdata 文件名,或者至少用命令检查它?

I don't think there's an automatic or easy way to find out. 我认为没有自动或简便的方法可以找出答案。 But you can try to find the likely culprits assuming that (a) each .Rdata file contains something unique variable(s) names, and (b) loading each file (temporarily) won't be time-prohibitive. 但是,您可以尝试假设(a)每个.Rdata文件包含一些唯一变量名称,并且(b)(临时)加载每个文件不会造成时间限制,来找出可能的罪魁祸首

A quick setup: 快速设置:

iris2 <- iris
mtcars2 <- mtcars
save(mtcars2, file="~/Downloads/mtcars.Rdata")
save(iris2, file="~/Downloads/iris.Rdata")

I clicked on mtcars.Rdata , and up came RStudio. 我单击mtcars.Rdata ,然后出现RStudio。

ls()
# [1] "mtcars2"         

Now the "hack": 现在的“ hack”:

list_of_vars <- sapply(list.files(pattern = "*.Rdata"),
  function(f) {
    e <- new.env(parent = emptyenv())
    load(f, envir = e)
    ls(envir = e)
  }, simplify = FALSE)
list_of_vars
# $iris.Rdata
# [1] "iris2"
# $mtcars.Rdata
# [1] "mtcars2"

Filter(function(x) all(exists(x)), list_of_vars)
# $mtcars.Rdata
# [1] "mtcars2"

This suggests that the file I double-clicked on is mtcars.Rdata . 这表明我双击的文件是mtcars.Rdata (This is by far neither robust nor fool-proof. If you have variable name commonality, you may be out of luck.) (到目前为止,这既不健壮也不是万无一失。如果您具有可变的名称通用性,则可能不走运。)

Update : 更新

Since your .Rdata files have similar or identical variable names (if not contents), then you can adapt the above technique to check if that objects themselves are identical, not just the presence of the variable names. 由于您的.Rdata文件具有相似或相同的变量名(如果不包含内容),则可以采用上述技术来检查对象本身是否相同,而不仅仅是变量名的存在。

New setup: 新设置:

mtcars2 <- mtcars
save(mtcars2, file="mtcars1.Rdata")
mtcars2$mpg[1] <- 21.1
save(mtcars2, file="mtcars2.Rdata")

Check contents: 检查内容:

VERBOSE <- TRUE
vars_equal <- sapply(list.files(pattern = "*.Rdata"),
                     function(f) {
                       if (VERBOSE) message(f)
                       e <- new.env(parent = emptyenv())
                       load(f, envir = e)
                       all(sapply(names(e), function(varname) exists(varname, envir = .GlobalEnv) && identical(e[[varname]], get(varname, envir = .GlobalEnv))))
                     })
vars_equal
# mtcars1.Rdata mtcars2.Rdata 
#         FALSE          TRUE 

If your objects are large then this will result in a momentary spike in memory usage. 如果您的对象很大,则将导致内存使用量的瞬时峰值。 As soon as R garbage collects, all of the temporary environments (and therefore the objects within each .Rdata file) created inside the outer sapply should be cleared. 一旦R垃圾回收, sapply应该清除在外部sapply内部创建的所有临时环境(以及每个.Rdata文件中的对象)。 (This could easily be cleaned up, not just for memory management but also just cleaner more robust execution. I'm not claiming coding-excellence in this :-) (这可以很容易地清理,不仅用于内存管理,而且还可以清理更鲁棒的执行。我在此不要求编码出色:-)

Good question!好问题! I'm asking this myself too!我自己也在问这个! Unfortunatelly, I don't know of a better solution than to parse it from the command-line arguments with which the R process was started:不幸的是,我不知道比从启动 R 进程的命令行参数解析它更好的解决方案:

commandArgs()
# [1] "C:\\Program Files\\R\\R-4.0.4\\bin\\x64\\RGui.exe"                                    
# [2] "--workspace=D:\\tomas\\ces\\gen_dat-full_test-model3,N=2000,seed=123,tr=0.6xAll.Rdata"

pat <- "^--workspace="
sub(pat, "", grep(pat, commandArgs(), value = TRUE))
# [1] "D:\\tomas\\ces\\gen_dat-full_test-model3,N=2000,seed=123,tr=0.6xAll.Rdata"

Definitely not an ideal solution (I came here to look for a better one), but since noone has posted better solution yet, I'm posting this.绝对不是一个理想的解决方案(我来这里是为了寻找更好的解决方案),但是由于还没有人发布更好的解决方案,所以我发布了这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM