Running RStudio and Setting Up Your Working Directory - Easy R Programming


After installing R and RStudio, the question is now how to start using R/RStudio. In this article, we’ll describe how to run RStudio and to set up your working directory.

Note that, it’s possible to use R outside or inside RStudio. However, we highly recommend to use R inside RStudio. RStudio allows users to run R in a more user-friendly environment.

Use R outside RStudio

Under Windows and MAC OSX

For the first time you use R, the suggested procedure, under Windows and MAC OSX, is as follow:

  1. Create a sub-directory, say R, in your “Documents” folder. This sub-folder, also known as working directory, will be used by R to read and save files.

  2. Launch R by double-clicking on the icon.

  3. Specify your working directory to R:
    • On Windows: File –> Change directory
    • On MAC OSX: Tools –> Change the working directory

Under Linux

  1. Open the shell prompt

  2. Create a working directory, named “R”, using “mkdir” command:


$ mkdir R $ cd R


  1. Start the R program with the command “R”:

$ R

  1. To quit R program, use this:

$ q()

Use R inside RStudio

Using R inside RStudio is the recommended choice.

Launch RStudio under Windows, MAC OSX and Linux

After installing R and RStudio, launch RStudio from your computer “application folders”.

RStudio screen

RStudio

RStudio is a four pane work-space for 1) creating file containing R script, 2) typing R commands, 3) viewing command histories, 4) viewing plots and more.

  1. Top-left panel: Code editor allowing you to create and open a file containing R script. The R script is where you keep a record of your work. R script can be created as follow: File –> New –> R Script.

  2. Bottom-left panel: R console for typing R commands

  3. Top-right panel:
    • Workspace tab: shows the list of R objects you created during your R session
    • History tab: shows the history of all previous commands
  4. Bottom-right panel:
    • Files tab: show files in your working directory
    • Plots tab: show the history of plots you created. From this tab, you can export a plot to a PDF or an image files
    • Packages tab: show external R packages available on your system. If checked, the package is loaded in R.

For more about RStudio read the online RStudio documentation.

Set your working directory

Recall that, the working directory is a folder where R reads and saves files.

Change your working directory

You can change your working directory as follow:


  1. Create a sub-directory named “R” in your “Documents” folder

  2. From RStudio, use the menu to change your working directory under Session > Set Working Directory > Choose Directory.
  3. Choose the directory you’ve just created in step 1


RStudio change working directory

It’s also possible to use the R function setwd(), which stands for “set working directory”.

setwd("/path/to/my/directory")

For Windows, the command might look like :

setwd("c:/Documents/my/working/directory")

Note that, if you want to know your current (or default) R working directory, type the command getwd(), which stands for “get working directory”.

Set a default working directory

A default working directory is a folder where RStudio goes, every time you open it. You can change the default working directory from RStudio menu under: Tools –> Global options –> click on “Browse” to select the default working directory you want.

Close your R/RStudio session

Each time you close R/RStudio, you will be asked whether you want to save the data from your R session. If you decide to save, the data will be available in future R sessions.

Infos

This analysis has been performed using R software (ver. 3.2.3).


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!





This page has been seen 961630 times