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

Wrapping warning messages in knitr is not consistent with R output #1142

Closed
helske opened this issue Dec 17, 2015 · 2 comments
Closed

Wrapping warning messages in knitr is not consistent with R output #1142

helske opened this issue Dec 17, 2015 · 2 comments
Milestone

Comments

@helske
Copy link

helske commented Dec 17, 2015

There seems to be slight issue of inconsistency in how knitr wraps lines in warning messages (and error messages) compared to usual R output:

warning_fun <- function(){
  long_function_name <- function(){
    warning("this is a relatively long warning message which should be on new line.")
  }
  long_function_name()
}

On knitted PDF:

warning_fun()
## Warning in long_function_name(): this is a relatively long warning message
## which should be on new line.

On R console:

warning_fun()
# Warning message:
# In long_function_name() :
#   this is a relatively long warning message which should be on new line.

It seems that if I set options(warn = 1) then I get similar warning in R console (but warning itself is still on a new line):

options(warn = 1)
warning_fun()
# Warning in long_function_name() :
#   this is a relatively long warning message which should be on new line.

This seems minor issue itself, but I suspect this relates to the issue that some warnings and errors to become truncated in PDFs due to long function call:

sqrt("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
## Error in sqrt("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"): non-numeric argu

On R console:

sqrt("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
# Error in sqrt("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") : 
#   non-numeric argument to mathematical function
@yihui
Copy link
Owner

yihui commented Dec 27, 2015

The inconsistency looks minor, and I tend not to change it at this point. If the main issue here is to solve the truncation of text in PDF, you can customize the error/warning hook function to wrap the error/warning message, e.g.

library(knitr)
local({
  hook_error = knit_hooks$get('error')
  knit_hooks$set(error = function(x, options) {
    # process the error message here; you may print(x)/gsub()/strwrap(x)/...
    hook_error(x, options)
  })
})

There is endless pain like this with PDFs due to the constraint of page sizes. I don't think it completely solves the problem even if I move the message to a new line. For example, your function name may be so wide that it may have already exceeded the page margin. I may be able to treat the symptom you had there, but it definitely does not treat the disease.

@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

No branches or pull requests

2 participants