Introduction

This chapter gives on overview of the book, but also introduces some fundamental concepts, such as objects, variables, and workspaces; saving and loading variables and objects; and R-scripts. Below is the supporting material for the sections of the chapter.

Variables and Objects

  • Interactive notebook: https://mybinder.org/badge_logo.svg

  • Script file: variables.R

    # Defining two variables
    a <- 1
    b <- 2
    # Entering the name leads to printing the content of the variable
    a
    # Showing the variables defined:
    ls()
    # Removing variables:
    rm(a,b)
    # Checking the existence of a variable
    exists("a")
    # Defining and removing "hidden" variables
    a <- 1
    b <- 2
    x <- 3
    y <- 4
    z <- 42
    .zzyx <- 1412
    rm(list=ls())
    ls()
    ls(all=TRUE)
    

Saving and loading Objects

  • Interactive notebook: https://mybinder.org/badge_logo.svg

  • Script file: saving-and-loading-objects.R

    # In this example we create the two variables 'a' and 'b':
    a <- 1
    b <- 1
    # We save them in the data file "ab.RData"
    save(a,b,file="ab.RData")
    # We then remove the two variables:
    rm(a,b)
    # With then load the data file
    load("ab.RData")
    # and verify that the two variables are restored
    a
    b
    

Working with R-scripts

  • Interactive notebook: https://mybinder.org/badge_logo.svg

  • Script file: running-scripts.R

    ## Running a script with and without echo
    source("source-echo-demo.R")
    
    source("source-echo-demo.R",echo=TRUE)
    

    Additional script file: source-echo-demo.R

    cat("Hello world!\n")
    a <- 1
    b <- 2
    a
    print(a)
    b
    show(b)