In this document I would like to show some basics about how I use R markdown in my statistical analysis.
If you use Rmarkdown method to create your calculation and report you will have 2 basic files:
If you have never used Rmarkdown before read the first 2 chapter of HTML file first
If you have used before or have red at least the first 2 chapter I recommend to read the two files simultaneously
These documents could not always be red continously: at the first few topics you have to go forward and backward to see what is happening and why - to understand the basic syntax and the results I’m using.
If you would like to run and render the full Rmarkdown file I created you will need the pictures in the pictures folder too and you HAVE TO SET THE WORKING DIRECTORY.
The Rmarkdown file begins with a so called YAML header
After that we have 2 part of the R-markdown file: + texts - just typing the text + code chunks - make a code chunk with three back ticks followed by an r in braces at a new line. End the chunk with three back ticks at new line or click on insert (see later)
That is the first think that I do and I put into Rmarkdown file - usually just immediately after the YAML header (see later). If I transport my file from work to home, or I give my Rmarkdown file to anyone who would like to modify, run and render it I have to change it - but usually this is the only thing that is necessary to change. My working directory is where I store all my files that I’m using for that report. Generally it have to be the path where your Rmarkdown file is!! (If you would like to use other check google…)
We have to create a code chunck and use the setwd() command. Into the bracket we have to put the location path between apostrophs.
setwd("f:/pendrivok/oktatos/oktatas_2020tavasz/nemet_stat/3")
You can do the same by “clicking”:
You can do the same by “clicking”:
If you use the choose.dir() command in a code chunk, you can do the same with an interactive clicking way:
setwd(choose.dir())
As I know it is working only with MS Windows and only folders under “Computer” is uaseable.
I personally don’t like it…
The YAML header (or shortly header) is reguired part of the Rmarkdown file at the beginning. It is located between “—” and “—” signs.
YAML header
The YAML header contains information:
Rmarkdown could create 3 type of output:
Each of them has pros and cons - at first I recommend to use html.
You can easily change the main options by clicking on the wheel at the top:
YAML header options
Note: there are additional settings that could not be setting this way, only by typing
more information: link
The 2nd code chunk in my files where I give:
# coding: UTF-8
library(formatR) # for tidy
knitr::opts_chunk$set(echo = TRUE, options(digits = 4), options(OutDec = ","), warning = FALSE, comment = NA, tidy = TRUE, tidy.opts = list(width.cutoff = 60))
If your output format is pdf, I recommend the next settings too:
For creating the output document:
Output creation
Create code chunks
After {r…
the options I’m using often:
more on options: chunk options
You can run it using the play icon at the upper right corner.
You can run only a line in it using ctrl+enter.
Here are the main options you can do:
text formatting
Additionally you can use markdown language (close to Latex, but not exactly…) syntax…