Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving long comments in console output. Not falling victim to ".... [TRUNCATED]"

Tags:

r

console

rstudio

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?

like image 990
Farrel Avatar asked May 24 '11 00:05

Farrel


1 Answers

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
like image 136
scottkosty Avatar answered Sep 22 '22 20:09

scottkosty