在控制台输出中保留长注释。不成为“......[截断]”的受害者

发布于 2024-11-09 18:05:06 字数 395 浏览 1 评论 0原文

我正在尝试运行一个包含大量注释的脚本来解释每个表格、统计测试和图表。我正在使用 RStudio IDE,如下所示

source(filename, echo=T)

,这确保脚本将所有内容输出到控制台。如果我运行以下序列,它将把所有输出发送到一个txt文件,然后关闭输出转移,

sink("filenameIwantforoutput.txt")
source(filename, echo=T)
sink()

唉,我发现我的很多评论都没有被输出。相反,我得到

“......但前提是我们有一个专门的 b .... [截断]”。

之前我曾经了解过在哪里保存输出,但那是几个月前的事了,现在我不记得了。你可以吗?

I am trying to run a script that has lots of comments to explain each table, statistical test and graph. I am using RStudio IDE as follows

source(filename, echo=T)

That ensures that the script outputs everything to the console. If I run the following sequence it will send all the output to a txt file and then switch off the output diversion

sink("filenameIwantforoutput.txt")
source(filename, echo=T)
sink()

Alas, I am finding that a lot of my comments are not being outputted. Instead I get

"...but only if we had had an exclusively b .... [TRUNCATED]".

Once before I learned where to preserve the output but that was a few months ago and now I cannot remember. Can you?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请止步禁区 2024-11-16 18:05:06

max.deparse.length= 参数设置为 source。您可能需要大于默认值 150 的值。例如:

source(filename, echo=TRUE, max.deparse.length=1e3)

并注意 ?source 的“详细信息”部分中的最后一段内容如下:

如果“echo”为真且为 deparsed
表达量超过
'max.deparse.length',那么多
输出字符后跟 '
....[截断]'.

Set the max.deparse.length= argument to source. You probably need something greater than the default of 150. For example:

source(filename, echo=TRUE, max.deparse.length=1e3)

And note the last paragraph in the Details section of ?source reads:

If ‘echo’ is true and a deparsed
expression exceeds
‘max.deparse.length’, that many
characters are output followed by ‘
.... [TRUNCATED] ’.

多情癖 2024-11-16 18:05:06

您可以通过覆盖 .Rprofile 中的 source() 函数将此行为设置为默认行为。

这似乎是覆盖函数的合理情况,因为从理论上讲,更改应该只影响屏幕输出。我们可以设计一个示例,但情况并非如此,例如,可以捕获屏幕输出并用作像 capture.output(source("somefile.R")) 这样的变量,但似乎不太可能。以更改返回值的方式更改函数可能会反过来影响您或与您共享代码的任何人(例如,如果您更改函数的 na.rm 参数的默认值)。

.source_modified <- source
formals(.source_modified)$max.deparse.length <- Inf
# Use 'nms' because we don't want to do it for all because some were already
# unlocked. Thus if we lock them again, then we are changing the previous
# state.
# (https://stackoverflow.com/a/62563023/1376404)
rlang::env_binding_unlock(env = baseenv(), nms = "source")
assign(x = "source", value = .source_modified, envir = baseenv())
rlang::env_binding_lock(env = baseenv(), nms = "source")
rm(.source_modified)

另一种方法是创建您自己的类似“别名”的函数。我在 .Rprofile 中使用以下内容:

s <- source
formals(s)$max.deparse.length <- Inf
formals(s)$echo <- TRUE

You can make this behavior the default by overriding the source() function in your .Rprofile.

This seems like a reasonable case for overriding a function because in theory the change should only affect the screen output. We could contrive an example where this is not the case, e.g., can capture the screen output and use as a variable like capture.output(source("somefile.R")) but it seems unlikely. Changing a function in a way that the return value is changed will likely come back to bite you or whoever you share your code with (e.g., if you change a default of a function's na.rm argument).

.source_modified <- source
formals(.source_modified)$max.deparse.length <- Inf
# Use 'nms' because we don't want to do it for all because some were already
# unlocked. Thus if we lock them again, then we are changing the previous
# state.
# (https://stackoverflow.com/a/62563023/1376404)
rlang::env_binding_unlock(env = baseenv(), nms = "source")
assign(x = "source", value = .source_modified, envir = baseenv())
rlang::env_binding_lock(env = baseenv(), nms = "source")
rm(.source_modified)

An alternative is to create your own 'alias'-like function. I use the following in my .Rprofile:

s <- source
formals(s)$max.deparse.length <- Inf
formals(s)$echo <- TRUE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文