Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: a way to easily suppress package startup messages #1583

Closed
wch opened this issue Aug 1, 2018 · 6 comments
Closed

Feature request: a way to easily suppress package startup messages #1583

wch opened this issue Aug 1, 2018 · 6 comments
Milestone

Comments

@wch
Copy link

wch commented Aug 1, 2018

In my Rmd documents, I almost never want something like library(tidyverse) to print out package startup messages. However, using the message=FALSE chunk option can be a little problematic, for two reasons: first, you have to think about it and do it; second, it may suppress other messages that are actually useful.

One possible workaround is for the user to put this in an echo=FALSE or include=FALSE chunk:

library <- function(...) {
  suppressPackageStartupMessages(base::library(...))
}

The user still has to remember to do it, though, and it's non-trivial for most people to write that code. Also, it won't catch library() calls that are from outside code.

In a conversation elsewhere, @jimhester suggested adding a packageStartupMessage chunk option which defaults to FALSE, and I agree that this would be a good idea. The evaluate package would need to support this.

@yihui yihui added this to the v1.21 milestone Aug 2, 2018
jimhester added a commit to jimhester/evaluate that referenced this issue Aug 2, 2018
These are often unwanted in knitr output, so this allows control of
whether to display them or not

Fixes yihui/knitr#1583
jimhester added a commit to jimhester/knitr that referenced this issue Aug 2, 2018
jimhester added a commit to jimhester/knitr that referenced this issue Aug 2, 2018
These are often unwanted in knitr output, so we default to not showing
them. Requires a corresponding change to evaluate
r-lib/evaluate#86

Fixes yihui#1583
@yihui
Copy link
Owner

yihui commented Aug 7, 2018

There is nothing wrong with @jimhester's (thanks!) implementations (#1584 r-lib/evaluate#86). In practice, you would have to set the option in every document:

knitr::opts_chunk$set(package.startup.message = FALSE)

I don't feel this is convenient enough for your case. Given the complexity of the two PRs (although they are not really complex), I'd rather use your wrapper function of library(). To make it slightly easier to type, you may devtools::install_github('yihui/xfun'), set the global option options(xfun.pkg_attach.message = FALSE) in .Rprofile, and use xfun::pkg_attach() to attach packages instead. This will suppress the messages globally (not only for knitr documents).

I wish base R could provide such a global option. Usually I don't like package startup messages, either.

@wch
Copy link
Author

wch commented Aug 7, 2018

I hope you'll reconsider the PR... I agree that knitr::opts_chunk$set(package.startup.message = FALSE) isn't super convenient, but I think it is better than having users write their own library() wrapper. I and others at RStudio were collectively able to write the library() wrapper that suppresses startup messages, but the vast majority of R users would not figure this out. (For example, I didn't find any answers, on SO or elsewhere, that had the desired effect of (1) looking like a normal library() call and (2) suppressing startup messages but not other messages.)

One way to simplify it is to suppress package startup messages by default. I think that that would be the desired behavior in the vast majority of cases. I could understand, though, if you are reluctant to do this because it is a significant change in behavior.

I don't think xfun::pkg_attach() is a solution for most people. For example, generally if I write a Rmd document and show library() in code, it's because I want people to see it and understand it, and using xfun::pkg_attach() doesn't have that effect. It also requires bringing in another package as a dependency.

@yihui
Copy link
Owner

yihui commented Aug 7, 2018

xfun is already a dependency of knitr.

This question has been asked multiple times on SO: https://www.google.com/search?q=knitr+suppress+package+startup+message (I answered one of them, and I guess that was my most upvoted answer on SO). Yes, message = FALSE suppresses other messages, too, but you have the option of attaching packages in a separate code chunk, which I don't think is really a big hassle.

```{r, message=FALSE}
library(tidyverse)
```

So the cost of the approach Jim proposed is two pull requests in two packages, plus documentation (in two packages), plus this in every document:

knitr::opts_chunk$set(package.startup.message = FALSE)

The cost of the wrapper

library <- function(...) suppressPackageStartupMessages(base::library(...))

is only the wrapper function (in every document). And the function is simple enough.

If you are concerned about the strangeness of xfun::pkg_attach() to users (which is a valid concern), I'd use the above wrapper function.

Anyway, I'm not convinced that knitr is the right place to solve the problem. If this problem is only solved in knitr, users may be surprised that when they run the code in the console (or R Markdown Notebooks) and see the messages. That is why I recommend a feature request to base R to suppress package startup messages. If you suppress them, it may be a better idea to do it consistently.

If you really hate the messages of a particular package, you may consider a feature request to that particular package :)

