When starting RStudio you also start an interactive R session. That R session has an Environment, which is basically a memory of the functions or variables that you loaded or created during this session, e.g. when loading a package, importing a dataset, assigning a variable. When you knit your markdown document into an HTML file, a new environment is temporarily created just for the lifetime of the knitting, and that environment is separate from your interactive session environment. You can submit lines of code from your Markdown document to the console, i.e. the interactive session. But when you knit an entire Markdown file into a pdf or html file, the environment in which the knitting happens is unaware of the interactive session and its variables. If this sounds confusing, here are a few Dos and Don’ts to get of to a healthy start.

  1. Do not install.packages() inside a markdown document! Packages only need to be installed once during an interactive session.

  2. You must load packages with library() inside the markdown document! If your code works in your interactive session but not when knitting your document, this may do the trick.

  3. Datasets must be loaded/imported in the markdown document. Remember, the datasets from your interactive session are not accessible when knitting the HMTL file.

  4. Variables must exist in the markdown document. Knitr does not see the variables from your interactive session when knitting the HMTL file. Also a probable scenario if your code works in your interactive session but not when knitting your document.

  5. An Error message usually shows the line number where the error occurred. However, that line number is the first line of the entire code block. To find the exact line within the code block, you can break down big code blocks into smaller code blocks.

  6. Rename the output file but keep the file naming convention. Only replace YourLastName

  7. No Rmd (markdown) files will be accepted as homework submissions. Only submit the html file!

  8. Use the forum to ask questions!

Further reading:

Copyright © 2020 Humboldt-Universität zu Berlin. Department of Geography.