R MARKDOWN

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. To knit to pdf, you need to install LaTeX first: https://www.latex-project.org/get/ but for a quick look, you can knit to HTML. It will not work now, because you first have to complete some exercises below. An alternative is importing tinytex and running tinytex::install_tinytex()

Let’s first try to focus on a code chunk; the grayish box below. The ‘```{r Example code chunk.}’ bits aren’t R code: They’re RMarkdown code, and they tell RStudio that everything in between them is R code. The rest of this document is just text, with occasional formatting instructions, like the stars around in between. The parts with square brackets with numbers inside e.g. [1] show the output of the code.

To make RStudio execute one, you can either: * Click the little green arrow at the top right of the chunk * Click anywhere inside it, and press cmd + Enter (for Mac) and ctrl + Enter (for Windows)

2 + 3
## [1] 5

The best part is to be able to insert R-code directly into your document. You can quickly insert chunks like these into your file with:

var <- "Hello there!"
var
## [1] "Hello there!"

When you render your .Rmd file, R Markdown will run each code chunk and embed the results beneath the code chunk in your final report

The most common R chunk options you’ll likely work with are echo, eval, and include. By default, all three of these options are set to TRUE.

If you want to compute something but hide it from the document, simply replace the above {r} by {r, echo=FALSE}. It prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures

## [1] "This one won't show up in the document"

eval = TRUE/FALSE specifies whether the code should be evaluated or just displayed without its output. So if you knit the document, the next line will only show the code but not the output

var2 <- "This one won't have the output"
var2

include = TRUE/FALSE specifies whether the code and its output should be included in the resulting knitted document. If it is set to FALSE the code is run but no remnant of the code or its output will be in the resulting document.

warning = FALSE prevents warnings that are generated by code from appearing in the finished

Global Options To set global options that apply to every chunk in your file, call knitr::opts_chunk\(set (\) this is a special symbol in R, you will learn more about it in the next module) in a code chunk. Knitr will treat each option that you pass to knitr::opts_chunk$set as a global default that can be overwritten in individual chunk headers.

Some important notes

Other then that RMarkdown’s R script chunks work exactly the same as what you learned in basic R. You can do calculations, create objects, make comments, create plots and many more. When you’re done press the knit button and select one of the options. You should see a document in the format you requested. Experiment in RMarkdown until you feel more confident. Remember to add code chunks! You can try calculating how many Eiffel towers could you place between the Earth and the Moon (Eiffel tower - 324m, distance to the moon - 384 400km). Make a variable that stores the Eiffel tower height in meters and another one which converts it into kilometers. Another variable should be the distance to the Moon. Name your final variable no_eiffel. Do the same for the Burj Khalifa (~830m) and call it no_burj. Then run the last chunk of code. You are ready to knit the code and that will be all for this module. See you in the next one!

## YOUR CODE HERE ##
eiffel_height_m <- 324
burjk_height_m <- 830
to_the_moon_m <- 384400000

no_eiffel <- to_the_moon_m/eiffel_height_m
no_burj <- to_the_moon_m/burjk_height_m
## -------------- ##

no_eiffel
## [1] 1186420
no_burj
## [1] 463132.5
barplot(c(no_eiffel,no_burj), names = c("Eiffel Tower","Burj Khalifa"),
        col = c("cornflowerblue", "thistle"))