从 R 中退出并重新启动干净的 R 会话?
有没有一种方法可以在 R 中创建一个别名,该别名将执行 q(),然后重新启动一个干净的 R 会话?
是的,我懒得输入 q()
然后输入字母 R
:)
Is there a way I can make an alias, within R, that will execute q()
and then restart a clean R session?
And yes, I am too lazy to type q()
and then the letter R
:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如果您在 RStudio 中:
命令/ctrl + Shift + F10
您也可以使用
If you're in RStudio:
command/ctrl + shift + F10
You can also use
我发现 .rs.restartR() 的工作方式类似于按 ctrl+shift+F10。但不要卸下包裹
I found that .rs.restartR() works similar to pressing ctrl+shift+F10. but dose not unload the packages
作为另一种选择,Emacs ESS (>= 16.10) 可以通过
inferior-ess-r-reload-hook
重新加载劣等 R 进程,该进程通过以下方式绑定到Cc Ce Cr
默认。As another alternative, Emacs ESS (>= 16.10) can reload the inferior R process via
inferior-ess-r-reload-hook
which is bound toC-c C-e C-r
by default.在寻找解决方案后,我根据此解决方案解决了我的问题 此处,使用 R 包 RDCOMClient。
一旦模拟按键 ctrl+shift+F10,下面的解决方案就可以在 RStudio (Windows) 中工作。
必须使用以下命令安装 RDCOMClient 软件包:
在 RStudio (Windows 10) 中模拟按键的代码为:
在上面代码的最后一行中,“ctrl”键由“^”表示,shift 键由'+'。有关此关键表示的所有解释均可 这里。
运行上面代码的最后一行后,RStudio 中的整个 R 会话将被重置,根据 @steadyfish 的评论。也就是说,它会删除当前会话中的所有数据,并卸载会话中所有已加载的包。
After looking for a solution to this, I solved my problem based on this solution here, using the R Package RDCOMClient.
The solution bellow just work within RStudio (Windows), once it simulates the keypresses ctrl+ shift + F10.
The RDCOMClient package must be installed with the command bellow:
The code to simulate the keypresses within RStudio (Windows 10) are:
In the last line in the code above, the 'ctrl' key is represented by '^' and the shift key by '+'. All the explanations for this key representaions are available here.
Just after running the last line of the code above, the whole R session in RStudio will be reset, according to @steadyfish's comment. That is, it removes all the data from current session and unload all the loaded packages in the session.
老帖子,但没有一个答案完全有效(对我来说,我使用的是Windows,没有测试过其他人),所以我将添加我的解决方案。我的一些术语可能偏离这里,但这应该能说明问题:
以上答案不太有效
此处提交的大多数答案都涉及使用
shell
或system
这不太有效,因为当他们打开新的 R 控制台并指示原始控制台关闭时,新控制台是在旧控制台的应用程序上下文中运行的进程。这意味着在新控制台关闭之前,原始控制台无法关闭。上述一些用户(例如 hedgedandlevered)报告说,关闭原始控制台会强制关闭新控制台。当我尝试时,新控制台确实打开,但旧控制台保持打开状态并处于冻结状态,直到新控制台关闭。基本问题是调用 shell 或 system 不会将应用程序上下文从原始控制台更改为新控制台,因此在新控制台关闭之前原始进程无法终止。
适合我的替代方案
而是使用
shell.exec
,它通常用于根据文件类型在默认应用程序中打开文件。当在 .exe 上使用时,显然它会运行可执行文件。但重要的区别在于系统在其自己单独的上下文中启动应用程序。所以这里是适合我的代码:您需要编辑文件路径
/bin/x64/Rgui.exe
以匹配您用于启动 R 的任何内容。您只需将此行放入您的 < code>.Rprofile 文件,然后您可以通过在 R 代码中输入restart.R
来调用它。与其他方法相比,这种方法的缺点是您无法像使用
shell
那样传递像--no-save
这样的命令行参数,但至少这会让您关闭了原始的 R 流程。Old post, but none of the answers quite work (for me, I'm using Windows, haven't tested others), so I'll add my solution. Some of my terminology might be off here, but this should get the point across:
Above answers don't quite work
Most of the answers submitted here involve using
shell
orsystem
which doesn't quite work because while they open a new R console and do instruct the original console to close, the new console is a process running in the application context of the old console. That means the original console cannot close until the new console closes. Some of the users above such as hedgedandlevered reported that closing the original console forces the new console to close. When I try, the new console does open, but the old console remains open in a frozen state until the new console is closed.The basic problem is calling
shell
orsystem
does not change the application context from the original console to the new one, and therefore the original process cannot terminate until the new console closes.Alternative that works for me
Instead use
shell.exec
which would normally be used to open a file in the default application based on file type. When used on a .exe, apparently, it runs the executable. The important difference, though, is that the system starts the application in it's own separate context. So here's the code that works for me:You'll need to edit the file path
/bin/x64/Rgui.exe
to match whatever you use to start R. You just put this line in your.Rprofile
file, then you can call it by enteringrestart.R
in your R code.The downside of this over other methods is that you can't pass command line arguments like
--no-save
as you would with justshell
, but at least this will let you close out the original R process.将此函数写入您的 .Rprofile
r()
会重新启动您的 R 会话。加载的包不会重新加载。您的环境将不会被保存。适用于 Linux。不知道其他操作系统上会发生什么
Write this function in your .Rprofile
r()
restarts you R session. Loaded packages will not reload. Your environment wont be saved.Works for Linux. No idea of what may happen on other OS
我需要在 Windows 上使用相同的刷新会话功能,最终得到了稍微修改过的代码版本:
在 Windows 上,您需要修改
Rprofile.site
文件。它位于 R 的etc
目录下。另请注意bin/x64
路径的最后部分可以根据您的系统配置进行更改。我希望这对其他人也有帮助。I needed the same refresh session functionality on windows and I ended up with a slightly modified version of the code:
On windows you need to modify the
Rprofile.site
file. It is under R'setc
directory. Also watch out for the last part of the path thebin/x64
can change according to your system configuration. I hope this will help others too.符合Martin Morgan使用
.Last()
的思想;这将使用之前调用的相同命令行选项集重新启动 R:In line with Martin Morgan's idea of using
.Last()
; this restarts R with the same set of command-line options as previously called:我认为,通过在选项中设置当前工作目录可以实现 R 的最佳使用。然后,每当您的工作区/会话文件开始显示或其中有足够的工作(在项目之间)时,您只需在关闭 R 后在工作目录中重命名此默认会话文件,R/Rstudio 就会自动在新的环境中启动您工作区/会话文件,而不会干扰您当前的工作。
记住退出 R 并重命名当前会话文件
当然,如果您不想保存当前工作,则必须确保在从原始对象复制后重置对象或对它们进行操作,以便它们是这样。相信我,知道您始终可以加载旧工作区是一种诱惑,但总比不有用。
简而言之,退出 R,它会给您一些间隙,而退出意味着该工作区已满,完成退出后重命名它并使用新的工作区重新启动 R/Rstudio。您始终可以在新工作区中加载选定的对象。理想情况下,所有重要工作都应位于项目目录中,但您仍然需要有时返回工作历史记录,并且一旦您处理较长的项目,保存的会话在某些时候会很有用。如果您不需要其中任何一个,只需
rm(list=ls())
另外,我喜欢 @Eduardo Alvin 的
RDComClient
想法,但它已停止使用。替代选项
在工作空间内随时摆脱包袱的一个简单替代方案是使用
save.image
这使您可以随意进出并打开需要多少工作区
I think, one realizes the best use of R by setting a current working directory in options. Then whenever your workspace /session file starts showing you up or has enough of your work in it (in between projects) you can just rename this default session file in the working directory after closing R and R/Rstudio will automatically start you in a new workspace/session file, without disturbing your current work.
Remember to quit R and rename the current session file
Of course if you do not want to save the current work you have to make sure you reset objects or operations on them were done after copying from original objects so they are as is. Trust me, knowing you can always load the old workspaces is a temptation but is more useful than not.
In short quit R, it gives you some gap while quitting means this workspace is full, rename it after completing the quit and restart R/Rstudio with a fresh workspace. You can always load select objects in the new workspace. Ideally all important work should be in Project directories but you still need a history of your jobs to go back to at times and saved sessions come in useful at some point once you are on longer projects. If you don't need any of it just
rm(list=ls())
Also, I like the
RDComClient
idea by @Eduardo Alvin but it has been discontinued.ALTERNATIVE OPTION
A simple alternative to get rid of the baggage at any time inside your workspace is to use
save.image
This leaves you free to come and go as you please and open as many workspaces as you need.
或者使用 --save 或 --no-save
我认为如果您在调用刷新之前使用了 setwd() ,这就是您所需要的(尽管这个版本和原始版本都不适合我,因为它重新启动 R 然后自行关闭,新窗口永远不会打开。如果有人可以对此发表评论,请这样做)
or with --save or --no-save
I think this is what you need if you've used setwd() before calling refresh (although neither this nor the original version works for me, since it restarts R then closes itself, a new window is never opened. If anyone can comment on this, please do so)
我写了以下函数。
记住!您只能使用它一次,然后您必须在 R 会话重新启动后重新加载它。
有时分离包也有帮助。
I have written the following function.
Remember! You can only use it once, then you have to reload it after the R session restarts.
Sometimes detaching a package also helps.
如果您在 Rstudio > 1.1.281,您可以使用:
If you're in Rstudio > 1.1.281, you can use: