I have a folder with many *.rdata files, by clicking on them I directly open a workspace. 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. Is there a way to display the .rdata filename in the windows top bar, or at least check it with a command?
-
I doubt that there is such a thing as an "association" between an R console and an .RData file. To the best of my knowledge, loading an .RData file will simply load variables into the global environment, but not lead to a persistent connection between the R session and that file. But the command used to load the .RData file is stored in the command history. Therefore, you could display the .RData files that you loaded with `utils::history(reverse = TRUE, pattern = "load\\(")`. – Stibu Apr 15 '17 at 20:37
-
@Stibu thanks for the idea, but that command turns out empty when I try. Have you tried it yourself? – Herman Toothrot Apr 15 '17 at 20:50
-
I have tried it in RStudio and on the console. It only shows something, if you have loaded an .RData file at some point before. How are you loading the .RData files? – Stibu Apr 15 '17 at 21:00
-
@Stibu ok I see the issue, I am loading it by clicking on the .rdata file in explorer in Windows. – Herman Toothrot Apr 15 '17 at 23:54
-
That is what I have done, but under Linux. RStudio then opens and loads the .RData file by calling `load()` all by itself. And afterwards I can figure out, which file was loaded, using `history()`. But it is of course possible that this functions differently under Windows. Are you using RStudio, by the way? It is much more powerful than the GUI that comes with the R installation. – Stibu Apr 16 '17 at 06:44
-
@Stibu I have used Rstudio in the past but I don't like the multiple windows, I find it clunky how it manages the screen estate. So no, I don't use rstudio. – Herman Toothrot Apr 16 '17 at 11:46
2 Answers
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 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.
ls()
# [1] "mtcars2"
Now the "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
. (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.
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. (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 :-)

- 141,215
- 6
- 77
- 149
-
sorry but all my files I have the same variables in it, just different values, but the values are not different enough that I can tell the different rdata files. – Herman Toothrot Apr 16 '17 at 11:44
-
Instead of checking `exists`, you could adapt this to check equality of all values in the temporary `e` environment with the variables in `.GlobalEnv`. Still a hack, but since R doesn't try to remember it's "reason for opening" and the file connection is closed at the end of `load`, this may be your only option. – r2evans Apr 16 '17 at 14:44
-
-
I am sorry but I didn't accept the answer because it doesn't work for my case. I need to know which .rdata was loaded because my .rdata files have the same variables with thousands of values, most very long lists with the same length but slightly different values. So unless I can see directly which file was loaded there's no other way for me to tell them apart. Thank for the effort. – Herman Toothrot May 10 '17 at 10:02
-
So what about the updated code (from Apr 16) does not work? It checks variable *values* not just names. – r2evans May 10 '17 at 16:09
-
it just tells me values are different but it doesn't tell from which .rdata file they came from. – Herman Toothrot May 10 '17 at 16:13
-
So when you run my code above, you don't get a *named* `logical` vector as I showed above? Perhaps you could edit your question and include the code you've tried and the results. – r2evans May 10 '17 at 16:19
-
Your code it's not working because the files are named 'RData' and not Rdata. When I change your code to "*.RData" I get an error Error in get(varname, envir = .GlobalEnv) : object 'a' not found – Herman Toothrot May 11 '17 at 15:25
-
Rdata-vs-RData ... whatever. `object 'a' not found` clearly states that the variable named `a` does not exist in the `.GlobalEnv`, indicating two things to me: (1) your assumption (that all Rdata files contain exactly the variables (albeit with different values) and each R session has one of these loaded) is not universally true; and (2) we can expand my code to check for existence as well, combining my two checks. See my edit. If it doesn't work, I think you need to revisit your assumptions, which environments you think you are using, etc. – r2evans May 11 '17 at 15:57
-
I am not sure where this object 'a' comes from, I am just loading the function in R. I just changed rdata to RData and the function doesn't load anymore. – Herman Toothrot May 12 '17 at 09:59
-
I added two lines to the script so that you can see *which* file is demonstrating the error. With that information, I suggest you `e <- new.env(); load(thatfile, envir=e); ls.str(e);` to see if there is actually an `a` in that environment. (I know you say they are all identical, but sometimes things happen ...) – r2evans May 12 '17 at 15:25
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:
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"
So, to put the .Rdata file name to window title, you can put something like this into your R profile:
pat <- "^--workspace="
Rdata_fn <- sub(pat, "", grep(pat, commandArgs(), value = TRUE))
if (length(Rdata_fn)) utils::setWindowTitle(Rdata_fn)
(you need to put utils::
because the built-in package utils
is not loaded yet when the Rprofile is run, see e.g. here)
Definitely not an ideal solution, to parse out command line arguments (I came here to look for a better one), but since noone has posted better solution yet, I'm posting this.

- 57,621
- 49
- 238
- 373