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

时间:2022-06-23 07:08:43

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.

我想看到一个有149行代码的输出。整个脚本中都有我想看到的表。我正在使用RStudio IDE。过去我用过Tinn-R。我将运行整个脚本,代码行和打印对象将在控制台中可见。

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.

我怎么能在RStudio中那样做呢?最接近它的是按下每一行的CTRL-ENTER键,但我不想这样做149次。如果我按下CTRL-SHIFT-ENTER键输入“run all”,那么R将处理所有数据并将对象放入内存中,但我看不到输出。

Please tell me how I can see all the output and/or send the output to a text file.

请告诉我如何查看所有的输出和/或将输出发送到文本文件。

3 个解决方案

#1


22  

I'm one of the RStudio developers. Thanks for the feedback--I'll log a bug.

我是RStudio开发人员之一。谢谢你的反馈——我会记录一个错误。

In the meantime, one workaround is to do source(filename, echo=T) from the console.

与此同时,一个解决方案是从控制台执行源(文件名,echo=T)。

#2


10  

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 press CTRL-A CTRL-ENTER.

您可以简单地选择要运行的代码,并按CTRL+ENTER在RStudio中执行您想要的操作。这适用于多行,就像tin - r。如果您想以冗长的方式一次运行所有内容,请按CTRL-A CTRL-ENTER。

As another option for saving to a text file, you can check ?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. 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.

sink()将控制台的所有输出重定向到一个连接,在本例中是一些文件。请注意,这只是标准输出,而不是警告或错误。这个命令还可以方便地在分析中创建输出文件,与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. In principle, RStudio runs silently if you run the whole script.

如果在RStudio中使用“run all”,则必须显式地使用所提到的任何函数来生成文件的输出。原则上,如果运行整个脚本,RStudio将以静默方式运行。

#3


1  

Use options(verbose=TRUE) to print all output verbosely throughout the script or session.

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

#1


22  

I'm one of the RStudio developers. Thanks for the feedback--I'll log a bug.

我是RStudio开发人员之一。谢谢你的反馈——我会记录一个错误。

In the meantime, one workaround is to do source(filename, echo=T) from the console.

与此同时,一个解决方案是从控制台执行源(文件名,echo=T)。

#2


10  

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 press CTRL-A CTRL-ENTER.

您可以简单地选择要运行的代码,并按CTRL+ENTER在RStudio中执行您想要的操作。这适用于多行,就像tin - r。如果您想以冗长的方式一次运行所有内容,请按CTRL-A CTRL-ENTER。

As another option for saving to a text file, you can check ?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. 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.

sink()将控制台的所有输出重定向到一个连接,在本例中是一些文件。请注意,这只是标准输出,而不是警告或错误。这个命令还可以方便地在分析中创建输出文件,与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. In principle, RStudio runs silently if you run the whole script.

如果在RStudio中使用“run all”,则必须显式地使用所提到的任何函数来生成文件的输出。原则上,如果运行整个脚本,RStudio将以静默方式运行。

#3


1  

Use options(verbose=TRUE) to print all output verbosely throughout the script or session.

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