Rmarkdown: Continue Knitting if Errors in Chunk

I have an R Markdown file that I repurpose to analyze new samples. Sometimes the analysis generates no significant results, and so when I try to plot the result an error is thrown. I simply want to continue to Knit the html file if such an error occurs.

I've tried {r error = TRUE}, but the knitting still halts for some reason. Is there another knitr option that would bypass the error?

{r Gene Ontology Enrichment, error = TRUE}
enrichplot::barplot(ego.MF,
        font.size = 8,
        title="GO Enrichment - Molecular Function",
        showCategory=20)
Quitting from lines 441-478 (file.Rmd) 
Error in ans[ypos] <- rep(yes, length.out = len)[ypos] : 
  replacement has length zero

This is also posted on Stack Overflow, but hasn't gotten any responses.

error = TRUE should catch the error and show it in the Rmd output and not exit the rendering process.
Do you have a full example to share ?

You could also make the plot chunk conditional using a condition for eval = involving a previous result.
If FALSE, the chunk would not be evaluated.

In creating a simplified version for you to view, I believe I found the issue. The section causing the issue had the header:

{r Gene Ontology Enrichment,  include = FALSE,  error = TRUE}

Quitting from lines 88-104 (example.Rmd) 
Error in ans[ypos] <- rep(yes, length.out = len)[ypos] : 
  replacement has length zero
Calls: <Anonymous> ... expand_limits_discrete_trans -> expand_limits_continuous_trans -> ifelse
Execution halted

Removing include=FALSE allows knitting to continue. However, I am now left with a chunk of code that I would rather not display in the report.

Any ideas how I can both exclude this section from the report and bypass stops due to errors?

Yeah if you said include to FALSE, error will always stop
See documentation of the option Options - Chunk options and package options - Yihui Xie | 谢益辉

If you want to hide the code source you can set echo=FALSE.

Thank you. I accepted your response as a Solution - is there anything more I should do to 'close' the thread?

1 Like

That is how you close a thread ! :+1:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.