Introduction

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:

  • the Rmarkdown syntax file - that I will show what you have to write in it to get
  • the rendered file (the resulted document, output file)

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)

Preparation of the Rmarkdown file

Working directory set

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…)

Solution 1

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")

Solution 2 - by clicking

You can do the same by “clicking”:

  • At the File Tab navigate to the given folder

  • Then clikk on more and click on Set As Working Directory

  • Then copy the and paste the command from the consol to the code chunk

Solution 2b - by clicking

You can do the same by “clicking”:

  • Session menu/Set Working Directory/Choose Directory…

  • Then copy the and paste the command from the consol to the code chunk

Solution 3 - by interactive command

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…

YAML header

The YAML header (or shortly header) is reguired part of the Rmarkdown file at the beginning. It is located between “—” and “—” signs.

YAML header

YAML header

The YAML header contains information:

  • on the document (title, author, date) and
  • on the output file.

Rmarkdown could create 3 type of output:

  • html
  • pdf
  • doc

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

YAML header options

Note: there are additional settings that could not be setting this way, only by typing

more information: link

Basic settings

The 2nd code chunk in my files where I give:

  • my basic settings on code chunks that could differ from default (you can change it at every code chunks but if you would like to apply it on all of them here you should do) - code chunks options: see later.
  • which character codes I used (UTF-8 is recommended) - connecting chapters: save as and reopen with encoding
  • used packages with information for what I used them - other users may have to install it - see later
# 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:

Rendering output

For creating the output document:

Output creation

Output creation

Code chunks

create

Create code chunks

Create code chunks

options

After {r…

  • you could name your code chunks (it could be used for refering it)
  • and after a comma you could give options

the options I’m using often:

more on options: chunk options

handling code chunks

  • You can run it using the play icon at the upper right corner.

  • You can run only a line in it using ctrl+enter.