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

show messages, warnings and errors in knit() #224

Closed
yihui opened this issue May 7, 2012 · 2 comments
Closed

show messages, warnings and errors in knit() #224

yihui opened this issue May 7, 2012 · 2 comments
Labels
feature Feature requests
Milestone

Comments

@yihui
Copy link
Owner

yihui commented May 7, 2012

otherwise users may never realize them

@matthias-studer
Copy link

Hello,

I have tried to use the following code to "compile" a file using knitr. However, if I do it, then all header definition (in LaTeX) are not included in the resulting file.

require(knitr)
filename <- "test2.Rnw"

## Setting error and warning hooks
knit_hooks$set(error=
local({
    error_list <- list()
    error_counter <- 0
    function(x, options){
        error_list[[options$label]] <<- c(error_list[[options$label]], x)
        error_counter <<- error_counter +1
    }
}))

knit_hooks$set(warning=local({
    warning_list <- list()
    warning_counter <- 0
    function(x, options){
        warning_list[[options$label]] <<- c(warning_list[[options$label]], x)
        warning_counter <<- warning_counter +1
    }
}))

## Knitting file
res <- tryCatch({
    knit(filename)
    NULL
}, error=function(e) e)

## Getting number of R errors
error_count <- environment(knit_hooks$get("error"))$error_counter

if(is.null(res) && error_count <1) { ## No error by knitr
    cat(filename, " success\n\n", file=stdout())
}else if(error_count >0){
    error_list <- environment(knit_hooks$get("error"))$error_list
    for(n in names(error_list)){
        cat("Error(s) in chunk: " , n, "\n", file=stderr())
        for(e in error_list[[n]]){
            cat("\t", e,"\n", file=stderr())
        }
        cat("\n", file=stderr())
    }
} else{ ## Error in knitr
    cat("\n\nknitr error: ", as.character(res), "\n\n", file=stderr())
}

@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
feature Feature requests
Projects
None yet
Development

No branches or pull requests

2 participants