是否可以重置R的实例?
EG。如果我使用了命令
x <- 1:10
plot(x, -x)
因此用x变量污染了系统。在这种状态下,我可以恢复到干净状态而不关闭R并再次启动它吗?
答案 0 :(得分:25)
您可以使用
从工作区中删除所有变量rm(list = ls())
您可以使用
“卸载”软件包detach(package:packagename)
编辑:
您可以使用
关闭所有图形设备graphics.off()
您可以使用CTRL+L
清除命令编辑器历史记录。
如果您使用Tinn-R作为编辑器,则会有一个“全部清除”按钮,用于清除工作区和命令编辑器历史记录,并关闭图形设备。 (它不会拆包。)
另一个编辑:
重置R需要做的另一件事是关闭所有打开的连接。留下开放式连接是非常糟糕的形式,所以这比必要性更belt and braces。 (您可以通过在晦涩的环境中打开连接来欺骗 close_all_connections
,但在这种情况下,您只能责怪自己。)
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()
正如Marek建议的那样,使用closeAllConnections
来做到这一点。
另一个编辑:
回应Ben关于重置选项的评论,这实际上有点棘手。最好的方法是在加载R时存储选项的副本,然后在此时重置它们。
#on R load
assign(".Options2", options(), baseenv())
#on reset
options(baseenv()$.Options2)
如果您在加载R时没有足够的预见性来设置它,那么您需要类似此功能的东西。
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)
)
)
该函数中的选项提供了一个vanilla R会话,因此您可能希望在之后获取Rprofile.site文件以自定义R您喜欢的方式。
source(file.path(R.home("etc"), "Rprofile.site"))
答案 1 :(得分:0)
基于@Richie Cotton的回应,评论等等,我认为值得考虑五个要素:
所以,这是我简单的ResetR
函数:
ResetR = function() {
# 1) Remove all objects
rm(list = ls(all=TRUE, envir = .GlobalEnv), envir = .GlobalEnv)
# 2) Unload non-native packages.
nat = c(".GlobalEnv", "package:datasets", "package:evd", "package:nortest", "package:MASS", "package:stats", "package:graphics", "package:grDevices", "package:utils", "package:methods", "Autoloads", "package:base")
p = search()
for (i in p) {
if (is.na(match(i, nat))) {
try(eval(parse(text=paste0("detach(", i, ", unload=T, force=T)"))), silent=T) # force=T is need in case package has dependency
}
}
# 3) Close all connections
try(closeAllConnections(), silent=T)
# 4) Restore default options
try(options(baseenv()$.Options2), silent=T) # Remember to put assign(".Options2", options(), baseenv()) at the bottom of YOUR_R_HOME\etc\Rprofile.site
# 5) Close all graphic devices
graphics.off()
}
HTH