KnitR/Hello World Example

Learning Task edit

  • (Installation KnitR) Install the KnitR package in R Studio!
  • (Run "Hello World" in KnitR) Go through the Hello World example below and get that example running on your computer!
  • (optional) When you go through the learning task and you identify some missing comments that could have helped you with the learning task, please modify this learning resource to be more comprehensive.

A "Hello World!" example edit

As our simplest KnitR document, we start with the classical "Hello world!". We will create a document, that uses the features of knitR to print the values of R variables to generate it. It looks like the following:

---
title: "Hello world! - 1"
author: "Martin Papke"
date: "23 August 2018"
output: pdf_document
---
```{r definition, include=FALSE}
helloworld <- "Hello World!"
```
I always like to say `r helloworld`

We first specified the title and author of the document and the output we want to generate. Then we created an R variable containing the text "Hello World!", which we printed into our document. To test this document, copy it into a textfile named 'hello.Rmd', start R and compile it with rmarkdown::render('hello.Rmd') from the R console. The used knitR features are explained below

Extension of the "Hello world!" example edit

As a next step, we want to use R's feature to generate random numbers to print "Hello World!" and random numbers. We extend our example as follows

---
title: "Hello world! - 2"
author: "Martin Papke"
date: "23 August 2018"
output: pdf_document
---
```{r definition, include=FALSE}
helloworld <- "Hello World!"
number <- sample(1:10, 1) # generate a random number between 1 and 10
```
It's better to say "Hello" more often, we will do it `r number`-times:
```{r loop, include=FALSE}
for (i in 1:number){
  helloworld
}
```

Here we generated a random number to print "Hello World!" more often.