Suppressing Error Messages in knitr

前端 未结 1 1406
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 13:30

I wonder how to suppress error messages in knitr. My MWE is below:

\\documentclass{article} 
\\begin{document}
<< Test >>=
1:10
X
@          


        
相关标签:
1条回答
  • 2021-01-14 14:10

    Errors have their own dedicated hook function, stored in the environment accessed by knit_hooks$get(). Here, for your enlightenment, is the complete list of those functions:

    names(knit_hooks$get())
    # [1] "source"   "output"   "warning"  "message"  "error"    "plot"    
    # [7] "inline"   "chunk"    "document"
    

    To suppress warnings, just overwrite the default error hook function with one that takes the required arguments, but doesn't return anything at all.

    \documentclass{article}
    \begin{document}
    
    <<setup, include=FALSE, cache=FALSE>>=
    muffleError <- function(x,options) {}
    knit_hooks$set(error=muffleError)
    @
    
    <<Test>>=
    1:10
    X
    @
    \end{document}
    

    Which, upon compilation, yields the following

    enter image description here

    0 讨论(0)
提交回复
热议问题