An introduction to R using R Markdown aimed at secondary school students (age 11-16) and for complete novices.A complete lesson plan to introduce the R coding language using R Markdown to deliver instructions.
To become familiar with the layout of an R Markdown document To practice typing in the code chunk To produce an output file of my work using the ‘knit’ command
tidyverse
I can write my name on my R Markdown document I can write in the code chunk I can produce an output file of my work
On line 3, replace where it says ‘write your name here’ with your full name. Make sure you don’t delete or write over the speech marks.
Click on the ‘knit’ command at the top of the pane. After a short wait you will see the output file of your work. Can you see your name on the output file?
If you can see your name, close the output file by clicking on X.
If you cannot see your name, drag the output file to the side so that you can see what you wrote in the Markdown file. Check you typed your name correctly. Check that there are speech marks around your name. Knit your Markdown document again. Has your name been printed? If so, close the output file by clicking on X.
When you write code in the code chunk, R will carry out the commands. Code chunks are named r chunk1, r chunk2 etc. You must write your code in the lines between the top row of and the bottom row of
.
We will tell R to do a calculation for us. Type 10 + 2 into the code chunk. Click on the little arrow on the right of the code chunk to tell R to carry out the command.
Did R give you the correct answer?
Now you make up an addition sum and type it into code chunk2. Don’t forget to click on the arrow on the right to run your code.
Now click on knit. Read through your output file to check that your sums are printed.
Try typing in some more calculations. Try a subtraction calculation or a multiplication calculation. (Use the symbol * for multiply). Type in code chunk3, chunk4 and chunk5 below.
I wonder what this code tells R to do.
Carefully copy and paste the following lines of code into code chunk 6. Run the code. What do you see?
x <- seq(1,128,1) y <- sin((x / 128.0) * pi) n <- 7 color <- rainbow(n, s = 1, v = 1, start = 0, end = max(1, n - 1)/n, alpha = 1) plot(x, y, type=“n”, ylim = c(0,10)) for(i in 1:7) {lines(x, y+i, col=color[i], lwd = 15)}
KNIT YOUR DOCUMENT for the final time. This will be the version of your work that your teacher will check.
Well done! You have completed your first R lesson.
License and Citation: You can use, modify, and adapt any of the lessons, but please include the following attribution: RGirls Community. (2022, April 10). RGirls Lessons. Zenodo. https://doi.org/10.5281/zenodo.6436861