Warnings, messages, and errors
1. Warnings, messages, and errors
Let's discuss warnings, messages, and errors, and how to specify whether or not to include them in a report.2. Warnings
We encountered warnings earlier while creating visualizations. Recall that when data was excluded from a plot, the knit file included a warning in the final report. The default for the warning chunk option is TRUE and, since we had not included an argument to suppress warnings, they were included in the knit file.3. Warnings
Earlier, we learned to use collapse equals TRUE to prevent warning messages from appearing in a separate block from the code in the knit report.4. Warnings
However, we may want to exclude them entirely and can do this by setting the warning chunk option equal to FALSE. As we discussed earlier when we first encountered warnings, if we exclude the warnings, we may want to include additional information about them in the text of the report.5. Messages
We have not encountered messages in our reports thus far, but this is because of another option that has been included. Recall, the data code chunk has the include option set to FALSE, which prevents the code and any results of the code from appearing in the final report.6. Messages
Notice that, if we remove this option, we see messages appear in the knit file. These messages are related to the packages and the data files that are loaded in this chunk.7. Messages
If we set message equals FALSE, these messages do not appear in the report. The include option has been used in the data chunk instead because we did not want the code or the results from the code in the report. Here, the results of the code are the messages we see when we do not specify any options in the code chunk. If we want the code to appear, but not any of the resulting messages, we should set message equals FALSE instead.8. Messages
For example, if the code chunk contains code to load the data and filter it further, we may want the code to appear in the report without any messages. To do this, we use message equals FALSE instead of include equals FALSE.9. Errors
The error option determines how errors in the code should be handled when knitting the report. The default option for error is FALSE, which means that if there is an error and we attempt to knit the file, the report will stop knitting when it encounters an error and return the error. If we set error equals TRUE instead, our report will knit, even with errors in the code, and the report will include the errors. This option is used less commonly, but it can be useful when working on a report to point out which code chunks are running as expected and which code chunks are causing errors. If we include this option, we should set it globally so it applies to all code chunks throughout the file.10. Let's practice!
Now that you've learned about warnings, messages, and errors, let's practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.