We will be using Markdown documents to fill out and submit
assignments. These markdown documents are text files that can include
code blocks (chunks), which appear light grey in RStudio. Package
knitr
converts these text files into HTML webpages. During
the conversion, the R code in the code blocks is run (interpreted) and
the output of the R code is ingested into the HTML file.
install.packages('knitr', dependencies = TRUE)
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.
Do not install.packages()
inside a
markdown document! Packages only need to be installed once during an
interactive session.
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.
Datasets must be loaded/imported in the markdown document. Remember, the datasets from your interactive session are not accessible when knitting the HMTL file.
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.
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.
Rename the output file but keep the file naming convention. Only replace YourLastName
No Rmd (markdown) files will be accepted as homework submissions. Only submit the html file!
Use the forum to ask questions!
Do not print long table outputs. This makes your assignment difficult to read. Use head(data.frame) to print only the first couple of rows.
Further reading:
Copyright © 2024 Humboldt-Universität zu Berlin. Department of Geography.