Session Two – Rstudio

Rstudio

On opening Rstudio, notice the different windows.

The Console

You can write commands here. On pressing ENTER, the result will also be displayed in the console. 

Environment

The environment tab displays any imported data set(s) as an object(s). Importing data can be achieved by writing commands in the console, or perhaps easier by pressing the import dataset button. 

Objects simplify data analysis, and besides data sets, objects can be numbers, figures and more.

The Bottom Right

The tab “files” shows any folders or files in the working directory of Rstudio. I find that making a seperate folder for each project simplifies working in R. By pressing “files” and “new project”, a new project is made in a specific directory. Saved files and data sets in this folder will be displayed in the bottom right under “files”. 

 

Notice the tabs “plots” and “viewer” – graphs and tables will be displayed here. Under “help” one can search for commands to read what they require to run correctly. All installed packages can be found under “packages”, and can be loaded by checking the respective box. However, it is easier to load packages using the command “library(PACKAGE NAME)”.

Writing Commands and Code

For ease of use and reproducibility, it is easier to write commands or code in a file in which we can comment and in which the results are not displayed. For this purpose, one can use Rmarkdown or Rscript. These can be accesed through “files” > “new file”.

Rmarkdown

Personally, I prefer to use Rmarkdown when analysing a data set. It allows writing in text fields and in “chunks” for code and commands. Furthermore, Rmarkdown allows exporting results as PDF, Word or HTML files. This makes sharing results easier. The output is also easily customizable for easy overview of ones results. See the Rmarkdown cheat sheet to get started.

 

To write code in Rmarkdown, insert a “chunck” either by pressing the chunk button in the upper right or by pressing (Ctrl+Alt+I).

By default results are displayed in Rmarkdown. To display code results in the console go under “Tools” > “Global Options” > “Rmarkdown” and uncheck the box “Show output inline for all Rmarkdown documents”.

 

One can also comment on ones code by in code chunks by putting a “#” before the comment. One can comment in Rscript using the same method. 

To export your work from Rmarkdown use the option “knit” 

Leave a Comment

Your email address will not be published. Required fields are marked *