2

This one is strange. In an R markdown document, every single code cell displays its output without error, but when I try to knit the document into html, I get an error:

Error: stat_bin() must not be used with a y aesthetic. Execution halted

The closest code I could find to the line number and the last cell name to flash by before the error occurred was this:

g + geom_histogram()  # default: bins=30 (for diamonds: 5.01 - 0.2 / 30)

g <- ggplot(data = diamonds, aes(x = carat))
g + geom_histogram(binwidth = 1)  # not fine grained enough

g + geom_histogram(binwidth = 0.1)

g + geom_histogram(binwidth = 0.01)  # too fine grained

1 Answer 1

3

A confusing aspect of the RStudio environment is that things can be loaded in memory that no longer reflect the current state of the code.

In the example given, g was changed in an earlier cell, but its clean perfect output continued to display in the later cell. Once all code errors were tracked down. The document then knitted correctly.

Among the things that needed to be addressed:

  • All packages in use need an explicit declaration as in library(dplyr). Some were in memory but not included in any of the markdown cells.

  • eval cannot be FALSE on any cell whose code effects later markdown cells, but include can be FALSE if the goal is to leave that cell out of the final knitted document.

  • Code loading data from files needed to get paths checked and included because the working directory got changed from what it was when files were loaded.

These are some of the things that can throw off the knit process, but once addressed, then the document should knit fine. Know any more things to check? Feel free to edit this post and add them in.

Thought about deleting this post after I caught my mistakes but decided to write this up in case helpful for anyone else. Best wishes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.