Introduction:
I have an RStudio project where I'm researching (fairly) big data sets. Though I'm trying to keep global environment clean, after some time it becomes filled with huge objects.
Problem:
RStudio always refreshes Environment pane after debugging (probably iterates global environment and calls summary()
on each object), and it takes tens of seconds on my global environment. Although the refresh itself is async, R session is busy and you must wait for it to finish before you can continue working. That makes debugging very annoying. And there's no way I know of to disable Environment pane in RStudio.
Question:
Can someone suggest any beautiful workaround of that? I see following possibilities:
I'm working on reproducible example now, but it's not clear which objects causing the issue.
I've emailed RStudio support about that issue some time ago, but didn't get any answer yet.
Using rm() command: When you want to clear a single variable from the R environment you can use the “rm()” command followed by the variable you want to remove. variable: that variable name you want to remove.
You can do both by restarting your R session in RStudio with the keyboard shortcut Ctrl+Shift+F10 which will totally clear your global environment of both objects and loaded packages.
RStudio by default displays four panes: Console, Source Code, Environment/History, and Files. You can rearrange them by going to View -> Panes -> Pane Layout. You can add and remove tabs from panes by going to View and selecting/deselecting tab options listed at the bottom.
If you use RStudio, you can just set the option "Global Options" --> "Save workspace as . RData on exit" to "Always", and the environment will be saved automatically and loaded the next time you open the same project.
While it's not yet available in a public release of RStudio, the v1.3 daily builds of RStudio allow you to disable the automatic-updates of the environment pane:
Selecting Manual Refresh Only
would disable the automatic refresh of the environment pane.
I can reproduce the problem with lots of small nested list variables.
# Populate global environment with lots of nested list variables
invisible(
replicate(
1000,
assign(
paste0(sample(letters, 10, replace = TRUE), collapse = ""),
list(a = 1, b = list(ba = 2.1, bb = list(bba = 2.21, bbb = 2.22))),
envir = globalenv()
)
)
)
f <- function() browser()
f() # hit ENTER in the console once you hit the browser
This suggests that the problem is RStudio running its equivalent of ls.str()
on the global environment.
I suspect that the behaviour is implemented in one of the functions listed by ls("tools:rstudio", all.names = TRUE)
, but I'm not sure which. If you find it, you can override it.
Alternatively, your best bet is to rework your code so that you aren't assigning so many variables in the global environment. Wrap most of your code into functions (so most variables only exist for the lifetime of the function call). You can also define a new environment
e <- new.env(parent = globalenv())
Then assign all your results inside e
. That way the refresh only takes a few microseconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With