Home - gizotso/R GitHub Wiki

Starting R from cmd line (R bin folder added to the %PATH%): R, Rgui.
q() : quit. q("no") to quit without saving.
Help !
-
help.start(): start the help (entry points to Manuals, Reference, Miscellaneous Material, ...)- http://127.0.0.1:19695/doc/html/UserManuals.html
- Help is installed under %R_HOME%/doc/html/index.html, manuals are located under %R_HOME%/doc/manual
-
Package Documentation:
help(package = "datasets") -
demo(): package demos "graphics", "colors".demo(package = .packages(all.available = TRUE)to list all demos available. -
? =
help():?plot,help(plot),help("plot")display help on object.?*help on arithmetic opertaor,?NA,?log, ...help("bs", try.all.packages = TRUE)help("bs", package = "splines")
example(): run example from an online help topic (that is valid entry in the online help.?colnames->example("colnames")
-
?? =
help.search(): search for help in all installed packages.??xyplot- use help.search("xyplot", rebuild = TRUE) to refresh help DB (useful when new packages have been installed)
-
str(a) # display the internal structure of an R object
-
system.file(): Finds the full file names of files in package- system.file("examples", package="shiny") [1] "T:/PortableApps/R/R_lib_32/shiny/examples"
Help! : apropos(), find() to find object by (partial) name
apropos() and find() search objects in the loaded packages matching expression.
apropos("GLM")` # objects with name containg GLM
[1] "glm" "glm.control" "glm.fit" "predict.glm"
[5] "residuals.glm" "summary.glm"
> apropos("^cor", ignore.case='TRUE') # objects beginning with cor
[1] "cor" "cor.test"
# the R 1-2-letter things
apropos("^..?$")
# the R 8-and-more letter things
apropos("^.{8,}$")
table(nchar(apropos("^.{8,}$")))
find() returns packages where objects of a given name can be found
find("plot")
[1] "package:graphics"
find("^plot", simple.words = FALSE)
[1] "package:stats" "package:graphics"
cor <- 1:3
find("cor")
[1] ".GlobalEnv" "package:stats"
find("cor", numeric = TRUE) # package position in search path (search())
find("cor", numeric = FALSE, mode = "function") # [1] "package:stats"
rm(cor)
R Session
R Workspace (R session image -> .RData / .rda)
ls(): list objects in the workspace / search pathls.str(pat="data_"): list objects with their structuresattach(): Attach set of R Objects to Search Path
name <- "Carmen"; n1 <- 10; n2 <- 100; m <- 0.5
ls(pat = "ˆm")
ls.str() # display details of in memory objects
> ls.str()
# m : num 0.5
# name : chr "Carmen"
lsf.str() # functions in memory
# rm(): remove an object
rm(a)
rm(a, b, c)
rm(list=ls()) # remove all workspace objects from memory
history(): R commands history
a <- rnorm(10^7)
object.size(a) ##80000024 bytes
print(object.size(a),units="b") ##80000024 bytes
print(object.size(a),units="Kb") ##78125 Kb
print(object.size(a),units="Mb") ##76.3 Mb
print(object.size(a),units="Gb") ##0.1 Gb
print(object.size(a),units="auto") ##76.3 Mb
save & load workspace image
load("T:\\SW Dev\\R project\\statsTeachR\\introR_21_1.0\\introR\\source\\labs\\.RData")
save.image("c:\\users\\family\\Desktop\\wk_img.RData")
# <=> save(list=ls(all=TRUE), file="c:\\...\wk_img.RData")
save.image(file="~/Logiciels/R/test.rda")
load("~/Logiciels/R/test.rda")
- https://en.wikibooks.org/wiki/R_Programming/Manage_your_workspace
- https://en.wikibooks.org/wiki/R_Programming/Sample_Session
Working Dir
-getwd(): Get Working Dir
-setwd("C:/data"): Set the working dir
setwd("T:/SW Dev/R project/statsTeachR/introR_21_1.0")
source("lab1_code1.R")
#set working dir to user's home dir
path.expand("~") # ~ -> $HOME
setwd("~")
setwd("~/Desktop")
getwd()
## [1] "d:/urrutiaa/My Documents"
# Lists the content of the working directory
dir()
list.files()
R datasets
data(): list datasets from loaded packagesdata(package="datasets")# lists all the datasets in the "datasets" packagedata(iris): load iris data?iris# Help for the "iris" Datasetsstr(iris)# structure
R Programming
page(lm): opens a new editor window and prints the code of the function in this editor.trCopy(): function in the TinnR package to copy the code of the function. Then you just have to paste it in a text editor to have a look at it.args(round): display function's arguments- Debug :
debug(f),undebug(f),debugonce(),isdebugged(f) traceback()browser(): Debug > by inserting this function in the code you will be able to inspect objects, traceback, and run step by step.trace("myfunc", browser, at = 4): add the browser() function within your func at line 4.untrace()to remove it.trace()can also be used to trace a function call. ex:trace(sum); hist(stats::rnorm(100))will trace each call to sum.untrace(sum).
options(error = recover),recover(). browse directly on any of the currently active function calls. options(error = NULL)
R Utils
system() gives access to the system (DOS or unix)
system("convert W:/toto.ps W:/toto.png") # converts toto.ps to toto.png
system("D:/Stata10/stata.exe do D:/pgm.do", wait = F) # opens Stata and run pgm.do
system("pdflatex.exe -shell-escape file.tex") # runs pdflatex
system("open file.pdf") # opens the pdf
system("open M:/.../doc/*.pdf") # opens all the pdf in a directory
File mgmt
- file.info("taille.txt")
- file.edit(), file.show()
- file.remove(dir(path="directoryname", pattern="*.log"))
Sys.sleep(5)
browseURL("http://en.wikibooks.org/wiki/R_Programming")
# For example, we download "http://en.wikibooks.org/wiki/R_Programming/Text_Processing" on our Desktop
download.file(url="http://en.wikibooks.org/wiki/R_Programming/Text_Processing", destfile= "~/Desktop/test_processing.html")
# You can also read it into R using readLines()
text <- readLines("http://en.wikibooks.org/wiki/R_Programming/Text_Processing")
Performances. (see also microbenchmark package adv-r Performance)
system.time(x<-rnorm(10^6))
# user system elapsed
# 0.11 0.00 0.11
debut <- proc.time()
x <- rnorm(10^6)
proc.time()-debut
# user system elapsed
# 0.11 0.00 0.10
R References
Stats with R
Data Mining / Statistics