Exploring the workspace

The working directory

getwd()
[1] "/home/dw/rexrepos/tmp"
setwd("d:/daniel/work/r/")

Create and remove objects

Create some objects

Aval <- 7
Bval <- 15
Cval <- 10
ls()
 [1] "Aval"        "Bval"        "Cval"        "dirMd"       "dirR"       
 [6] "dirTmp"      "fIn"         "fName"       "fOut"        "markdEngine"
[11] "siteGen"    

List available objects

# objects in .GlobalEnv workspace that have a capital C in their name
ls(".GlobalEnv", pattern="C")
character(0)
exists("Bval")
[1] TRUE
exists("doesNotExist")
[1] FALSE

Remove objects

Aval
[1] 7
rm(Aval)
Aval                             # this will give an error
Error in eval(expr, envir, enclos): Objekt 'Aval' nicht gefunden
## rm(list=ls(all.names=TRUE))   # remove all objects from the workspace

Show and rename objects

Show objects

Bval
[1] 15
print(Bval)
[1] 15
(Bval <- 4.5)
[1] 4.5
.Last.value
[1] "nanoc"
get("Bval")
[1] 4.5
varName <- "Bval"
get(varName)
[1] 4.5

Rename objects

ls()
 [1] "Bval"        "Cval"        "dirMd"       "dirR"        "dirTmp"     
 [6] "fIn"         "fName"       "fOut"        "markdEngine" "siteGen"    
[11] "varName"    
newNameVar <- "varNew"
assign(newNameVar, Bval)
varNew
[1] 4.5

Environments and search path

search()
 [1] ".GlobalEnv"        "package:stringr"   "package:knitr"    
 [4] "package:stats"     "package:graphics"  "package:grDevices"
 [7] "package:utils"     "package:datasets"  "Autoloads"        
[10] "package:base"     

How R Searches and Finds Stuff

Session history

By default, savehistory() saves the command history to the file .Rhistory in the current working directory.

history()
savehistory("d:/daniel/work/r/history.r")

By default, save.image() saves all objects in the workspace to the file .RData in the current working directory.

save.image("d:/daniel/work/r/objects.Rdata")

Get the article source from GitHub

R markdown - markdown - R code - all posts