bright-star
bright-star

Reputation: 6447

How to disable source() file echo in R?

According to the documentation, source() takes a default option echo = verbose, which can get old fast when testing functions. How can I set this to be FALSE just for source() in a simple way (such as an .Rprofile setting)?

I tried setting options(echo=FALSE) but that throws a wrench in the terminal functioning:

> options(echo=FALSE)

5
[1] 5
options(echo=TRUE)
> 

Upvotes: 2

Views: 8511

Answers (4)

Ben Bolker
Ben Bolker

Reputation: 226732

How about

library(Defaults)
setDefaults("source",echo=FALSE)

?

This is similar to (but not quite identical/somewhat simpler than) the answer to this question.

Since the Defaults package was archived 6 months after this question was answered, you either would have to get it from here or use devtools::install_version("Defaults","1.1-1"), or fall back to @KonradRudolph's answer.

Upvotes: 2

yeliabsalohcin
yeliabsalohcin

Reputation: 740

If you are using RStudio, the Source button can perform either "Source" or "Source with Echo" using the little dropdown arrow to select between then. The button will then continue to run with the last chosen option.

Upvotes: 4

Quigi
Quigi

Reputation: 314

No, source does not take a "default option". It takes a logical argument echo, which defaults to the value of verbose. If the caller doesn't pass verbose either, that argument, in turn, defaults to getOption("verbose"). So if you wanted to set a global option to affect echoing of the input text, you would do options(verbose=FALSE). BTW, that's the setting of this option by default anyway, so you only need to change any of the above if you set it differently.

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 545963

Redefine source:

source = function (file, local = FALSE, print.eval = echo,
                   verbose = getOption("verbose"),
                   prompt.echo = getOption("prompt"), max.deparse.length = 150,
                   chdir = FALSE, encoding = getOption("encoding"),
                   continue.echo = getOption("continue"), skip.echo = 0,
                   keep.source = getOption("keep.source")) {
    base::source(file, local, echo = FALSE, print.eval, verbose, prompt.echo,
                 max.deparse.length, chdir, encoding, continue.echo, skip.echo,
                 keep.source)
}

Terrible, I know. But effective.

Upvotes: 2

Related Questions