Is it possible to reset an instance of R?
Eg. if I have used the commands
x <- 1:10
plot(x, -x)
And thus polluted the system with the x variable. In this state can I then revert back to a clean state without shutting R down and launching it again?
You can remove all variables from your workspace using
rm(list = ls())
You can 'unload' packages with
detach(package:packagename)
EDIT:
You can close all graphics devices with
graphics.off()
You can clear the command editor history with CTRL+L
.
If you use Tinn-R as your editor, there is a 'Clear all' button, which clears your workspace and command editor history, and closes graphics devices. (It does not detach packages.)
ANOTHER EDIT:
One other thing that you would have to do to reset R is to close all open connections. It is incredibly bad form to leave open connections lying about, so this is more belt and braces than a necessity. (You can probably fool close_all_connections
by opening connections in obscure environments, but in that case you have only yourself to blame.)
is.connection <- function(x) inherits(x, "connection")
get_connections <- function(envir = parent.frame())
{
Filter(is.connection, mget(ls(envir = envir), envir = envir))
}
close_all_connections <- function()
{
lapply(c(sys.frames(), globalenv(), baseenv()),
function(e) lapply(get_connections(e), close))
}
close_all_connections()
As Marek suggests, use closeAllConnections
to do this.
ANOTHER EDIT:
In response to Ben's comment about resetting options, that's actually a little bit tricky. the best way to do it would be to store a copy of your options when you load R, and then reset them at this point.
#on R load
assign(".Options2", options(), baseenv())
#on reset
options(baseenv()$.Options2)
If you aren't foresighted enough to set this up when you load R, then you need something like this function.
reset_options <- function()
{
is_win <- .Platform$OS.type == "windows"
options(
add.smooth = TRUE,
browserNLdisabled = FALSE,
CBoundsCheck = FALSE,
check.bounds = FALSE,
continue = "+ ",
contrasts = c(
unordered = "contr.treatment",
ordered = "contr.poly"
),
defaultPackages = c(
"datasets",
"utils",
"grDevices",
"graphics",
"stats",
"methods"
),
demo.ask = "default",
device = if(is_win) windows else x11,
device.ask.default = FALSE,
digits = 7,
echo = TRUE,
editor = "internal",
encoding = "native.enc",
example.ask = "default",
expressions = 5000,
help.search.types = c("vignette", "demo", "help"),
help.try.all.packages = FALSE,
help_type = "text",
HTTPUserAgent = with(
R.version,
paste0(
"R (",
paste(major, minor, sep = "."),
" ",
platform,
" ",
arch,
" ",
os,
")"
)
),
internet.info = 2,
keep.source = TRUE,
keep.source.pkgs = FALSE,
locatorBell = TRUE,
mailer = "mailto",
max.print = 99999,
menu.graphics = TRUE,
na.action = "na.omit",
nwarnings = 50,
OutDec = ".",
pager = "internal",
papersize = "a4",
pdfviewer = file.path(R.home("bin"), "open.exe"),
pkgType = if(is_win) "win.binary" else "source",
prompt = "> ",
repos = c(
CRAN = "@CRAN@",
CRANextra = "http://www.stats.ox.ac.uk/pub/RWin"
),
scipen = 0,
show.coef.Pvalues = TRUE,
show.error.messages = TRUE,
show.signif.stars = TRUE,
str = list(
strict.width = "no",
digits.d = 3,
vec.len = 4
),
str.dendrogram.last = "`",
stringsAsFactors = TRUE,
timeout = 60,
ts.eps = 1e-05,
ts.S.compat = FALSE,
unzip = "internal",
useFancyQuotes = TRUE,
verbose = FALSE,
warn = 0,
warning.length = 1000,
width = 80,
windowsTimeouts = c(100, 500)
)
)
The options in that function provide a vanilla R session so you might wish to source your Rprofile.site file afterwards to customise R how you like it.
source(file.path(R.home("etc"), "Rprofile.site"))
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