14.2 Objects, working directory and workspace

  • Possible storages
    • Harddrive
    • “Workspace”: Storage within R
    • Online on some server
    • Q: Who has his own server?
  • Working directory
    • getwd(): Display working directory
    • setwd(): Set working directory
    • dir(): Display files in working directory
    • R does not understand \
      • Use \\ instead or / in directory paths
    • #: Start comment that is not evaluated

  • Objects
    • Meaningful names; No blanks
    • a <- 10 or a = 10: Assign something to an object
    • a: Type name to display content of object a
    • rm(): Delete object from workspace
    • save(a, b, file = "myobjects.RData"): Save single objects a and b
    • load("myobjects.RData"): Load saved objects

  • “Workspace”
    • …stores all objects generated during the session
    • …is loaded automatically when you start R
    • ls(): Display workspace content
    • R queries whether you would like to save the workspace when quitting it

14.2.1 Example: Objects, working directory and workspace

# Comments in the script start with #
# Everything after # in the line is ignored by R

# Send code with CTRL + R

getwd() # Display working directory

# Generate a new folder on your hardrive called "2016_03_R_course_EUI"
# Use this folder as your working directory, i.e. to store the downloaded files in there
# If you need the path to your folder, copy it from the path field in the explorer (in windows)
# The course files are available here: https://drive.google.com/folderview?id=0Bwer5wQoreiuMTRueFBGdDllNEk&usp=sharing

# Set the your working directory to your new folder

dir() # Display content of working directory

a <- 50 # Generate object that contains the number 50
a       # Show content of object
a = 70

ls() # Display objects in workspace

# Create a more complicated object
thesenseoflife <- c("worshipping god","having fun","no idea")
interesting <- thesenseoflife # define another object

# Q: How can I store the object "interesting" in the object "boring"?

# Objectnames do not have blanks
# Names should make sense and be systematic 
  # e.g. for dummy variables: d.male.voter
  # e.g. variance of variables: var.income
a <- 1
b <- 2

# Save objects a and b in the file "objects-a-and-b.RData"
# in the working directory

# Delete objects a and b
ls() # display all objects
load("objects-a-and-b.RData") # load objects from wd

# Check if they were loaded

14.2.2 Exercise: Working directory, objects and workspace

  1. Open R and create a new script that you save under the name “yourlastname_rworkshop_2016.r”. In this script you save all the code that you use to solve the exercises from now on.
  2. Display the working directory.
  3. Change the working directory to your personal folder.
  4. Create two objects: The object “hundred” in which you save the number 100 and the object “xyz” in which you save the number 250.
  5. Check whether the created objects have really been stored in the workspace.
  6. Save the two objects in your working directory. Delete them from the workspace and load them again from your working directory.
  7. Check whether they are stored again in the working directory.

14.2.3 Solution: Working directory, objects and workspace