In this tutorial, we will cover the R
knitr package that is used to convert
R Markdown into a rendered document (HTML, PDF, etc).
At the end of this activity, you will:
- Be able to produce (‘knit’) an HTML file from a R Markdown file.
- Know how to modify chunk options to change the output in your HTML file.
Things You’ll Need To Complete This Tutorial
You will need the most current version of R and, preferably, RStudio loaded on your computer to complete this tutorial.
Install R Packages
Share & Publish Results Directly from Your Code!
knitr package allow us to:
- Publish & share preliminary results with collaborators.
- Create professional reports that document our workflow and results directly from our code, reducing the risk of accidental copy and paste or transcription errors.
- Document our workflow to facilitate reproducibility.
- Efficiently change code outputs (figures, files) given changes in the data, methods, etc.
Publish from Rmd files with knitr
To complete this tutorial you need:
- The R
knitrpackage to complete this tutorial. If you need help installing packages, visit the R packages tutorial.
- An R Markdown document that contains a YAML header, code chunks and markdown segments. If you don't have an .Rmd file, visit the R Markdown tutorial to create one.
When To Knit: Knitting is a useful exercise throughout your scientific workflow. It allows you to see what your outputs look like and also to test that your code runs without errors. The time required to knit depends on the length and complexity of the script and the size of your data.
How to Knit
To knit in RStudio, click the knit pull down button. You want to use the
knit HTML for this lesson.
When you click the Knit HTML button, a window will open in your console titled R Markdown. This pane shows the knitting progress. The output (HTML in this case) file will automatically be saved in the current working directory. If there is an error in the code, an error message will appear with a line number in the R Console to help you diagnose the problem.
Data Tip: You can run
knitr from the command prompt
Activity: Knit Script
Knit the .Rmd file that you built in the last tutorial. What does it look like?
View the Output
When knitting is complete, the new HTML file produced will automatically open.
Notice that information from the YAML header (title, author, date) is printed at the top of the HTML document. Then the HTML shows the text, code, and results of the code that you included in the RMD document.
Data Institute Participants: Complete Week 2 Assignment
- Read this week’s assignment closely.
- Be sure to carefully check your knitr output to make sure it is rendering the way you think it should!
- When you are complete, submit your .Rmd and .html files to the NEON Institute participants GitHub repository (NEONScience/DI-NEON-participants).
- The files will have automatically saved to your R working directory, you will need to transfer the files to the /participants/pre-institute3-rmd/ directory and submitted via a pull request.
If you have questions or comments on this content, please contact us.Contact Us