Of all possible ways to go, my personal preference is to attach packages in a separate code chunk with message=FALSE. If you don't want to do this and prefer writing all code in a single chunk instead, I don't think it will be that bad if you happen to miss certain other messages (messages are messages, after all). If you really want to be precise on controlling which messages to show or suppress, you can actually use a numeric index, e.g., use a negative index to remove the first message:

```{r, message = -1}
message('hello')
message('world')
```

This is certainly not a reliable solution (e.g., what if a package no longer emits messages).

Typically I try my best not to close PRs, but for this one, I really don't think the gain is significant compared to the cost.

@jimhester
Copy link
Contributor

jimhester commented Aug 7, 2018

My knitr PR set the option to default to FALSE, so you don't have to set anything in a new document.

Of all possible ways to go, my personal preference is to attach packages in a separate code chunk with message=FALSE

This is what I do as well, but many people clearly don't realize you can do this.

E.g. there are > 500 people with .md files containing the tidyverse attach message on GitHub. I would wager the vast majority of those people would prefer not to have this message in their output, but don't know how to suppress it.

@yihui
Copy link
Owner

yihui commented Aug 7, 2018

I don't really know whether defaulting to FALSE is a good idea. Maybe some startup messages are important. Maybe not. My guess is that once you are familiar with a certain package, its startup message will be more annoying than useful. I never add startup messages to any of my packages.

It is hard to know whether these 500 people don't know how to suppress messages (they could easily find it out if they could google) or just don't bother. Winston is the first person who has proposed that package startup messages should be suppressed by default in the seven years of knitr.

Is tidyverse the only or primary source of pain? If it is, I'd ask Hadley not to print the message in non-interactive() R sessions.

Seriously, I'm willing to pay each of you $100 not to introduce this new option in knitr or evaluate. The workaround is simple enough. I don't want to maintain the new code or documentation, or make the decision on whether this option should default to TRUE or FALSE.

Thanks again!

clrpackages pushed a commit to clearlinux-pkgs/R-xfun that referenced this issue Oct 26, 2018
…forms (not only Windows), since Microsoft's R could also fail with the 'auto' method in download.file()

Daijiang Li (1):
      add test_string for n2w() (#6)

Xianying Tan (2):
      is_ascii() returns NA for NA_character_ (#9)
      invalid_utf8() should ignore NA input (#10)

Yihui Xie (16):
      replace random tempdir/tempfile paths in check logs to avoid diffs when comparing check logs
      support the class JS_EVAL in tojson()
      mention jsonlite
      move the argument rw_error from gsub_dir() to gsub_file()
      canonical CRAN URL
      close rstudio/rmarkdown#708 and close rstudio/blogdown#106: provide a simple yet general approach to embedding files/dir in HTML output
      roxygenize and bump version
      enable code coverage
      add some sample tests
      install covr earlier
      update roxygen
      close yihui/knitr#1583: add a `message` argument to pkg_attach() so that we can suppress package startup messages
      fix rstudio/blogdown#327: try all methods on all platforms (not only Windows), since Microsoft's R could also fail with the 'auto' method in download.file()
      stop if zip() fails
      mention that the download link doesn't work with IE
      CRAN release v0.4
@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants