How to request an early exit when knitting an Rmd document?

后端 未结 2 1484
礼貌的吻别
礼貌的吻别 2020-12-07 17:45

Let\'s say you have an R markdown document that will not render cleanly.

I know you can set the knitr chunk option error to TRUE

2条回答
  •  感动是毒
    2020-12-07 18:27

    IMHO, difficulty debugging an Rmd document is a warning that something is wrong. I have a rule of thumb: Do the heavy lifting outside the Rmd. Do rendering inside the Rmd, and only rendering. That keeps the Rmd code simple.

    My large R programs look like this.

    data <- loadData()
    analytics <- doAnalytics(data)
    rmarkdown::render("theDoc.Rmd", envir=analytics)
    

    (Here, doAnalytics returns a list or environment. That list or environment gets passed to the Rmd document via the envir parameter, making the results of the analytics computations available inside the document.)

    The doAnalytics function does the complicated calculations. I can debug it using the regular tools, and I can easily check its output. By the time I call rmarkdown::render, I know the hard stuff is working correctly. The Rmd code is just "print this" and "format that", easy to debug.

    This division of responsibility has served me well, and I can recommend it. Especially compared to the mind-bending task of debugging complicated calculations buried inside a dynamically rendered document.

提交回复
热议问题