简体   繁体   English

如何获取我在RStudio中运行的脚本的所有输出

[英]How do I get all the output from script I am running in RStudio

I want to see the output of a script that has 149 lines. 我想看看有149行的脚本输出。 All the way through the script there are tables that I want to see. 一直到脚本都有我想看的表。 I am using RStudio IDE. 我正在使用RStudio IDE。 In the past I have used Tinn-R. 在过去,我使用过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? 我怎么能在RStudio中做到这一点? The closest I come to it is hitting CTRL-ENTER on each line, but I do not want to do that 149 times. 我最接近它的是在每一行上按CTRL-ENTER,但我不想这样做149次。 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. 如果我点击CTRL-SHIFT-ENTER进行“全部运行”,则R处理所有数据并将对象放入内存,但我看不到输出。

Please tell me how I can see all the output and/or send the output to a text file. 请告诉我如何查看所有输出和/或将输出发送到文本文件。

I'm one of the RStudio developers. 我是RStudio开发人员之一。 Thanks for the feedback--I'll log a bug. 感谢您的反馈 - 我会记录一个错误。

In the meantime, one workaround is to do source(filename, echo=T) from the console. 与此同时,一种解决方法是从控制台执行source(filename, echo=T)

You can simply select the code you want to run and press CTRL+ENTER to do what you want in RStudio. 您只需选择要运行的代码,然后按CTRL+ENTER在RStudio中执行您想要的操作。 This works for multiple lines, exactly like in Tinn-R. 这适用于多行,与Tinn-R完全相同。 If you want to run everything at once in a verbose way, you press CTRL-A CTRL-ENTER . 如果要以详细的方式一次性运行所有内容,请按CTRL-A CTRL-ENTER

As another option for saving to a text file, you can check ?sink : 作为保存到文本文件的另一个选项,您可以检查?sink

sink(file='path/to/somefile.ext')
... # the code generating output
sink()

sink() redirects all output of the console to a connection, in this case some file. sink()将控制台的所有输出重定向到连接,在本例中为某个文件。 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 with print() , cat() , sprintf() etc. 此命令也可以方便地在分析中创建输出文件,并结合print()cat()sprintf()等。

If you use "run all" in RStudio, you have to explicitly use any of the mentioned functions to generate the output to the file. 如果在RStudio中使用“run all”,则必须明确使用任何提到的函数来生成文件的输出。 In principle, RStudio runs silently if you run the whole script. 原则上,如果您运行整个脚本,RStudio将以静默方式运行。

使用选项(verbose = TRUE)在整个脚本或会话中详细打印所有输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM