如何获取在 RStudio 中运行的脚本的所有输出
我想查看 149 行脚本的输出。在整个脚本中都有我想查看的表格。我正在使用 RStudio IDE。过去我用过Tinn-R。我将运行整个脚本,代码行和打印的对象将在控制台中可见。
例如,这是摘录
attach(uniquehuman.race.eth)
partA.eth <-table(Ethnicity, Sex,useNA="ifany")
partA.eth
margin.table(partA.eth,1)#row totals
margin.table(partA.eth,2)#column totals
nrow(uniquehuman.race.eth)#total logged in
上面的代码将给出表格的文本输出和我需要的数字。然后我可以保存控制台或将整个内容复制并粘贴到文本文件中。
我怎样才能在 RStudio 中做到这一点?我最接近的方法是在每一行上按 CTRL-ENTER,但我不想这样做 149 次。如果我按 CTRL-SHIFT-ENTER 键“运行全部”,则 R 会处理所有数据并将对象放入内存中,但我看不到输出。
请告诉我如何查看所有输出和/或将输出发送到文本文件。
I want to see the output of a script that has 149 lines. All the way through the script there are tables that I want to see. I am using RStudio IDE. In the past I have used Tinn-R. I would run the whole script and the lines of code and the printed objects would be visible in the console.
For instance, here is an excerpt
attach(uniquehuman.race.eth)
partA.eth <-table(Ethnicity, Sex,useNA="ifany")
partA.eth
margin.table(partA.eth,1)#row totals
margin.table(partA.eth,2)#column totals
nrow(uniquehuman.race.eth)#total logged in
The above code would give a text output of the tables and the numbers I needed. I could then save the console or copy and paste the whole thing in a text file.
How can I do that in RStudio? The closest I come to it is hitting CTRL-ENTER on each line, but I do not want to do that 149 times. If I hit CTRL-SHIFT-ENTER for "run all", then R processes all the data and puts the objects into memory, but I do not see the output.
Please tell me how I can see all the output and/or send the output to a text file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我是 RStudio 开发人员之一。感谢您的反馈——我将记录一个错误。
与此同时,一种解决方法是从控制台执行
source(filename, echo=T)
。I'm one of the RStudio developers. Thanks for the feedback--I'll log a bug.
In the meantime, one workaround is to do
source(filename, echo=T)
from the console.您只需选择要运行的代码,然后按
CTRL+ENTER
即可在 RStudio 中执行您想要的操作。这适用于多行,就像 Tinn-R 中一样。如果您想以详细方式一次运行所有内容,请按CTRL-A CTRL-ENTER
。作为保存到文本文件的另一个选项,您可以检查
?sink
:sink()
将控制台的所有输出重定向到连接,在本例中是某个文件。请注意,这只是标准输出,而不是警告或错误。该命令还可以与print()
、cat()
、sprintf()
等结合使用,在分析中创建输出文件。如果您在 RStudio 中使用“run all”,则必须显式使用任何提到的函数来生成文件的输出。原则上,如果您运行整个脚本,RStudio 会静默运行。
You can simply select the code you want to run and press
CTRL+ENTER
to do what you want in RStudio. This works for multiple lines, exactly like in Tinn-R. If you want to run everything at once in a verbose way, you pressCTRL-A CTRL-ENTER
.As another option for saving to a text file, you can check
?sink
:sink()
redirects all output of the console to a connection, in this case some file. Mind you, this is only the standard output, not the warnings or errors. This command also come in handy to create output files in analyses, in combination withprint()
,cat()
,sprintf()
etc.If you use "run all" in RStudio, you have to explicitly use any of the mentioned functions to generate the output to the file. In principle, RStudio runs silently if you run the whole script.
使用 options(verbose=TRUE) 详细打印整个脚本或会话中的所有输出。
Use options(verbose=TRUE) to print all output verbosely throughout the script or session.