Get startedGet started for free

Code chunk options

1. Code chunk options

We've learned how to add, label, and modify code chunks. Now, let's discuss some code chunk options we can use to modify the knit report.

2. The data code chunk

Let's look at the code chunk we've seen in the exercises throughout the course, called data. The data chunk has loaded the data and packages needed for each exercise.

3. The data code chunk

Notice that, after the code chunk name, we see include equals FALSE inside the curly braces.

4. The include option

The include option determines whether or not the code and results appear in the report. When include is FALSE, the code and results do not appear in the report. When the file is knit, the code chunk runs and the results can be used throughout the file, like the data chunk in each exercise. The default argument for include is TRUE. Since include is not specified in any other code chunks in previous exercises, the code and results from those chunks have been included in the report.

5. The echo option

A related option is echo. When the echo option is set to FALSE, it prevents the code from appearing in the knit file, but displays the results of the code. This is an option we might use when creating plots. While we want the report to include the plot, we may not want the report to display the code that created it. Similar to include, the default for echo is also TRUE. We have also seen this in previous exercises, since the report displays both the code and the results from a code chunk by default.

6. The eval option

The eval option specifies whether or not we want to evaluate the code in a code chunk. If we want to exclude a code chunk from the report for the time being, we can set eval equals FALSE. When eval is FALSE, the code will not run and the results will not be included in the report. Notice that the code itself still appears in the report. This differs from other options, like include, because the code does not impact the report. Recall, with the include option, even if the code is not in the knit report, it will still be evaluated when the file is knit and impact any subsequent code chunks that reference defined objects from the code chunk. The default for eval is TRUE, since all code chunks are evaluated when the report is knit unless otherwise specified using this option.

7. Code option summary

Let's summarize each option we've discussed so far. The code is run in the report when either the include or the echo options are set to FALSE. Using the echo option, the results also appear in the report. When eval is set to FALSE, the code appears in the report, but the code is not run and the results do not appear.

8. The collapse option

Let's discuss one more chunk option, collapse. The collapse option specifies whether or not the code and any text output are split into multiple blocks or included in a single block in the final report. Consider the warning messages we saw when creating plots. We can modify how these render in the final report using the collapse option. Unlike the other options we've discussed so far, the default option for collapse is FALSE. This means that the code from the code chunk and text output resulting from the code will be separated in the knit report.

9. The collapse option

If instead we specify collapse equals TRUE, the code and text output are merged into a single block in the knit report.

10. Let's practice!

Now that you've learned more code chunk options, 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.