| Title: | The R Utils Package |
|---|---|
| Description: | R utility functions. |
| Authors: | R Core Team and contributors worldwide |
| Maintainer: | R Core Team <[email protected]> |
| License: | Part of R 4.5.0 |
| Version: | 4.5.0 |
| Built: | 2025-04-11 20:15:12 UTC |
| Source: | base |
R utility functions
This package contains a collection of utility functions.
For a completelist, uselibrary(help = "utils").
R Core Team and contributors worldwide
Maintainer: R Core Team[email protected]
Compute the approximate string distance between character vectors.The distance is a generalized Levenshtein (edit) distance, giving theminimal possibly weighted number of insertions, deletions andsubstitutions needed to transform one string into another.
adist(x, y = NULL, costs = NULL, counts = FALSE, fixed = TRUE, partial = !fixed, ignore.case = FALSE, useBytes = FALSE)adist(x, y=NULL, costs=NULL, counts=FALSE, fixed=TRUE, partial=!fixed, ignore.case=FALSE, useBytes=FALSE)
x | a character vector.Long vectors are not supported. |
y | a character vector, or |
costs | a numeric vector or list with names partially matching‘insertions’, ‘deletions’ and ‘substitutions’ givingthe respective costs for computing the Levenshtein distance, or |
counts | a logical indicating whether to optionally return thetransformation counts (numbers of insertions, deletions andsubstitutions) as the |
fixed | a logical. If |
partial | a logical indicating whether the transformed |
ignore.case | a logical. If |
useBytes | a logical. If |
The (generalized) Levenshtein (or edit) distance between two stringss andt is the minimal possibly weighted number ofinsertions, deletions and substitutions needed to transformsintot (so that the transformation exactly matchest).This distance is computed forpartial = FALSE, currently usinga dynamic programming algorithm (see, e.g.,https://en.wikipedia.org/wiki/Levenshtein_distance) with spaceand time complexity, where and are thelengths ofs andt, respectively. Additionally computingthe transformation sequence and counts is.
The generalized Levenshtein distance can also be used for approximate(fuzzy) string matching, in which case one finds the substring oft with minimal distance to the patterns (which could betaken as a regular expression, in which case the principle of usingthe leftmost and longest match applies), see, e.g.,https://en.wikipedia.org/wiki/Approximate_string_matching. Thisdistance is computed forpartial = TRUE using ‘tre’ byVille Laurikari (https://github.com/laurikari/tre) andcorresponds to the distance used byagrep. In thiscase, the given cost values are coerced to integer.
Note that the costs for insertions and deletions can be different, inwhich case the distance betweens andt can be differentfrom the distance betweent ands.
A matrix with the approximate string distances of the elements ofx andy, with rows and columns corresponding toxandy, respectively.
Ifcounts isTRUE, the transformation counts arereturned as the"counts" attribute of this matrix, as a3-dimensional array with dimensions corresponding to the elements ofx, the elements ofy, and the type of transformation(insertions, deletions and substitutions), respectively.Additionally, ifpartial = FALSE, the transformation sequencesare returned as the"trafos" attribute of the return value, ascharacter strings with elements ‘M’, ‘I’, ‘D’ and‘S’ indicating a match, insertion, deletion and substitution,respectively. Ifpartial = TRUE, the offsets (positions ofthe first and last element) of the matched substrings are returned asthe"offsets" attribute of the return value (with both offsets in case of no match).
agrep for approximate string matching (fuzzy matching)using the generalized Levenshtein distance.
## Cf. https://en.wikipedia.org/wiki/Levenshtein_distanceadist("kitten", "sitting")## To see the transformation counts for the Levenshtein distance:drop(attr(adist("kitten", "sitting", counts = TRUE), "counts"))## To see the transformation sequences:attr(adist(c("kitten", "sitting"), counts = TRUE), "trafos")## Cf. the examples for agrep:adist("lasy", "1 lazy 2")## For a "partial approximate match" (as used for agrep):adist("lasy", "1 lazy 2", partial = TRUE)## Cf. https://en.wikipedia.org/wiki/Levenshtein_distanceadist("kitten","sitting")## To see the transformation counts for the Levenshtein distance:drop(attr(adist("kitten","sitting", counts=TRUE),"counts"))## To see the transformation sequences:attr(adist(c("kitten","sitting"), counts=TRUE),"trafos")## Cf. the examples for agrep:adist("lasy","1 lazy 2")## For a "partial approximate match" (as used for agrep):adist("lasy","1 lazy 2", partial=TRUE)
Gives an audible or visual signal to the user.
alarm()alarm()
alarm() works by sending a"\a" character to the console.On most platforms this will ring a bell, beep, or give some other signalto the user (unless standard output has been redirected).
It attempts to flush the console (seeflush.console).
No useful value is returned.
alarm()alarm()
apropos() returns a character vector giving the names ofobjects in the search list matching (as a regular expression)what.
find() returns where objects of a given name can be found.
apropos(what, where = FALSE, ignore.case = TRUE, dot_internals = FALSE, mode = "any")find(what, mode = "any", numeric = FALSE, simple.words = TRUE)apropos(what, where=FALSE, ignore.case=TRUE, dot_internals=FALSE, mode="any")find(what, mode="any", numeric=FALSE, simple.words=TRUE)
what | a character string. For |
where,numeric | logical indicating whether positions in thesearch list should also be returned. |
ignore.case | logical indicating if the search should becase-insensitive, |
dot_internals | logical indicating if the search result should showbase internal objects, |
mode | a character string; if not |
simple.words | logical; if |
Ifmode != "any" only those objects which are of modemodeare considered.
find is a different user interface for a similar task toapropos. By default (simple.words == TRUE),only whole names are matched. Unlikeapropos, matching isalways case-sensitive.
Unlike the default behaviour ofls, names whichbegin with a ‘.’ are included, but base ‘internal’ objectsare included only whendot_internals is true.
Forapropos, a character vector sorted by name. Forwhere = TRUE this has names giving the (numerical) positions onthe search path.
Forfind, either a character vector of environment names or(fornumeric = TRUE) a numerical vector of positions on thesearch path with names the names of the corresponding environments.
Originally, Kurt Hornik and Martin Maechler (May 1997).
glob2rx to convert wildcard patterns to regular expressions.
objects for listing objects from one place,help.search for searching the help system,search for the search path.
require(stats)## Not run: apropos("lm")apropos("GLM") # severalapropos("GLM", ignore.case = FALSE) # not oneapropos("lq")cor <- 1:pifind("cor") #> ".GlobalEnv" "package:stats"find("cor", numeric = TRUE) # numbers with these namesfind("cor", numeric = TRUE, mode = "function") # only the second onerm(cor)## Not run: apropos(".", mode = "list") # includes many datasets# extraction/replacement methods (need a DOUBLE backslash '\\')apropos("\\[")# everything % not diff-ablelength(apropos("."))# those starting with 'pr'apropos("^pr")# the 1-letter thingsapropos("^.$")# the 1-2-letter thingsapropos("^..?$")# the 2-to-4 letter thingsapropos("^.{2,4}$")# frequencies of 8-and-more letter thingstable(nchar(apropos("^.{8,}$")))require(stats)## Not run: apropos("lm")apropos("GLM")# severalapropos("GLM", ignore.case=FALSE)# not oneapropos("lq")cor<-1:pifind("cor")#> ".GlobalEnv" "package:stats"find("cor", numeric=TRUE)# numbers with these namesfind("cor", numeric=TRUE, mode="function")# only the second onerm(cor)## Not run: apropos(".", mode = "list") # includes many datasets# extraction/replacement methods (need a DOUBLE backslash '\\')apropos("\\[")# everything % not diff-ablelength(apropos("."))# those starting with 'pr'apropos("^pr")# the 1-letter thingsapropos("^.$")# the 1-2-letter thingsapropos("^..?$")# the 2-to-4 letter thingsapropos("^.{2,4}$")# frequencies of 8-and-more letter thingstable(nchar(apropos("^.{8,}$")))
Determine positions of approximate string matches.
aregexec(pattern, text, max.distance = 0.1, costs = NULL, ignore.case = FALSE, fixed = FALSE, useBytes = FALSE)aregexec(pattern, text, max.distance=0.1, costs=NULL, ignore.case=FALSE, fixed=FALSE, useBytes=FALSE)
pattern | a non-empty character string or a character stringcontaining a regular expression (for |
text | character vector where matches are sought.Coerced by |
max.distance | maximum distance allowed for a match.See |
costs | cost of transformations.See |
ignore.case | a logical. If |
fixed | If |
useBytes | a logical. If |
aregexec provides a different interface to approximate stringmatching thanagrep (along the lines of the interfacesto exact string matching provided byregexec andgrep).
Note that by default,agrep performs literal matches,whereasaregexec performs regular expression matches.
Seeagrep andadist for more informationabout approximate string matching and distances.
Comparisons are byte-by-byte ifpattern or any element oftext is marked as"bytes".
A list of the same length astext, each element of which iseither if there is no match, or a sequence of integers withthe starting positions of the match and all substrings correspondingto parenthesized subexpressions ofpattern, with attribute"match.length" an integer vector giving the lengths of thematches (or for no match).
regmatches for extracting the matched substrings.
## Cf. the examples for agrep.x <- c("1 lazy", "1", "1 LAZY")aregexec("laysy", x, max.distance = 2)aregexec("(lay)(sy)", x, max.distance = 2)aregexec("(lay)(sy)", x, max.distance = 2, ignore.case = TRUE)m <- aregexec("(lay)(sy)", x, max.distance = 2)regmatches(x, m)## Cf. the examples for agrep.x<- c("1 lazy","1","1 LAZY")aregexec("laysy", x, max.distance=2)aregexec("(lay)(sy)", x, max.distance=2)aregexec("(lay)(sy)", x, max.distance=2, ignore.case=TRUE)m<- aregexec("(lay)(sy)", x, max.distance=2)regmatches(x, m)
This function allows you to tile or cascade windows, or to minimize orrestore them (on Windows, i.e. when(.Platform$OS.type == "windows")).This may include windows not “belonging” toR.
arrangeWindows(action, windows, preserve = TRUE, outer = FALSE)arrangeWindows(action, windows, preserve=TRUE, outer=FALSE)
action | a character string, the action to perform on thewindows. The choices are |
windows | a |
preserve | If |
outer | This argument is only used in MDI mode. If |
The actions are as follows:
"vertical"Tile vertically.
"horizontal"Tile horizontally.
"cascade"Cascade the windows.
"minimize"Minimize all of the windows.
"restore"Restore all of the windows to normal size (not minimized, not maximized).
The tiling and cascading are done by the standard Windows API functions, but unlike those functions,they will apply to all of the windows in thewindows list.
By default,windows is set to the result ofgetWindowsHandles() (with one exception describedbelow). This will select windows belonging to the currentR process.However, if the global environment contains a variable named.arrangeWindowsDefaults, it will be used as the argument listinstead. See thegetWindowsHandles man page for adiscussion of the optional arguments to that function.
Whenaction = "restore" is used withwindows unspecified,minimized = TRUE is added to the argument list ofgetWindowsHandles so that minimized windows will be restored.
In MDI mode, by default tiling and cascading will happen within theRGUI frame. However, ifouter = TRUE, tiling is done on the systemdesktop. This will generally not give desirable results if anyR childwindows are included withinwindows.
This function is called for the side effect of arranging the windows.The list of window handles is returned invisibly.
This is only available on Windows.
Duncan Murdoch
## Not run: ## Only available on Windows :arrangeWindows("v")# This default is useful only in SDI mode: it will tile any Firefox window# along with the R windows.arrangeWindowsDefaults <- list(c("R", "all"), pattern = c("", "Firefox"))arrangeWindows("v")## End(Not run)## Not run: ## Only available on Windows :arrangeWindows("v")# This default is useful only in SDI mode: it will tile any Firefox window# along with the R windows.arrangeWindowsDefaults<- list(c("R","all"), pattern= c("","Firefox"))arrangeWindows("v")## End(Not run)
askYesNo provides a standard way to ask the user a yes/no question. It provides a way for front-ends to substitute their own dialogs.
askYesNo(msg, default = TRUE, prompts = getOption("askYesNo", gettext(c("Yes", "No", "Cancel"))), ...)askYesNo(msg, default=TRUE, prompts= getOption("askYesNo", gettext(c("Yes","No","Cancel"))),...)
msg | The prompt message for the user. |
default | The default response. |
prompts | Any of: a character vector containing 3 prompts corresponding toreturn values of |
... | Additional parameters, ignored by the default function. |
askYesNo will accept case-independent partial matches to the prompts. If no responseis given the value ofdefault will be returned; if a non-emptystring that doesn't match any of the prompts is entered, an error will be raised.
If a function or single character string naming a functionis given forprompts, it will be called asfn(msg = msg, default = default, prompts = prompts, ...). OnWindows, the GUI uses the unexportedutils:::askYesNoWinDialogfunction for this purpose.
If strings (or a string such as"Y/N/C") are given asprompts, the choices will be mapped to lowercase for the non-default choices, and left as-is for the default choice.
TRUE for yes,FALSE for no, andNA for cancel.
readline for more general user input.
if (interactive()) askYesNo("Do you want to use askYesNo?")if(interactive()) askYesNo("Do you want to use askYesNo?")
Spell check given files via Aspell, Hunspell or Ispell.
aspell(files, filter, control = list(), encoding = "unknown", program = NULL, dictionaries = character())aspell(files, filter, control= list(), encoding="unknown", program=NULL, dictionaries= character())
files | a character vector with the names of files to be checked. |
filter | an optional filter for processing the files before spellchecking, given as either a function (with formals |
control | a list or character vector of control options for thespell checker. |
encoding | the encoding of the files. Recycled as needed. |
program | a character string giving the name (if on the systempath) or full path of the spell check program to be used, or |
dictionaries | a character vector of names or file paths ofadditional R level dictionaries to use. Elements with no pathseparator specify R system dictionaries (in subdirectory‘share/dictionaries’ of the R home directory). The fileextension (currently, only ‘.rds’) can be omitted. |
The spell check programs employed must support the so-called Ispellpipe interface activated via command line option-a. Inaddition to the programs, suitable dictionaries need to be available.Seehttp://aspell.net,https://hunspell.github.io/ andhttps://www.cs.hmc.edu/~geoff/ispell.html, respectively, forobtaining the Aspell, Hunspell and (International) Ispell programs anddictionaries.
On Windows, Aspell is available viaMSYS2. One should use a non-Cygwinversion, e.g. packagemingw-w64-x86_64-aspell. The version builtagainst the Cygwin runtime (packageaspell) requires Unix lineendings in files and Unix-style paths, which is incompatible withaspell().
The currently available built-in filters are"Rd"(corresponding toRdTextFilter, with additional argumentignore allowing to give regular expressions for parts of thetext to be ignored for spell checking),"Sweave"(corresponding toSweaveTeXFilter),"R","pot","dcf" and"md".
Filter"R" is for R code and extracts the message stringconstants in calls tomessage,warning,stop,packageStartupMessage,gettext,gettextf, andngettext (the unnamed string constants for the firstfive, andfmt andmsg1/msg2 string constants,respectively, for the latter two).
Filter"pot" is for message string catalog ‘.pot’ files.Both have an argumentignore allowing to give regularexpressions for parts of message strings to be ignored for spellchecking: e.g., using"[ \t]'[^']*'[ \t[:punct:]]" ignores alltext inside single quotes.
Filter"dcf" is for files in Debian Control File format.The fields to keep can be controlled by argumentkeep (acharacter vector with the respective field names). By default,‘Title’ and ‘Description’ fields are kept.
Filter"md" is for files inMarkdown format(‘.md’ and ‘.Rmd’ files), and needs packagescommonmark andxml2 to be available.
The print method for the objects returned byaspell has anindent argument controlling the indentation of the positions ofpossibly misspelled words. The default is 2; Emacs users may find ituseful to use an indentation of 0 and visit output in grep-mode. Italso has averbose argument: when this is true, suggestions forreplacements are shown as well.
It is possible to employ additional R level dictionaries. Currently,these are files with extension ‘.rds’ obtained by serializingcharacter vectors of word lists usingsaveRDS. If suchdictionaries are employed, they are combined into a single word listfile which is then used as the spell checker's personal dictionary(option-p): hence, the default personal dictionary is notused in this case.
A data frame inheriting fromaspell (which has a useful printmethod) with the information about possibly misspelled words.
Kurt Hornik and Duncan Murdoch (2011).“Watch your spelling!”The R Journal,3(2), 22–28.doi:10.32614/RJ-2011-014.
aspell-utils for utilities for spell checking packages.
## Not run: ## To check all Rd files in a directory, (additionally) skipping the## \references sections.files <- Sys.glob("*.Rd")aspell(files, filter = list("Rd", drop = "\\references"))## To check all Sweave filesfiles <- Sys.glob(c("*.Rnw", "*.Snw", "*.rnw", "*.snw"))aspell(files, filter = "Sweave", control = "-t")## To check all Texinfo files (Aspell only)files <- Sys.glob("*.texi")aspell(files, control = "--mode=texinfo")## End(Not run)## List the available R system dictionaries.Sys.glob(file.path(R.home("share"), "dictionaries", "*.rds"))## Not run:## To check all Rd files in a directory, (additionally) skipping the## \references sections.files<- Sys.glob("*.Rd")aspell(files, filter= list("Rd", drop="\\references"))## To check all Sweave filesfiles<- Sys.glob(c("*.Rnw","*.Snw","*.rnw","*.snw"))aspell(files, filter="Sweave", control="-t")## To check all Texinfo files (Aspell only)files<- Sys.glob("*.texi")aspell(files, control="--mode=texinfo")## End(Not run)## List the available R system dictionaries.Sys.glob(file.path(R.home("share"),"dictionaries","*.rds"))
Utilities for spell checking packages via Aspell, Hunspell or Ispell.
aspell_package_Rd_files(dir, drop = c("\\abbr", "\\acronym", "\\author", "\\references"), control = list(), program = NULL, dictionaries = character())aspell_package_vignettes(dir, control = list(), program = NULL, dictionaries = character())aspell_package_R_files(dir, ignore = character(), control = list(), program = NULL, dictionaries = character())aspell_package_C_files(dir, ignore = character(), control = list(), program = NULL, dictionaries = character())aspell_write_personal_dictionary_file(x, out, language = "en", program = NULL)aspell_package_Rd_files(dir, drop= c("\\abbr","\\acronym","\\author","\\references"), control= list(), program=NULL, dictionaries= character())aspell_package_vignettes(dir, control= list(), program=NULL, dictionaries= character())aspell_package_R_files(dir, ignore= character(), control= list(), program=NULL, dictionaries= character())aspell_package_C_files(dir, ignore= character(), control= list(), program=NULL, dictionaries= character())aspell_write_personal_dictionary_file(x, out, language="en", program=NULL)
dir | a character string specifying the path to a package's rootdirectory. |
drop | a character vector naming additional Rd sections to dropwhen selecting text via |
control | a list or character vector of control options forthe spell checker. |
program | a character string giving the name (if on the systempath) or full path of the spell check program to be used, or |
dictionaries | a character vector of names or file paths ofadditional R level dictionaries to use. See |
ignore | a character vector with regular expressions to bereplaced by blanks when filtering the message strings. |
x | a character vector, or the result of a call to |
out | a character string naming the personal dictionary file towrite to. |
language | a character string indicating a language as used byAspell. |
Functionsaspell_package_Rd_files,aspell_package_vignettes,aspell_package_R_files andaspell_package_C_files perform spell checking on the Rd files,vignettes, R files, and C-level messages of the package with rootdirectorydir. They determine the respective files, apply theappropriate filters, and run the spell checker.
Seeaspell for details on filters.
The C-level message string are obtained from the‘po/PACKAGE.pot’ message catalog file, withPACKAGEthe basename ofdir.See the section on ‘C-level messages’ in‘Writing R Extensions’ for more information.
When using Aspell, the vignette checking skips parameters and/oroptions of commands\Sexpr,\citep,\code,\pkg,\proglang and\samp (in addition to thewhat the Aspell TeX/LaTeX filter skips by default). Furthercommands can be skipped by adding--add-tex-command options tothecontrol argument. E.g., to skip both option and parameterof\mycmd, add--add-tex-command='mycmd op'.
Suitable values forcontrol,program,dictionaries,drop andignore can also bespecified using a package defaults file which should go as‘defaults.R’ into the ‘.aspell’ subdirectory ofdir,and provides defaults via assignments of suitable named lists, e.g.,
vignettes <- list(control = "--add-tex-command='mycmd op'")
for vignettes (when using Aspell) and similarly assigning toRd_files,R_files andC_files for Rd files, Rfiles and C level message defaults.
Maintainers of packages using both English and American spelling willfind it convenient to pass control options--master=en_US and--add-extra-dicts=en_GB to Aspell and control options-d en_US,en_GB to Hunspell (provided that the correspondingdictionaries are installed).
Older versions ofR had no support for R level dictionaries, andhence provided the functionaspell_write_personal_dictionary_file to create (spell check)program-specific personal dictionary files from words to be accepted.The new mechanism is to use R level dictionaries, i.e., ‘.rds’files obtained by serializing character vectors of such words usingsaveRDS. For such dictionaries specified via thepackage defaults mechanism, elements with no path separator can be Rsystem dictionaries or dictionaries in the ‘.aspell’subdirectory.
available.packages returns a matrix of details corresponding topackages currently available at one or more repositories. Thecurrent list of packages is downloaded over the internet (or copiedfrom a local mirror).
available.packages(contriburl = contrib.url(repos, type), method, fields = getOption("available_packages_fields"), type = getOption("pkgType"), filters = NULL, repos = getOption("repos"), ignore_repo_cache = FALSE, max_repo_cache_age, cache_user_dir = str2logical(Sys.getenv("R_PACKAGES_CACHE_USER_DIR", FALSE)), quiet = TRUE, verbose = FALSE, ...)available.packages(contriburl= contrib.url(repos, type), method, fields= getOption("available_packages_fields"), type= getOption("pkgType"), filters=NULL, repos= getOption("repos"), ignore_repo_cache=FALSE, max_repo_cache_age, cache_user_dir= str2logical(Sys.getenv("R_PACKAGES_CACHE_USER_DIR",FALSE)), quiet=TRUE, verbose=FALSE,...)
contriburl | URL(s) of the ‘contrib’ sections of the repositories.Specify this argument only if your repository mirror is incomplete,e.g., because you mirrored only the ‘contrib’ section. |
method | download method, see |
type | character string, indicate which type of packages: see If |
fields | a character vector giving the fields to extract fromthe ‘PACKAGES’ file(s) in addition to the default ones, or |
filters | a character vector or list or |
repos | character vector, the base URL(s) of the repositories to use. |
ignore_repo_cache | logical. If true, the repository cache isnever used (see ‘Details’). |
max_repo_cache_age | any cached values older than this in secondswill be ignored. See ‘Details’. |
cache_user_dir |
|
quiet | logical, passed to |
verbose | logical indicating if a “progress report” lineshould be printed about the number of packages found in each repository. |
... | allow additional arguments to be passed from callers (which might bearguments to future versions of this function). Currently these areall passed to |
The list of packages is either copied from a local mirror (specified by a‘file://’URI) or downloaded. If downloaded andignore_repo_cache is false (the default), the list is cachedfor theR session in a per-repository file intempdir()with a name like
repos_http%3a%2f%2fcran.r-project.org%2fsrc%2fcontrib.rds
The cached values are renewed when found to be too old, with the agelimit controlledvia argumentmax_repo_cache_age.This defaults to the current value of the environment variableR_AVAILABLE_PACKAGES_CACHE_CONTROL_MAX_AGE, or if unset, to3600 (one hour).
By default, the return value includes only packages whose version andOS requirements are met by the running version ofR, and only givesinformation on the latest versions of packages.
Argumentfilters can be used to select which of the packages on therepositories are reported. It is called with its default value(NULL) by functions such asinstall.packages: this valuecorresponds togetOption("available_packages_filters")and toc("R_version", "OS_type", "subarch", "duplicates") ifthat is unset or set toNULL.
The built-in filters are
"R_version"Exclude packages whoseR versionrequirements are not met.
"OS_type"Exclude packages whose OS requirement isincompatible with this version ofR: that is excludeWindows-only packages on a Unix-alike platformandvice versa.
"subarch"For binary packages, exclude those withcompiled code that is not available for the currentsub-architecture, e.g. exclude packages only compiled for32-bit Windows on a 64-bit WindowsR.
"duplicates"Only report the latest version where morethan one version is available, and only report the first-namedrepository (incontriburl) with the latest version if thatis in more than one repository.
"license/FOSS"Include only packages for whichinstallation can proceed solely based on packages which can beverified as Free or Open Source Software (FOSS, e.g.,https://en.wikipedia.org/wiki/FOSS) employing the availablelicense specifications. Thus both the package and any packagesthat it depends on to load need to beknown to beFOSS.
Note that this does depend on the repository supplying licenseinformation.
"license/restricts_use"Include only packages forwhich installation can proceed solely based on packages which areknown not to restrict use.
"CRAN"If all the filters are from this set, then they can be specified as acharacter vector; otherwisefilters should be a list withelements which are character strings, user-defined functions oradd = TRUE (see below).
User-defined filters are functions which take a single argument, amatrix of the form returned byavailable.packages, andreturn a matrix consisting of a subset of the rows of the argument.
The special ‘filter’add = TRUE appends the otherelements of the filter list to the default filters.
A character matrix with one row per package, row names the package names andcolumn names including"Package","Version","Priority","Depends","Imports","LinkingTo","Suggests","Enhances","File" and"Repository". Additional columns can bespecified using thefields argument.
Where provided by the repository, fields"OS_type","License","License_is_FOSS","License_restricts_use","Archs","MD5sum" and"NeedsCompilation" are reported for use by the filters andpackage management tools, includinginstall.packages.
packageStatus,update.packages,install.packages,download.packages,contrib.url.
The ‘R Installation and Administration’ manual for how toset up a repository.
## Count package licensesdb <- available.packages(repos = findCRANmirror("web"), filters = "duplicates")table(db[,"License"])## Use custom filter function to only keep recommended packages## which do not require compilationavailable.packages(repos = findCRANmirror("web"), filters = list( add = TRUE, function (db) db[db[,"Priority"] %in% "recommended" & db[,"NeedsCompilation"] == "no", ] ))## Not run: ## Restrict install.packages() (etc) to known-to-be-FOSS packagesoptions(available_packages_filters = c("R_version", "OS_type", "subarch", "duplicates", "license/FOSS"))## oroptions(available_packages_filters = list(add = TRUE, "license/FOSS"))## Give priority to released versions on CRAN, rather than development## versions on R-Forge etc.options(available_packages_filters = c("R_version", "OS_type", "subarch", "CRAN", "duplicates"))## End(Not run)## Count package licensesdb<- available.packages(repos= findCRANmirror("web"), filters="duplicates")table(db[,"License"])## Use custom filter function to only keep recommended packages## which do not require compilationavailable.packages(repos= findCRANmirror("web"), filters= list( add=TRUE,function(db) db[db[,"Priority"]%in%"recommended"& db[,"NeedsCompilation"]=="no",]))## Not run:## Restrict install.packages() (etc) to known-to-be-FOSS packagesoptions(available_packages_filters= c("R_version","OS_type","subarch","duplicates","license/FOSS"))## oroptions(available_packages_filters= list(add=TRUE,"license/FOSS"))## Give priority to released versions on CRAN, rather than development## versions on R-Forge etc.options(available_packages_filters= c("R_version","OS_type","subarch","CRAN","duplicates"))## End(Not run)
RunR non-interactively with input frominfile andsend output (stdout/stderr) to another file.
R CMD BATCH [options] infile [outfile]R CMD BATCH[options] infile[outfile]
infile | the name of a file withR code to be executed. |
options | a list ofR command line options, e.g., for setting theamount of memory available and controlling the load/save process.If |
outfile | the name of a file to which to write output. If notgiven, the name used is that of |
UseR CMD BATCH --help to be reminded of the usage.
By default, the input commands are printed along with the output. Tosuppress this behavior, addoptions(echo = FALSE) at thebeginning ofinfile, or use option--no-echo.
Theinfile can have end of line marked byLF orCRLF (but notjustCR), and files with an incomplete last line (missing end of line(EOL) mark) are processed correctly.
A final expression ‘proc.time()’ will be executed after the inputscript unless the latter callsq(runLast = FALSE) or is aborted.This can be suppressed by the option--no-timing.
Additional options can be set by the environment variableR_BATCH_OPTIONS: these come after the default options (see thedescription of theoptions argument) and before any optionsgiven on the command line.
On Unix-alikes only: UnlikeSplus BATCH, this does not run theRprocess in the background.In most shells,
R CMD BATCH [options] infile [outfile] &
will do so.
Functionality for representing and manipulating bibliographicinformation in enhanced BibTeX style.
bibentry(bibtype, textVersion = NULL, header = NULL, footer = NULL, key = NULL, ..., other = list(), mheader = NULL, mfooter = NULL)## S3 method for class 'bibentry'print(x, style = "text", .bibstyle, bibtex = length(x) <= getOption("citation.bibtex.max", 1), ...)## S3 method for class 'bibentry'format(x, style = "text", .bibstyle = NULL, bibtex = length(x) <= 1, citMsg = missing(bibtex), sort = FALSE, macros = NULL, ...)## S3 method for class 'bibentry'sort(x, decreasing = FALSE, .bibstyle = NULL, drop = FALSE, ...)## S3 method for class 'citation' print(x, style = "citation", ...)## S3 method for class 'citation'format(x, style = "citation", ...)## S3 method for class 'bibentry'toBibtex(object, escape = FALSE, ...)bibentry(bibtype, textVersion=NULL, header=NULL, footer=NULL, key=NULL,..., other= list(), mheader=NULL, mfooter=NULL)## S3 method for class 'bibentry'print(x, style="text", .bibstyle, bibtex= length(x)<= getOption("citation.bibtex.max",1),...)## S3 method for class 'bibentry'format(x, style="text", .bibstyle=NULL, bibtex= length(x)<=1, citMsg= missing(bibtex), sort=FALSE, macros=NULL,...)## S3 method for class 'bibentry'sort(x, decreasing=FALSE, .bibstyle=NULL, drop=FALSE,...)## S3 method for class 'citation' print(x, style="citation",...)## S3 method for class 'citation'format(x, style="citation",...)## S3 method for class 'bibentry'toBibtex(object, escape=FALSE,...)
bibtype | a character string with a BibTeX entry type.SeeEntry Types for details. |
textVersion | a character string with a text representation ofthe reference to optionally be employed for printing. It isrecommended to leave this unspecified if |
header | a character string with optional header text. |
footer | a character string with optional footer text. |
key | a character string giving the citation key for the entry. |
... | for For the For the For the |
other | a list of arguments as in |
mheader | a character string with optional “outer” headertext. |
mfooter | a character string with optional “outer” footertext. |
x | an object inheriting from class |
style | an optional character string specifying the print style.If present, must be a unique abbreviation (with case ignored) of the availablestyles, seeDetails. |
decreasing | logical, passed to |
.bibstyle | a character string naming a bibliography style,see |
bibtex |
|
citMsg |
|
sort | logical indicating if bibentries should be sorted, using |
macros | a character string or an object with already loaded Rdmacros, seeDetails. |
drop | logical used as |
object | an object inheriting from class |
escape | a logical indicating whether non-ASCII characters shouldbe translated to LaTeX escape sequences. |
The bibentry objects created bybibentry can represent anarbitrary positive number of references. One can usec() tocombine bibentry objects, and hence in particular build a multiplereference object from single reference ones. Alternatively, one canusebibentry to directly create a multiple reference object byspecifying the arguments as lists of character strings.
Theprint method for bibentry objects is based on acorrespondingformat method and provides a choicebetween seven different styles:plain text (style"text"),BibTeX ("bibtex"),a mixture of plain text and BibTeX as traditionally used for citations("citation"),HTML ("html"),LaTeX ("latex"),R code ("R"),and a simple copy of thetextVersion elements (style"textVersion").
The"text","html" and"latex" styles make useof the.bibstyle argument: a style defined by thebibstyle function for rendering the bibentry into(intermediate) Rd format.The Rd format uses markup commands documented in the ‘Rd format’section of the ‘Writing R Extensions’ manual, e.g.\bold.In addition, one can use themacros argument toprovide additional (otherwise unknown, presumably LaTeX-style) Rdmacros, either by giving the path to a file with Rd macros to beloaded vialoadRdMacros, or an object with macrosalready loaded.Note that the"latex" result may contain commands from theLaTeX style file ‘Rd.sty’ shipped withR;put\usepackage{Rd} in the preamble of a LaTeX documentto make these available when compiling,e.g. withtexi2pdf.
When printing bibentry objects in citation style, aheader/footer for each item can be displayed as well asamheader/mfooter for the whole vector of references.
For formatting as R code,a choice between giving a character vector with onebibentry()call for each bibentry (as commonly used in ‘CITATION’ files), ora character string with one collapsed call, obtained by combining theindividual calls withc() if there is more than one bibentry.This can be controlled by passing the argumentcollapse=FALSE(default) orTRUE, respectively, to theformat() method.(Printing in R style always collapses to a single call.)
It is possible to subscript bibentry objects by their keys (which areused for character subscripts if the names areNULL).
There is also atoBibtex method for direct conversion toBibTeX.
As ofR 4.3.0, there is also atransform method whichallows to directly use the current fields, see the examples.
bibentry produces an object of class"bibentry".
bibentry creates"bibentry" objects, which are modeledafter BibTeX entries. The entry should be a valid BibTeX entry type,e.g.,
An article from a journal or magazine.
A book with an explicit publisher.
A part of a book, which may be a chapter (or sectionor whatever) and/or a range of pages.
A part of a book having its own title.
An article in a conference proceedings.
Technical documentation like a software manual.
A Master's thesis.
Use this type when nothing else fits.
A PhD thesis.
The proceedings of a conference.
A report published by a school or otherinstitution, usually numbered within a series.
A document having an author and title, but notformally published.
The... argument ofbibentry can be any number ofBibTeX fields, including
The address of the publisher or other type ofinstitution.
The name(s) of the author(s), either as aperson object, or as a character string whichas.person correctly coerces to such.
Title of a book, part of which is being cited.
A chapter (or section or whatever) number.
TheDOI(https://en.wikipedia.org/wiki/Digital_Object_Identifier)for the reference.
Name(s) of editor(s), same format asauthor.
The publishing institution of a technical report.
A journal name.
Any additional information that can help the reader.The first word should be capitalized.
The number of a journal, magazine, technical report,or of a work in a series.
One or more page numbers or range of numbers.
The publisher's name.
The name of the school where a thesis was written.
The name of a series or set of books.
The work's title.
A URL for the reference.(If the URL is an expandedDOI, we recommend to use the‘doi’ field with the unexpandedDOI instead.)
The volume of a journal or multi-volume book.
The year of publication.
## R referencerref <- bibentry( bibtype = "Manual", title = "R: A Language and Environment for Statistical Computing", author = person("R Core Team"), organization = "R Foundation for Statistical Computing", address = "Vienna, Austria", year = 2014, url = "https://www.R-project.org/")## Different printing stylesprint(rref)print(rref, style = "bibtex")print(rref, style = "citation")print(rref, style = "html")print(rref, style = "latex")print(rref, style = "R")## References for boot package and associated bookbref <- c( bibentry( bibtype = "Manual", title = "boot: Bootstrap R (S-PLUS) Functions", author = c( person("Angelo", "Canty", role = "aut", comment = "S original"), person(c("Brian", "D."), "Ripley", role = c("aut", "trl", "cre"), comment = "R port, author of parallel support", email = "[email protected]") ), year = "2012", note = "R package version 1.3-4", url = "https://CRAN.R-project.org/package=boot", key = "boot-package" ), bibentry( bibtype = "Book", title = "Bootstrap Methods and Their Applications", author = as.person("Anthony C. Davison [aut], David V. Hinkley [aut]"), year = "1997", publisher = "Cambridge University Press", address = "Cambridge", isbn = "0-521-57391-2", url = "http://statwww.epfl.ch/davison/BMA/", key = "boot-book" ))## Combining and subsettingc(rref, bref)bref[2]bref["boot-book"]## Extracting fieldsbref$authorbref[1]$authorbref[1]$author[2]$email## Field names are case-insensitiverref$Yearrref$Year <- R.version$yearstopifnot(identical(rref$year, R.version$year))## Convert to BibTeXtoBibtex(bref)## Transformtransform(rref, address = paste0(address, ", Europe"))## BibTeX reminder message (in case of >= 2 refs):print(bref, style = "citation")## Format in R style## One bibentry() call for each bibentry:writeLines(paste(format(bref, "R"), collapse = "\n\n"))## One collapsed call:writeLines(format(bref, "R", collapse = TRUE))## R referencerref<- bibentry( bibtype="Manual", title="R: A Language and Environment for Statistical Computing", author= person("R Core Team"), organization="R Foundation for Statistical Computing", address="Vienna, Austria", year=2014, url="https://www.R-project.org/")## Different printing stylesprint(rref)print(rref, style="bibtex")print(rref, style="citation")print(rref, style="html")print(rref, style="latex")print(rref, style="R")## References for boot package and associated bookbref<- c( bibentry( bibtype="Manual", title="boot: Bootstrap R (S-PLUS) Functions", author= c( person("Angelo","Canty", role="aut", comment="S original"), person(c("Brian","D."),"Ripley", role= c("aut","trl","cre"), comment="R port, author of parallel support", email="[email protected]")), year="2012", note="R package version 1.3-4", url="https://CRAN.R-project.org/package=boot", key="boot-package"), bibentry( bibtype="Book", title="Bootstrap Methods and Their Applications", author= as.person("Anthony C. Davison [aut], David V. Hinkley [aut]"), year="1997", publisher="Cambridge University Press", address="Cambridge", isbn="0-521-57391-2", url="http://statwww.epfl.ch/davison/BMA/", key="boot-book"))## Combining and subsettingc(rref, bref)bref[2]bref["boot-book"]## Extracting fieldsbref$authorbref[1]$authorbref[1]$author[2]$email## Field names are case-insensitiverref$Yearrref$Year<- R.version$yearstopifnot(identical(rref$year, R.version$year))## Convert to BibTeXtoBibtex(bref)## Transformtransform(rref, address= paste0(address,", Europe"))## BibTeX reminder message (in case of >= 2 refs):print(bref, style="citation")## Format in R style## One bibentry() call for each bibentry:writeLines(paste(format(bref,"R"), collapse="\n\n"))## One collapsed call:writeLines(format(bref,"R", collapse=TRUE))
ThebrowseEnv function opens a browser with list of objectscurrently insys.frame() environment.
browseEnv(envir = .GlobalEnv, pattern, excludepatt = "^last\\.warning", html = .Platform$GUI != "AQUA", expanded = TRUE, properties = NULL, main = NULL, debugMe = FALSE)browseEnv(envir= .GlobalEnv, pattern, excludepatt="^last\\.warning", html= .Platform$GUI!="AQUA", expanded=TRUE, properties=NULL, main=NULL, debugMe=FALSE)
envir | an |
pattern | aregular expression for object subselectionis passed to the internal |
excludepatt | a regular expression fordropping objectswith matching names. |
html | is used to display the workspaceon a HTML page in your favorite browser. The default except whenrunning from |
expanded | whether to show one level of recursion. It can be usefulto switch it to |
properties | a named list of global properties (of the objects chosen)to be showed in the browser; when |
main | a title string to be used in the browser; when |
debugMe | logical switch; if true, some diagnostic output is produced. |
Very experimental code: displays a static HTML page on all platformsexceptR.app on macOS.
Only allows one level of recursion into object structures.
It can be generalized. See sources for details.Most probably, this should rather work through using thetkWidgetpackage (fromhttps://www.bioconductor.org).
if(interactive()) { ## create some interesting objects : ofa <- ordered(4:1) ex1 <- expression(1+ 0:9) ex3 <- expression(u, v, 1+ 0:9) example(factor, echo = FALSE) example(table, echo = FALSE) example(ftable, echo = FALSE) example(lm, echo = FALSE, ask = FALSE) example(str, echo = FALSE) ## and browse them: browseEnv() ## a (simple) function's environment: af12 <- approxfun(1:2, 1:2, method = "const") browseEnv(envir = environment(af12)) }if(interactive()){## create some interesting objects : ofa<- ordered(4:1) ex1<- expression(1+0:9) ex3<- expression(u, v,1+0:9) example(factor, echo=FALSE) example(table, echo=FALSE) example(ftable, echo=FALSE) example(lm, echo=FALSE, ask=FALSE) example(str, echo=FALSE)## and browse them: browseEnv()## a (simple) function's environment: af12<- approxfun(1:2,1:2, method="const") browseEnv(envir= environment(af12))}
Load a given URL into an HTML browser.
browseURL(url, browser = getOption("browser"), encodeIfNeeded = FALSE)browseURL(url, browser= getOption("browser"), encodeIfNeeded=FALSE)
url | a non-empty character string giving the URL to be loaded.Some platforms also accept file paths. |
browser | a non-empty character string giving the name of theprogram to be used as the HTML browser. It should be in the PATH,or a full path specified. Alternatively, anR function to becalled to invoke the browser. Under Windows |
encodeIfNeeded | Should the URL be encoded by |
The default browser is set by option"browser", in turn set bythe environment variableR_BROWSER which is by default set infile ‘R_HOME/etc/Renviron’ to a choicemade manually or automatically whenR was configured. (SeeStartup for where to override that default value.)To suppress showing URLs altogether, use the value"false".
On many platforms it is best to set option"browser" to ageneric program/script and let that invoke the user's choice ofbrowser. For example, on macOS useopen and on many otherUnix-alikes usexdg-open.
Ifbrowser supports remote control andR knows how to performit, the URL is opened in any already-running browser or a new one ifnecessary. This mechanism currently is available for browsers whichsupport the"-remote openURL(...)" interface (which includesMozilla and Opera), Galeon, KDE konqueror (via kfmclient) andthe GNOME interface to Mozilla. (Firefox has dropped support, butdefaults to using an already-running browser.) Note that the type ofbrowser is determined from its name, so this mechanism will only beused if the browser is installed under its canonical name.
Because"-remote" will use any browser displaying on the Xserver (whatever machine it is running on), the remote controlmechanism is only used ifDISPLAY points to the local host.This may not allow displaying more than one URL at a time from aremote host.
It is the caller's responsibility to encodeurl if necessary(seeURLencode).
To suppress showing URLs altogether, setbrowser = "false".
The behaviour for argumentsurl which are not URLs isplatform-dependent. Some platforms accept absolute file paths; feweraccept relative file paths.
The default browser is set by option"browser", in turn set bythe environment variableR_BROWSER if that is set, otherwise toNULL.To suppress showing URLs altogether, use the value"false".
Some browsers have required ‘:’ be replaced by ‘|’ in filepaths: others do not accept that. All seem to accept ‘\’ as apath separator even though the RFC1738 standard requires ‘/’.
To suppress showing URLs altogether, setbrowser = "false".
Which URL schemes are accepted is platform-specific: expect‘http://’, ‘https://’ and ‘ftp://’ to work, but‘mailto:’ may or may not (and if it does may not use the user'spreferred email client). However, modern browsers are unlikely to handle‘ftp://’.
For the ‘file://’ scheme the format accepted (if any) can depend onboth browser and OS.
## Not run: ## for KDE users who want to open files in a new taboptions(browser = "kfmclient newTab")browseURL("https://www.r-project.org")## On Windows-only, something likebrowseURL("file://d:/R/R-2.5.1/doc/html/index.html", browser = "C:/Program Files/Mozilla Firefox/firefox.exe")## End(Not run)## Not run:## for KDE users who want to open files in a new taboptions(browser="kfmclient newTab")browseURL("https://www.r-project.org")## On Windows-only, something likebrowseURL("file://d:/R/R-2.5.1/doc/html/index.html", browser="C:/Program Files/Mozilla Firefox/firefox.exe")## End(Not run)
List available vignettes in an HTML browser with links to PDF,LaTeX/Noweb source, and (tangled) R code (if available).
browseVignettes(package = NULL, lib.loc = NULL, all = TRUE)## S3 method for class 'browseVignettes'print(x, ...)browseVignettes(package=NULL, lib.loc=NULL, all=TRUE)## S3 method for class 'browseVignettes'print(x,...)
package | a character vector with the names of packages tosearch through, or |
lib.loc | a character vector of directory names ofR libraries,or |
all | logical; if |
x | Object of class |
... | Further arguments, ignored by the |
FunctionbrowseVignettes returns an object of the same class;the print method displays it as an HTML page in a browser (usingbrowseURL).
## List vignettes from all *attached* packagesbrowseVignettes(all = FALSE)## List vignettes from a specific packagebrowseVignettes("grid")## List vignettes from all *attached* packagesbrowseVignettes(all=FALSE)## List vignettes from a specific packagebrowseVignettes("grid")
Invokes an editor or email program to write a bug report or opens aweb page for bug submission. Some standard information on the currentversion and configuration ofR are included automatically.
bug.report(subject = "", address, file = "R.bug.report", package = NULL, lib.loc = NULL, ...)bug.report(subject="", address, file="R.bug.report", package=NULL, lib.loc=NULL,...)
subject | Subject of the email. |
address | Recipient's email address, where applicable: forpackage bug reports sent by email this defaults to the address ofthe package maintainer (the first if more than one is listed). |
file | filename to use (if needed) for setting up the email. |
package | Optional character vector naming a single package which isthe subject of the bug report. |
lib.loc | A character vector describing the location ofRlibrary trees in which to search for the package, or |
... | additional named arguments such as |
Ifpackage isNULL or a base package, this opens the Rbugs tracker athttps://bugs.r-project.org/.
Ifpackage is specified, it is assumed that the bug report isabout that package, and parts of its ‘DESCRIPTION’ file are addedto the standard information. If the package has a non-emptyBugReports field in the ‘DESCRIPTION’ file specifying theURL of a webpage, that URL will be opened usingbrowseURL, otherwise an email directed to the packagemaintainer will be generated usingcreate.post. Ifthere is any other form ofBugReports field or aContactfield, this is examined as it may provide a preferred email address.
Nothing useful.
IfR executes an illegal instruction, or dies with an operatingsystem error message that indicates a problem in the program (asopposed to something like “disk full”), then it is certainly abug.
Taking forever to complete a command can be a bug, but you must makecertain that it was reallyR's fault. Some commands simply take along time. If the input was such that you KNOW it should have beenprocessed quickly, report a bug. If you don't know whether thecommand should take a long time, find out by looking in the manual orby asking for assistance.
If a command you are familiar with causes anR error message in acase where its usual definition ought to be reasonable, it is probablya bug. If a command does the wrong thing, that is a bug. But be sureyou know for certain what it ought to have done. If you aren'tfamiliar with the command, or don't know for certain how the commandis supposed to work, then it might actually be working right. Ratherthan jumping to conclusions, show the problem to someone who knows forcertain.
Finally, a command's intended definition may not be best forstatistical analysis. This is a very important sort of problem, butit is also a matter of judgement. Also, it is easy to come to such aconclusion out of ignorance of some of the existing features. It isprobably best not to complain about such a problem until you havechecked the documentation in the usual ways, feel confident that youunderstand it, and know for certain that what you want is notavailable. The mailing list[email protected] is a betterplace for discussions of this sort than the bug list.
If you are not sure what the command is supposed to doafter a careful reading of the manual this indicates a bug in themanual. The manual's job is to make everything clear. It is just asimportant to report documentation bugs as program bugs.
If the online argument list of a function disagrees with the manual,one of them must be wrong, so report the bug.
When you decide that there is a bug, it is important to report it andto report it in a way which is useful. What is most useful is anexact description of what commands you type, from when you startRuntil the problem happens. Always include the version ofR, machine,and operating system that you are using; typeversion inR toprint this. To help us keep track of which bugs have been fixed andwhich are still open please send a separate report for each bug.
The most important principle in reporting a bug is to report FACTS,not hypotheses or categorizations. It is always easier to report thefacts, but people seem to prefer to strain to posit explanations andreport them instead. If the explanations are based on guesses abouthowR is implemented, they will be useless; we will have to try tofigure out what the facts must have been to lead to suchspeculations. Sometimes this is impossible. But in any case, it isunnecessary work for us.
For example, suppose that on a data set which you know to be quitelarge the commanddata.frame(x, y, z, monday, tuesday) neverreturns. Do not report thatdata.frame() fails for large datasets. Perhaps it fails when a variable name is a day of the week. Ifthis is so then when we got your report we would try out thedata.frame() command on a large data set, probably with no dayof the week variable name, and not see any problem. There is no way inthe world that we could guess that we should try a day of the weekvariable name.
Or perhaps the command fails because the last command you used was a[ method that had a bug causingR's internal data structuresto be corrupted and making thedata.frame() command fail fromthen on. This is why we need to know what other commands you havetyped (or read from your startup file).
It is very useful to try and find simple examples that produceapparently the same bug, and somewhat useful to find simple examplesthat might be expected to produce the bug but actually do not. If youwant to debug the problem and find exactly what caused it, that iswonderful. You should still report the facts as well as anyexplanations or solutions.
InvokingR with the--vanilla option may help in isolating abug. This ensures that the site profile and saved data files are notread.
A bug report can be generated using the functionbug.report().For reports onR this will open the Web page athttps://bugs.r-project.org/: for a contributed package it willopen the package's bug tracker Web page or help you compose an emailto the maintainer.
Bug reports oncontributed packages should not be sent to theR bug tracker: rather make use of thepackage argument.
This help page is adapted from the Emacs manual and the R FAQ
help.request which you possibly should trybeforebug.report.
create.post, which handles emailing reports.
The R FAQ, alsosessionInfo() from which you may addto the bug report.
Evaluates its arguments with the output being returned as a characterstring or sent to a file. Related tosink similarly to howwith is related toattach.
capture.output(..., file = NULL, append = FALSE, type = c("output", "message"), split = FALSE)capture.output(..., file=NULL, append=FALSE, type= c("output","message"), split=FALSE)
... | Expressions to be evaluated. |
file | A file name or aconnection, or |
append | logical. If |
type,split | are passed to |
It works viasink(<file connection>) and hence theR codeindots mustnot interfere with the connection (e.g., bycallingcloseAllConnections()).
An attempt is made to write output as far as possible tofileif there is an error in evaluating the expressions, but forfile = NULL all output will be lost.
Messages sent tostderr() (including those frommessage,warning andstop)are captured bytype = "message". Note that this can be“unsafe” and should only be used with care.
A character string (iffile = NULL), or invisibleNULL.
require(stats)glmout <- capture.output(summary(glm(case ~ spontaneous+induced, data = infert, family = binomial())))glmout[1:5]capture.output(1+1, 2+2)capture.output({1+1; 2+2})## Not run: ## on Unix-alike with a2ps availableop <- options(useFancyQuotes=FALSE)pdf <- pipe("a2ps -o - | ps2pdf - tempout.pdf", "w")capture.output(example(glm), file = pdf)close(pdf); options(op) ; system("evince tempout.pdf &")## End(Not run)require(stats)glmout<- capture.output(summary(glm(case~ spontaneous+induced, data= infert, family= binomial())))glmout[1:5]capture.output(1+1,2+2)capture.output({1+1;2+2})## Not run: ## on Unix-alike with a2ps availableop<- options(useFancyQuotes=FALSE)pdf<- pipe("a2ps -o - | ps2pdf - tempout.pdf","w")capture.output(example(glm), file= pdf)close(pdf); options(op); system("evince tempout.pdf &")## End(Not run)
fileSnapshot takes a snapshot of a selection of files,recording summary information about each.changedFilescompares two snapshots, or compares one snapshot to the current stateof the file system. The snapshots need not be the same directory;this could be used to compare two directories.
fileSnapshot(path = ".", file.info = TRUE, timestamp = NULL, md5sum = FALSE, digest = NULL, full.names = length(path) > 1, ...) changedFiles(before, after, path = before$path, timestamp = before$timestamp, check.file.info = c("size", "isdir", "mode", "mtime"), md5sum = before$md5sum, digest = before$digest, full.names = before$full.names, ...) ## S3 method for class 'fileSnapshot'print(x, verbose = FALSE, ...)## S3 method for class 'changedFiles'print(x, verbose = FALSE, ...)fileSnapshot(path=".", file.info=TRUE, timestamp=NULL, md5sum=FALSE, digest=NULL, full.names= length(path)>1,...) changedFiles(before, after, path= before$path, timestamp= before$timestamp, check.file.info= c("size","isdir","mode","mtime"), md5sum= before$md5sum, digest= before$digest, full.names= before$full.names,...)## S3 method for class 'fileSnapshot'print(x, verbose=FALSE,...)## S3 method for class 'changedFiles'print(x, verbose=FALSE,...)
path | character vector; the path(s) to record. |
file.info | logical; whether to record |
timestamp | character string or |
md5sum | logical; whether MD5 summaries of each file should betaken as part of the snapshot. |
digest | a function or |
full.names | logical; whether full names (as in |
... | additional parameters to pass to |
before,after | objects produced by |
check.file.info | character vector; which columns from |
x | the object to print. |
verbose | logical; whether to list all data when printing. |
ThefileSnapshot function useslist.files toobtain a list of files, and depending on thefile.info,md5sum, anddigest arguments, records information abouteach file.
ThechangedFiles function compares two snapshots.
If thetimestamp argument tofileSnapshot is length 1, afile with that name is created. If it is length 1 inchangedFiles, thefile_test function is used tocompare the age of all files common to bothbefore andafter to it. This test may be unreliable: it compares thecurrent modification time of theafter files to the timestamp;that may not be the same as the modification time when theafter snapshot was taken. It may also give incorrect resultsif the clock on the file system holding the timestamp differs from theone holding the snapshot files.
If thecheck.file.info argument contains a non-empty charactervector, the indicated columns from the result of a call tofile.info will be compared.
Ifmd5sum isTRUE,fileSnapshot will call thetools::md5sum function to record the 32 byte MD5checksum for each file, andchangedFiles will compare thevalues. Thedigest argument allows users to provide their owndigest function.
fileSnapshot returns an object of class"fileSnapshot".This is a list containing the fields
info | a data frame whose rownames are the filenames, and whosecolumns contain the requested snapshot data |
path | the normalized |
timestamp,file.info,md5sum,digest,full.names | a record ofthe other arguments from the call |
args | other arguments passed via |
changedFiles produces an object of class"changedFiles".This is a list containing
added,deleted,changed,unchanged | character vectors offilenames from the before and after snapshots, with obvious meanings |
changes | a logical matrix with a row for each common file, and acolumn for each comparison test. |
print methods are defined for each of these types. Theprint method for"fileSnapshot" objectsdisplays the arguments used to produce them, while the one for"changedFiles" displays theadded,deleted andchanged fields if non-empty, and a submatrix of thechanges matrix containing all of theTRUE values.
Duncan Murdoch, using suggestions from Karl Millar and others.
# Create some files in a temporary directorydir <- tempfile()dir.create(dir)writeBin(1L, file.path(dir, "file1"))writeBin(2L, file.path(dir, "file2"))dir.create(file.path(dir, "dir"))# Take a snapshotsnapshot <- fileSnapshot(dir, timestamp = tempfile("timestamp"), md5sum=TRUE) # Change one of the files.writeBin(3L:4L, file.path(dir, "file2"))# Display the detected changes. We may or may not see mtime change...changedFiles(snapshot)changedFiles(snapshot)$changes# Create some files in a temporary directorydir<- tempfile()dir.create(dir)writeBin(1L, file.path(dir,"file1"))writeBin(2L, file.path(dir,"file2"))dir.create(file.path(dir,"dir"))# Take a snapshotsnapshot<- fileSnapshot(dir, timestamp= tempfile("timestamp"), md5sum=TRUE)# Change one of the files.writeBin(3L:4L, file.path(dir,"file2"))# Display the detected changes. We may or may not see mtime change...changedFiles(snapshot)changedFiles(snapshot)$changes
An interface to the (C99) wide character classification functions in use.
charClass(x, class)charClass(x, class)
x | Either a UTF-8-encoded length-1 character vectoror an integer vector of Unicode points (or a vectorcoercible to integer). |
class | A character string, one of those given in the‘Details’ section. |
The classification into character classes is platform-dependent. Theclasses are determined by internal tables on Windows and (optionallybut by default) on macOS and AIX.
The character classes are interpreted as follows:
"alnum"Alphabetic or numeric.
"alpha"Alphabetic.
"blank"Space or tab.
"cntrl"Control characters.
"digit"Digits0-9.
"graph"Graphical characters (printable charactersexcept whitespace).
"lower"Lower-case alphabetic.
"print"Printable characters.
"punct"Punctuation characters. Some platforms treat allnon-alphanumeric graphical characters as punctuation.
"space"Whitespace, including tabs, form and linefeeds and carriage returns. Some OSes include non-breakingspaces, some exclude them.
"upper"Upper-case alphabetic.
"xdigit"Hexadecimal character, one of0-9A-fa-f.
Alphabetic characters contain all lower- and upper-case ones and someothers (for example, those in ‘title case’).
Whether a character is printable is used to decide whether to escapeit when printing – see the help forprint.default.
Ifx is a character string it should either be ASCII or declaredas UTF-8 – seeEncoding.
charClass was added inR 4.1.0. A less direct way to examinecharacter classes which also worked in earlier versions is to usesomething likegrepl("[[:print:]]", intToUtf8(x)) – however,the regular-expression code might not use the same classificationfunctions as printing and on macOS used not to.
A logical vector of the length the number of characters or integers inx.
Non-ASCII digits are excluded by the C99 standard from the class"digit": most platforms will have them as alphabetic.
It is an assumption that the system's wide character classificationfunctions are coded in Unicode points, but this is known to be truefor all recent platforms.
The classification may depend on the locale even on one platform.
Character classes are used inregular expressions.
The OS'sman pages foriswctype andwctype.
x <- c(48:70, 32, 0xa0) # Last is non-breaking spacecl <- c("alnum", "alpha", "blank", "digit", "graph", "punct", "upper", "xdigit")X <- lapply(cl, function(y) charClass(x,y)); names(X) <- clX <- as.data.frame(X); row.names(X) <- sQuote(intToUtf8(x, multiple = TRUE))XcharClass("ABC123", "alpha")## Some accented capital Greek characters(x <- "\u0386\u0388\u0389")charClass(x, "upper")## How many printable characters are there? (Around 280,000 in Unicode 13.)## There are 2^21-1 possible Unicode points (most not yet assigned).pr <- charClass(1:0x1fffff, "print") table(pr)x<- c(48:70,32,0xa0)# Last is non-breaking spacecl<- c("alnum","alpha","blank","digit","graph","punct","upper","xdigit")X<- lapply(cl,function(y) charClass(x,y)); names(X)<- clX<- as.data.frame(X); row.names(X)<- sQuote(intToUtf8(x, multiple=TRUE))XcharClass("ABC123","alpha")## Some accented capital Greek characters(x<-"\u0386\u0388\u0389")charClass(x,"upper")## How many printable characters are there? (Around 280,000 in Unicode 13.)## There are 2^21-1 possible Unicode points (most not yet assigned).pr<- charClass(1:0x1fffff,"print") table(pr)
Use a Windows shell folder widget to choose a folder interactively.
choose.dir(default = "", caption = "Select folder")choose.dir(default="", caption="Select folder")
default | which folder to show initially. |
caption | the caption on the selection dialog. |
This brings up the Windows shell folder selection widget. With thedefaultdefault = "", ‘My Computer’ (or similar) isinitially selected.
To workaround a bug, on Vista and later only folders under‘Computer’ are accessible via the widget.
A length-one character vector, characterNA if‘Cancel’ was selected.
This is only available on Windows.
choose.files (on Windows) andfile.choose(on all platforms).
if (interactive() && .Platform$OS.type == "windows") choose.dir(getwd(), "Choose a suitable folder")if(interactive()&& .Platform$OS.type=="windows") choose.dir(getwd(),"Choose a suitable folder")
Use a Windows file dialog to choose a list of zero or more filesinteractively.
choose.files(default = "", caption = "Select files", multi = TRUE, filters = Filters, index = nrow(Filters))Filterschoose.files(default="", caption="Select files", multi=TRUE, filters= Filters, index= nrow(Filters))Filters
default | which filename to show initially |
caption | the caption on the file selection dialog |
multi | whether to allow multiple files to be selected |
filters | a matrix of filename filters (see Details) |
index | which row of filters to use by default |
Unlikefile.choose,choose.files will alwaysattempt to return a character vector giving a list of files. If theuser cancels the dialog, then zero files are returned, whereasfile.choose would signal an error.choose.dirchooses a directory.
Windows file dialog boxes include a list of ‘filters’, which allowthe file selection to be limited to files of specific types.Thefilters argument tochoose.files allows the listof filters to be set. It should be ann by2 charactermatrix. The first column gives, for each filter, the description theuser will see, while the second column gives the mask(s) to selectthose files. If more than one mask is used, separate them bysemicolons, with no spaces. Theindex argument chooses whichfilter will be used initially.
Filters is a matrix giving the descriptions and masks forthe file types thatR knows about. Print it to see typical formatsfor filter specifications. The examples below show how particularfilters may be selected.
If you would like to display files in a particular directory,give a fully qualified file mask (e.g.,"c:\\*.*")in thedefault argument. If a directory is not given, thedialog will start in the current directory the first time, andremember the last directory used on subsequent invocations.
There is a buffer limit on the total length of the selected filenames:it is large but this function is not intended to select thousands offiles, when the limit might be reached.
A character vector giving zero or more file paths.
This is only available on Windows.
Sys.glob orlist.files to select multiplefiles by pattern.
if (interactive() && .Platform$OS.type == "windows") choose.files(filters = Filters[c("zip", "All"),])if(interactive()&& .Platform$OS.type=="windows") choose.files(filters= Filters[c("zip","All"),])
Interact with the user to choose a Bioconductor mirror.
chooseBioCmirror(graphics = getOption("menu.graphics"), ind = NULL, local.only = FALSE)chooseBioCmirror(graphics= getOption("menu.graphics"), ind=NULL, local.only=FALSE)
graphics | Logical. If true, use a graphical list: on Windows orthe macOS GUI use a list box, and on a Unix-alike use a Tk widget ifpackagetcltk and an X server are available. Otherwise use atext |
ind | Optional numeric value giving which entry to select. |
local.only | Logical, try to get most recent list from theBioconductor master or use file on local disk only. |
This sets theoption"BioC_mirror": it is usedbefore a call tosetRepositories. The out-of-the-boxdefault for that option isNULL, which currently corresponds tothe mirrorhttps://bioconductor.org.
The ‘Bioconductor (World-wide)’ ‘mirror’ is a network ofmirrors providing reliable world-wide access; other mirrors mayprovide faster access on a geographically local scale.
ind chooses a row in‘R_HOME/doc/BioC_mirrors.csv’,by number.
None: this function is invoked for itsside effect of updatingoptions("BioC_mirror").
setRepositories,chooseCRANmirror.
Interact with the user to choose a CRAN mirror.
chooseCRANmirror(graphics = getOption("menu.graphics"), ind = NULL, local.only = FALSE)getCRANmirrors(all = FALSE, local.only = FALSE)chooseCRANmirror(graphics= getOption("menu.graphics"), ind=NULL, local.only=FALSE)getCRANmirrors(all=FALSE, local.only=FALSE)
graphics | Logical. If true, use a graphical list: on Windows orthe macOS GUI use a list box, and on a Unix-alike use a Tk widget ifpackagetcltk and an X server are available. Otherwise use atext |
ind | Optional numeric value giving which entry to select. |
all | Logical, get all known mirrors or only the ones flagged as OK. |
local.only | Logical, try to get most recent list from the CRANmaster or use file on local disk only. |
A list of mirrors is stored in file‘R_HOME/doc/CRAN_mirrors.csv’, but first an on-linelist of current mirrors is consulted, and the file copy used only ifthe on-line list is inaccessible.
chooseCRANmirror is called by a Windows GUI menu item and bycontrib.url if it finds the initial dummy value ofoptions("repos").
ind chooses a row in the list of current mirrors, by number. Itis best used withlocal.only = TRUE and row numbers in‘R_HOME/doc/CRAN_mirrors.csv’.
None forchooseCRANmirror(), this function is invoked for itsside effect of updatingoptions("repos").
getCRANmirrors() returns a data frame with mirror information.
setRepositories,findCRANmirror,chooseBioCmirror,contrib.url.
How to citeR andR packages in publications.
citation(package = "base", lib.loc = NULL, auto = NULL)readCitationFile(file, meta = NULL)citHeader(...)citFooter(...)citation(package="base", lib.loc=NULL, auto=NULL)readCitationFile(file, meta=NULL)citHeader(...)citFooter(...)
package | a character string with the name of a single package.An error occurs if more than one package name is given. |
lib.loc | a character vector with path names ofR libraries, orthe directory containing the source for |
auto | a logical indicating whether the default citationauto-generated from the package ‘DESCRIPTION’ metadata shouldbe used or not, or |
file | a file name. |
meta | a list of package metadata as obtained by |
... | character strings (which will be |
TheR core development team and the very active community of packageauthors have invested a lot of time and effort in creatingR as it istoday. Please give credit where credit is due and citeR andRpackages when you use them for data analysis.
Usecitation() (without arguments) for information on how tocite the base R system in publications.
Ifcitation() is called withpackage the name of anon-base package, as controlled by theauto argument it eitherreturns the information contained in the package ‘CITATION’ fileor auto-generates citation information from the package‘DESCRIPTION’ file. By default (auto = NULL), the‘CITATION’ file is used if it exists, in which case it is readviareadCitationFile withmeta equal topackageDescription(package, lib.loc). One can forceauto-generation viaauto = TRUE.
The auto-generated citation includesURLs for packagesinstalled from the standard repositories CRAN and Bioconductor andfrom development platforms such as GitHub, GitLab, orR-Forge. In case of CRAN and Bioconductor,DOIs areincluded as well.
Packages can use an ‘Authors@R’ field in their‘DESCRIPTION’ to provide (R code giving) aperson object with a refined, machine-readabledescription of the package “authors” (in particular specifyingtheir precise roles). Only those with an author role will beincluded in the auto-generated citation.
If the object returned bycitation() contains only one reference,the associated print method shows both a text version and a BibTeXentry for it. If a package has more than one reference then only thetext versions are shown. This threshold is controlled byoptions("citation.bibtex.max").The BibTeX versions can also be obtained usingfunctiontoBibtex() (see the examples below).
The ‘CITATION’ file of an R package should be placed in the‘inst’ subdirectory of the package source. The file is an Rsource file and may contain arbitrary R commands includingconditionals and computations. FunctionreadCitationFile() isused bycitation() to extract the information in‘CITATION’ files. The file issource()ed by the Rparser in a temporary environment and all resulting bibliographicobjects (specifically, inheriting from"bibentry") arecollected.These are typically produced by one or morebibentry()calls, optionally preceded by acitHeader() and followedby acitFooter() call.One can include an auto-generated package citation in the‘CITATION’ file viacitation(auto = meta).
readCitationFile makes use of theEncoding element (ifany) ofmeta to determine the encoding of the file.
An object of class"citation", inheriting from class"bibentry"; see there, notably for theprint andformat methods.
citHeader andcitFooter return an empty"bibentry" storing “outer” header/footer textfor the package citation.
## the basic R referencecitation()## extract the BibTeX entry from the return valuex <- citation()toBibtex(x)## references for a packagecitation("lattice")citation("lattice", auto = TRUE) # request the Manual-type referencecitation("foreign")## a CITATION file with more than one bibentry:file.show(system.file("CITATION", package="mgcv"))cm <- citation("mgcv")cm # header, text references, plus "reminder" about getting BibTeXprint(cm, bibtex = TRUE) # each showing its bibtex code## a CITATION file including citation(auto = meta)file.show(system.file("CITATION", package="nlme"))citation("nlme")## the basic R referencecitation()## extract the BibTeX entry from the return valuex<- citation()toBibtex(x)## references for a packagecitation("lattice")citation("lattice", auto=TRUE)# request the Manual-type referencecitation("foreign")## a CITATION file with more than one bibentry:file.show(system.file("CITATION", package="mgcv"))cm<- citation("mgcv")cm# header, text references, plus "reminder" about getting BibTeXprint(cm, bibtex=TRUE)# each showing its bibtex code## a CITATION file including citation(auto = meta)file.show(system.file("CITATION", package="nlme"))citation("nlme")
Cite abibentry object in text. Thecite() functionuses thecite() function from the defaultbibstyle if present, orciteNatbib() if not.citeNatbib() uses a style similar to that used by the LaTeXpackage ‘natbib’.
cite(keys, bib, ...)citeNatbib(keys, bib, textual = FALSE, before = NULL, after = NULL, mode = c("authoryear", "numbers", "super"), abbreviate = TRUE, longnamesfirst = TRUE, bibpunct = c("(", ")", ";", "a", "", ","), previous)cite(keys, bib,...)citeNatbib(keys, bib, textual=FALSE, before=NULL, after=NULL, mode= c("authoryear","numbers","super"), abbreviate=TRUE, longnamesfirst=TRUE, bibpunct= c("(",")",";","a","",","), previous)
keys | A character vector of keys of entries to cite. May contain multiple keys ina single entry, separated by commas. |
bib | A |
... | Additional arguments to pass to the |
textual | Produce a “textual” style of citation, i.e. what ‘\citet’ wouldproduce in LaTeX. |
before | Optional text to display before the citation. |
after | Optional text to display after the citation. |
mode | The “mode” of citation. |
abbreviate | Whether to abbreviate long author lists. |
longnamesfirst | If |
bibpunct | A vector of punctuation to use in the citation, as used in ‘natbib’. Seethe Details section. |
previous | A list of keys that have been previously cited, to be used when |
Argument names are chosen based on the documentation for the LaTeX ‘natbib’package. See that documentation for the interpretation of thebibpunct entries.
The entries inbibpunct are as follows:
The left delimiter.
The right delimiter.
The separator between references within a citation.
An indicator of the “mode”:"n" for numbers,"s" for superscripts, anything else for author-year.
Punctuation to go between the author and year.
Punctuation to go between years when authorship is suppressed.
Note that ifmode is specified, it overrides themode specification inbibpunct[4]. Partial matching is used formode.
The defaults forciteNatbib have been chosen to match theJSS style, andby default these are used incite. Seebibstylefor how to set a different default style.
A single element character string is returned, containing the citation.
Duncan Murdoch
## R referencerref <- bibentry( bibtype = "Manual", title = "R: A Language and Environment for Statistical Computing", author = person("R Core Team"), organization = "R Foundation for Statistical Computing", address = "Vienna, Austria", year = 2013, url = "https://www.R-project.org/", key = "R")## References for boot package and associated bookbref <- c( bibentry( bibtype = "Manual", title = "boot: Bootstrap R (S-PLUS) Functions", author = c( person("Angelo", "Canty", role = "aut", comment = "S original"), person(c("Brian", "D."), "Ripley", role = c("aut", "trl", "cre"), comment = "R port, author of parallel support", email = "[email protected]") ), year = "2012", note = "R package version 1.3-4", url = "https://CRAN.R-project.org/package=boot", key = "boot-package" ), bibentry( bibtype = "Book", title = "Bootstrap Methods and Their Applications", author = as.person("Anthony C. Davison [aut], David V. Hinkley [aut]"), year = "1997", publisher = "Cambridge University Press", address = "Cambridge", isbn = "0-521-57391-2", url = "http://statwww.epfl.ch/davison/BMA/", key = "boot-book" ))## Combine and citerefs <- c(rref, bref)cite("R, boot-package", refs)## Cite numericallysavestyle <- tools::getBibstyle()tools::bibstyle("JSSnumbered", .init = TRUE, fmtPrefix = function(paper) paste0("[", paper$.index, "]"), cite = function(key, bib, ...) citeNatbib(key, bib, mode = "numbers", bibpunct = c("[", "]", ";", "n", "", ","), ...) )cite("R, boot-package", refs, textual = TRUE)refs## restore the old styletools::bibstyle(savestyle, .default = TRUE)## R referencerref<- bibentry( bibtype="Manual", title="R: A Language and Environment for Statistical Computing", author= person("R Core Team"), organization="R Foundation for Statistical Computing", address="Vienna, Austria", year=2013, url="https://www.R-project.org/", key="R")## References for boot package and associated bookbref<- c( bibentry( bibtype="Manual", title="boot: Bootstrap R (S-PLUS) Functions", author= c( person("Angelo","Canty", role="aut", comment="S original"), person(c("Brian","D."),"Ripley", role= c("aut","trl","cre"), comment="R port, author of parallel support", email="[email protected]")), year="2012", note="R package version 1.3-4", url="https://CRAN.R-project.org/package=boot", key="boot-package"), bibentry( bibtype="Book", title="Bootstrap Methods and Their Applications", author= as.person("Anthony C. Davison [aut], David V. Hinkley [aut]"), year="1997", publisher="Cambridge University Press", address="Cambridge", isbn="0-521-57391-2", url="http://statwww.epfl.ch/davison/BMA/", key="boot-book"))## Combine and citerefs<- c(rref, bref)cite("R, boot-package", refs)## Cite numericallysavestyle<- tools::getBibstyle()tools::bibstyle("JSSnumbered", .init=TRUE, fmtPrefix=function(paper) paste0("[", paper$.index,"]"), cite=function(key, bib,...) citeNatbib(key, bib, mode="numbers", bibpunct= c("[","]",";","n","",","),...))cite("R, boot-package", refs, textual=TRUE)refs## restore the old styletools::bibstyle(savestyle, .default=TRUE)
Old interface providing functionality for specifying bibliographic information in enhanced BibTeX style. Since R 2.14.0 this has been superseded bybibentry.
citEntry(entry, textVersion = NULL, header = NULL, footer = NULL, ...)citEntry(entry, textVersion=NULL, header=NULL, footer=NULL,...)
entry | a character string with a BibTeX entry type.See sectionEntry Types in |
textVersion | a character string with a text representation ofthe reference to optionally be employed for printing. |
header | a character string with optional header text. |
footer | a character string with optional footer text. |
... | for |
citEntry produces an object of class"bibentry".
citation for more information about citing R and Rpackages and ‘CITATION’ files;bibentry for the newer functionality for representingand manipulating bibliographic information.
Transfer text between a character vector and the Windows clipboard inMS Windows (only).
getClipboardFormats(numeric = FALSE)readClipboard(format = 13, raw = FALSE)writeClipboard(str, format = 13)getClipboardFormats(numeric=FALSE)readClipboard(format=13, raw=FALSE)writeClipboard(str, format=13)
numeric | logical: should the result be in human-readable form(the default) or raw numbers? |
format | an integer giving the desired format. |
raw | should the value be returned as a raw vector rather thanas a character vector? |
str | a character vector or a raw vector. |
The Windows clipboard offers data in a number of formats: seee.g.https://learn.microsoft.com/en-gb/windows/desktop/dataxchg/clipboard-formats.
The standard formats include
| CF_TEXT | 1 | Text in the machine's locale |
| CF_BITMAP | 2 | |
| CF_METAFILEPICT | 3 | Metafile picture |
| CF_SYLK | 4 | Symbolic link |
| CF_DIF | 5 | Data Interchange Format |
| CF_TIFF | 6 | Tagged-Image File Format |
| CF_OEMTEXT | 7 | Text in theOEM codepage |
| CF_DIB | 8 | Device-Independent Bitmap |
| CF_PALETTE | 9 | |
| CF_PENDATA | 10 | |
| CF_RIFF | 11 | Audio data |
| CF_WAVE | 12 | Audio data |
| CF_UNICODETEXT | 13 | Text in Unicode (UCS-2) |
| CF_ENHMETAFILE | 14 | Enhanced metafile |
| CF_HDROP | 15 | Drag-and-drop data |
| CF_LOCALE | 16 | Locale for the text on the clipboard |
| CF_MAX | 17 | Shell-oriented formats |
Applications normally make data available in one or more of these andpossibly additional private formats. Useraw = TRUE to read binaryformats,raw = FALSE (the default) for text formats. Thecurrent codepage is used to convert text to Unicode text, andinformation on that is contained in theCF_LOCALE format.(Take care if you are running R in a different locale from Windows. It isrecommended to read as Unicode text, so that Windows does the conversionbased onCF_LOCALE, if available.)
ThewriteClipboard function will write a character vector astext or Unicode text with standardCRLF line terminators. It willcopy a raw vector directly to the clipboard without any changes. It isrecommended to use Unicode text (the default) instead of text to avoid interoperabilityproblems. (Note thatR 4.2 and newer on recent systems uses UTF-8 as thenative encoding but the machine's locale uses a different encoding.)
ForgetClipboardFormats, a character or integer vector ofavailable formats, in numeric order. If non human-readable characterrepresentation is known, the number is returned.
ForreadClipboard, a character vector by default, a raw vectorifraw isTRUE, orNULL, if the format isunavailable.
ForwriteClipboard an invisible logical indicating success orfailure.
This is only available on Windows.
file which can be used to set up a connection to a clipboard.
Closes the socket and frees the space in the file descriptor table. Theport may not be freed immediately.
close.socket(socket, ...)close.socket(socket,...)
socket | a |
... | further arguments passed to or from other methods. |
logical indicating success or failure
Thomas Lumley
Compiling in support for sockets was optional prior toR 3.3.0: seecapabilities("sockets") to see if it is available.
Generate all combinations of the elements ofx takenmat a time. Ifx is a positive integer, returns allcombinations of the elements ofseq(x) takenm at atime. If argumentFUN is notNULL, applies a function givenby the argument to each point. If simplify is FALSE, returnsa list; otherwise returns anarray, typically amatrix.... are passed unchanged to theFUN function, if specified.
combn(x, m, FUN = NULL, simplify = TRUE, ...)combn(x, m, FUN=NULL, simplify=TRUE,...)
x | vector source for combinations, or integer |
m | number of elements to choose. |
FUN | function to be applied to each combination; default |
simplify | logical indicating if the result should be simplifiedto an |
... | optionally, further arguments to |
Factorsx are accepted.
Alist orarray, see thesimplifyargument above. In the latter case, the identitydim(combn(n, m)) == c(m, choose(n, m)) holds.
Scott Chasalow wrote the original in 1994 for S;R packagecombinat and documentation by Vince Carey[email protected];small changes by the R core team, notably to return an array in allcases ofsimplify = TRUE, e.g., forcombn(5,5).
Nijenhuis, A. and Wilf, H.S. (1978)Combinatorial Algorithms for Computers and Calculators;Academic Press, NY.
choose for fast computation of thenumber ofcombinations.expand.grid for creating a data frame fromall combinations of factors or vectors.
combn(letters[1:4], 2)(m <- combn(10, 5, min)) # minimum value in each combinationmm <- combn(15, 6, function(x) matrix(x, 2, 3))stopifnot(round(choose(10, 5)) == length(m), is.array(m), # 1-dimensional c(2,3, round(choose(15, 6))) == dim(mm))## Different way of encoding points:combn(c(1,1,1,1,2,2,2,3,3,4), 3, tabulate, nbins = 4)## Compute support points and (scaled) probabilities for a## Multivariate-Hypergeometric(n = 3, N = c(4,3,2,1)) p.f.:# table.mat(t(combn(c(1,1,1,1,2,2,2,3,3,4), 3, tabulate, nbins = 4)))## Assuring the identityfor(n in 1:7) for(m in 0:n) stopifnot(is.array(cc <- combn(n, m)), dim(cc) == c(m, choose(n, m)), identical(cc, combn(n, m, identity)) || m == 1)combn(letters[1:4],2)(m<- combn(10,5, min))# minimum value in each combinationmm<- combn(15,6,function(x) matrix(x,2,3))stopifnot(round(choose(10,5))== length(m), is.array(m),# 1-dimensional c(2,3, round(choose(15,6)))== dim(mm))## Different way of encoding points:combn(c(1,1,1,1,2,2,2,3,3,4),3, tabulate, nbins=4)## Compute support points and (scaled) probabilities for a## Multivariate-Hypergeometric(n = 3, N = c(4,3,2,1)) p.f.:# table.mat(t(combn(c(1,1,1,1,2,2,2,3,3,4), 3, tabulate, nbins = 4)))## Assuring the identityfor(nin1:7)for(min0:n) stopifnot(is.array(cc<- combn(n, m)), dim(cc)== c(m, choose(n, m)), identical(cc, combn(n, m, identity))|| m==1)
Compare two package version numbers to see which is later.
compareVersion(a, b)compareVersion(a, b)
a,b | Character strings representing package version numbers. |
R package version numbers are of the formx.y-z for integersx,y andz, with components afterxoptionally missing (in which case the version number is older thanthose with the components present).
0 if the numbers are equal,-1 ifb is laterand1 ifa is later (analogous to the C functionstrcmp).
Gives anR error on malformed inputs.
package_version,library,packageStatus.
compareVersion("1.0", "1.0-1")compareVersion("7.2-0","7.1-12")compareVersion("1.0","1.0-1")compareVersion("7.2-0","7.1-12")
Compile given source files so that they can subsequently be collectedinto a shared object usingR CMD SHLIB or an executableprogram usingR CMD LINK. Not available on Windows.
R CMD COMPILE [options] srcfilesR CMD COMPILE[options] srcfiles
srcfiles | A list of the names of source files to be compiled.Currently, C, C++, Objective C, Objective C++ and Fortran aresupported; the corresponding files should have the extensions‘.c’, ‘.cc’ (or ‘.cpp’), ‘.m’,‘.mm’ (or ‘.M’), ‘.f’ and ‘.f90’ or ‘.f95’,respectively. |
options | A list of compile-relevant settings, or for obtaininginformation about usage and version of the utility. |
R CMD SHLIB can both compile and link files into ashared object: since it knows what run-time libraries are neededwhen passed C++, Fortran and Objective C(++) sources, passing sourcefiles toR CMD SHLIB is more reliable.
Objective C and Objective C++ support is optional and will work onlyif the corresponding compilers were available atR configure time:their main usage is on macOS.
Compilation arranges to include the paths to theR public C/C++ headers.
As this compiles code suitable for incorporation into a shared object,it generates PIC code: that might occasionally be undesirable for themain code of an executable program.
This is amake-based facility, so will not compile a source fileif a newer corresponding ‘.o’ file is present.
Some binary distributions ofR haveCOMPILE in a separatebundle, e.g. anR-devel RPM.
This is not available on Windows.
The section on “Customizing package compilation” inthe ‘R Administration and Installation’ manual:RShowDoc("R-admin").
contrib.url adds the appropriate type-specific path within arepository to each URL inrepos.
contrib.url(repos, type = getOption("pkgType"))contrib.url(repos, type= getOption("pkgType"))
repos | character vector, the base URL(s) of the repositoriesto use. |
type | character string, indicating which type of packages: see |
Iftype = "both" this will use the source repository.
A character vector of the same length asrepos.
setRepositories to setoptions("repos"),the most common value used for argumentrepos.
available.packages,download.packages,install.packages.
The ‘R Installation and Administration’ manual for how toset up a repository.
count.fields counts the number of fields, as separated bysep, in each of the lines offile read.
count.fields(file, sep = "", quote = "\"'", skip = 0, blank.lines.skip = TRUE, comment.char = "#")count.fields(file, sep="", quote="\"'", skip=0, blank.lines.skip=TRUE, comment.char="#")
file | a character string naming an ASCII data file, or a |
sep | the field separator character. Values on each line of thefile are separated by this character. By default, arbitrary amountsof whitespace can separate fields. |
quote | the set of quoting characters |
skip | the number of lines of the data file to skip beforebeginning to read data. |
blank.lines.skip | logical: if |
comment.char | character: a character vector of length onecontaining a single character or an empty string. |
This used to be used byread.table and can still beuseful in discovering problems in reading a file by that function.
For the handling of comments, seescan.
Consistent withscan,count.fields allowsquoted strings to contain newline characters. In such a case thestarting line will have the field count recorded asNA, andthe ending line will include the count of all fields from thebeginning of the record.
A vector with the numbers of fields found.
fil <- tempfile()cat("NAME", "1:John", "2:Paul", file = fil, sep = "\n")count.fields(fil, sep = ":")unlink(fil)fil<- tempfile()cat("NAME","1:John","2:Paul", file= fil, sep="\n")count.fields(fil, sep=":")unlink(fil)
An ancillary function used bybug.report andhelp.request to prepare emails for submission to packagemaintainers or toR mailing lists.
create.post(instructions = character(), description = "post", subject = "", method = getOption("mailer"), address = "the relevant mailing list", ccaddress = getOption("ccaddress", ""), filename = "R.post", info = character())create.post(instructions= character(), description="post", subject="", method= getOption("mailer"), address="the relevant mailing list", ccaddress= getOption("ccaddress",""), filename="R.post", info= character())
instructions | Character vector of instructions to put at the topof the template email. |
description | Character string: a description to be incorporatedinto messages. |
subject | Subject of the email. Optional except for the |
method | Submission method, one of |
address | Recipient's email address, where applicable: forpackage bug reports sent by email this defaults to the address ofthe package maintainer (the first if more than one is listed). |
ccaddress | Optional email address for copies with the |
filename | Filename to use for setting up the email (or storing it whenmethod is |
info | character vector of information to include in the templateemail below the ‘please do not edit the information below’ line. |
What this does depends on themethod. The function firstcreates a template email body.
noneA file editor (seefile.edit) isopened with instructions and the template email. When this returns,the completed email is in filefile ready to be read/pastedinto an email program.
mailtoThis opens the default email program with a template email(including address, Cc: address and subject) for you to edit andsend.
This works where default mailers are set up (usual on macOS andWindows, and wherexdg-open is available and configured onother Unix-alikes: if that fails it tries the browser set byR_BROWSER).
This is the ‘factory-fresh’ default method.
mailx(Unix-alikes only.)A file editor (seefile.edit) isopened with instructions and the template email. When thisreturns, it is mailed using a Unix command line mail utility suchasmailx, to the address (and optionally, the Cc: address)given.
gnudoitAn (X)emacs mail buffer is opened for the email to be edited andsent: this requires thegnudoit program to beavailable. Currentlysubject is ignored.
essThe body of the template email is sent tostdout.
InvisibleNULL.
Loads specified data sets, or list the available data sets.
data(..., list = character(), package = NULL, lib.loc = NULL, verbose = getOption("verbose"), envir = .GlobalEnv, overwrite = TRUE)data(..., list= character(), package=NULL, lib.loc=NULL, verbose= getOption("verbose"), envir= .GlobalEnv, overwrite=TRUE)
... | literal character strings or names. |
list | a character vector. |
package | a character vector giving the package(s) to lookin for data sets, or By default, all packages in the search path are used, thenthe ‘data’ subdirectory (if present) of the current workingdirectory. |
lib.loc | a character vector of directory names ofR libraries,or |
verbose | a logical. If |
envir | theenvironment where the data should be loaded. |
overwrite | logical: should existing objects of the same name inenvir be replaced? |
Currently, four formats of data files are supported:
files ending ‘.R’ or ‘.r’ aresource()d in, with theR working directory changedtemporarily to the directory containing the respective file.(data ensures that theutils package is attached, incase it had been runviautils::data.)
files ending ‘.RData’ or ‘.rdata’ or ‘.rda’ areload()ed.
files ending ‘.tab’, ‘.txt’ or ‘.TXT’ are readusingread.table(..., header = TRUE, as.is=FALSE),and henceresult in a data frame.
files ending ‘.csv’ or ‘.CSV’ are read usingread.table(..., header = TRUE, sep = ";", as.is=FALSE),and also result in a data frame.
If more than one matching file name is found, the first on this listis used. (Files with extensions ‘.txt’, ‘.tab’ or‘.csv’ can be compressed, with or without further extension‘.gz’, ‘.bz2’ or ‘.xz’.)
The data sets to be loaded can be specified as a set of characterstrings or names, or as the character vectorlist, or as both.
For each given data set, the first two types (‘.R’ or ‘.r’,and ‘.RData’ or ‘.rda’ files) can create several variablesin the load environment, which might all be named differently from thedata set. The third and fourth types will always result in thecreation of a single variable with the same name (without extension)as the data set.
If no data sets are specified,data lists the available datasets. For each package,it looks for a data index in the ‘Meta’ subdirectory or, ifthis is not found, scans the ‘data’ subdirectory for data filesusinglist_files_with_type.The information aboutavailable data sets is returned in an object of class"packageIQR". The structure of this class is experimental.Where the datasets have a different name from the argument that shouldbe used to retrieve them the index will have an entry likebeaver1 (beavers) which tells us that datasetbeaver1can be retrieved by the calldata(beavers).
Iflib.loc andpackage are bothNULL (thedefault), the data sets are searched for in all the currently loadedpackages then in the ‘data’ directory (if any) of the currentworking directory.
Iflib.loc = NULL butpackage is specified as acharacter vector, the specified package(s) are searched for firstamongst loaded packages and then in the default libraries(see.libPaths).
Iflib.locis specified (and notNULL), packagesare searched for in the specified libraries, even if they arealready loaded from another library.
To just look in the ‘data’ directory of the current workingdirectory, setpackage = character(0)(andlib.loc = NULL, the default).
A character vector of all data sets specified (whether found or not),or information about all available data sets in an object of class"packageIQR" if none were specified.
There is no requirement fordata(foo) to create an objectnamedfoo (nor to create one object), although it muchreduces confusion if this convention is followed (and it is enforcedif datasets are lazy-loaded).
data() was originally intended to allow users to load datasetsfrom packages for use in their examples, and as such it loaded thedatasets into the workspace.GlobalEnv. This avoidedhaving large datasets in memory when not in use: that need has beenalmost entirely superseded by lazy-loading of datasets.
The ability to specify a dataset by name (without quotes) is aconvenience: in programming the datasets should be specified bycharacter strings (with quotes).
Use ofdata within a function without anenvir argumenthas the almost always undesirable side-effect of putting an object inthe user's workspace (and indeed, of replacing any object of that namealready there). It would almost always be better to put the object inthe current evaluation environment bydata(..., envir = environment()).However, two alternatives are usually preferable,both described in the ‘Writing R Extensions’ manual.
For sets of data, set up a package to use lazy-loading of data.
For objects which are system data, for example lookup tablesused in calculations within the function, use a file‘R/sysdata.rda’ in the package sources or create the objects byR code at package installation time.
A sometimes important distinction is that the second approach placesobjects in the namespace but the first does not. So if it is importantthat the function seesmytable as an object from the package,it is system data and the second approach should be used. In theunusual case that a package uses a lazy-loaded dataset as a defaultargument to a function, that needs to be specified by::,e.g.,survival::survexp.us.
This function creates objects in theenvir environment (bydefault the user's workspace) replacing any which alreadyexisted.data("foo") can silently create objects other thanfoo: there have been instances in published packages where itcreated/replaced.Random.seed and hence change the seedfor the session.
One can take advantage of the search order and the fact that a‘.R’ file will change directory. If raw data are stored in‘mydata.txt’ then one can set up ‘mydata.R’ to read‘mydata.txt’ and pre-process it, e.g., usingtransform().For instance one can convert numeric vectors to factors with theappropriate labels. Thus, the ‘.R’ file can effectively containa metadata specification for the plaintext formats.
help for obtaining documentation on data sets,save forcreating the second (‘.rda’) kindof data, typically the most efficient one.
The ‘Writing R Extensions’ manual for considerations in preparing the‘data’ directory of a package.
require(utils)data() # list all available data setstry(data(package = "rpart"), silent = TRUE) # list the data sets in the rpart packagedata(USArrests, "VADeaths") # load the data sets 'USArrests' and 'VADeaths'## Not run: ## Alternativelyds <- c("USArrests", "VADeaths"); data(list = ds)## End(Not run)help(USArrests) # give information on data set 'USArrests'require(utils)data()# list all available data setstry(data(package="rpart"), silent=TRUE)# list the data sets in the rpart packagedata(USArrests,"VADeaths")# load the data sets 'USArrests' and 'VADeaths'## Not run: ## Alternativelyds<- c("USArrests","VADeaths"); data(list= ds)## End(Not run)help(USArrests)# give information on data set 'USArrests'
A spreadsheet-like editor for entering or editing data.
data.entry(..., Modes = NULL, Names = NULL)dataentry(data, modes)de(..., Modes = list(), Names = NULL)data.entry(..., Modes=NULL, Names=NULL)dataentry(data, modes)de(..., Modes= list(), Names=NULL)
... | A list of variables: currently these should be numeric orcharacter vectors or list containing such vectors. |
Modes | The modes to be used for the variables. |
Names | The names to be used for the variables. |
data | A list of numeric and/or character vectors. |
modes | A list of length up to that of |
The data entry editor is only available on some platforms and GUIs.Where available it provides a means to visually edit a matrix ora collection of variables (including a data frame) as described in theNotes section.
data.entry has side effects, any changes made in thespreadsheet are reflected in the variables. Functionde andthe internal functionsde.ncols,de.setupandde.restore are designed to help achieve these side effects. If the user passes in a matrix,X say, then the matrix is broken into columns beforedataentry is called. Then on return the columns are collectedand glued back together and the result assigned to the variableX. If you don't want this behaviour usedataentry directly.
The primitive function isdataentry. It takes a list ofvectors of possibly different lengths and modes (the second argument)and opens a spreadsheet with these variables being the columns.The columns of the data entry window are returned as vectors in alist when the spreadsheet is closed.
de.ncols counts the number of columns which are supplied as argumentstodata.entry. It attempts to count columns in lists, matricesand vectors.de.setup sets things up so that on return thecolumns can be regrouped and reassigned to the correct name. Thisis handled byde.restore.
de anddataentry return the edited value of theirarguments.data.entry invisibly returns a vector of variablenames but its main value is its side effect of assigning new versionof those variables in the user's workspace.
The data entry window responds to X resources of classR_dataentry. Resourcesforeground,background andgeometry are utilized.
The details of interface to the data grid may differ by platform andGUI. The following description applies tothe X11-based implementation under Unix.
You can navigate around the grid using the cursor keys or by clickingwith the (left) mouse button on any cell. The active cell ishighlighted by thickening the surrounding rectangle. Moving to theright or down will scroll the grid as needed: there is no constraintto the rows or columns currently in use.
There are alternative ways to navigate using the keys. Return and(keypad) Enter and LineFeed all move down. Tab moves right andShift-Tab move left. Home moves to the top left.
PageDown or Control-F moves down a page, and PageUp orControl-B up by a page. End will show the last used column and thelast few rows used (in any column).
Using any other key starts an editing process on the currentlyselected cell: moving away from that cell enters the edited valuewhereas Esc cancels the edit and restores the previous value. Whenthe editing process starts the cell is cleared.
In numerical columns(the default) only letters making up a valid number (including-.eE) are accepted, and entering an invalid edited value (suchas blank) entersNA in that cell. The last entered value canbe deleted using the BackSpace or Del(ete) key. Only a limitednumber of characters (currently 29) can be entered in a cell, and ifnecessary only the start or end of the string will be displayed, with theomissions indicated by> or<. (The start is shownexcept when editing.)
Entering a value in a cell further down a column than the last usedcell extends the variable and fills the gap (if any) byNAs (notshown on screen).
The column names can only be selected by clicking in them. This givesa popup menu to select the column type (currently Real (numeric) orCharacter) or to change the name. Changing the type converts thecurrent contents of the column (and converting from Character to Realmay generateNAs.)If changing the name is selected theheader cell becomes editable (and is cleared). As with all cells, thevalue is entered by moving away from the cell by clicking elsewhere orby any of the keys for moving down (only).
New columns are created by entering values in them (and not by justassigning a new name). The mode of the column is auto-detected fromthe first value entered: if this is a valid number it gives a numericcolumn. Unused columns are ignored, soadding data invar5 to a three-column grid adds one extravariable, not two.
TheCopy button copies the currently selected cell:paste copies the last copied value to the current cell, andright-clicking selects a celland copies in the value.Initially the value is blank, and attempts to paste a blank value willhave no effect.
Control-L will refresh the display, recalculating field widths to fitthe current entries.
In the default mode the column widths are chosen to fit the contentsof each column, with a default of 10 characters for empty columns.you can specify fixed column widths by setting optionde.cellwidth to the required fixed width (in characters).(set it to zero to return to variable widths). The displayedwidth of any field is limited to600 pixels (and by the window width).
vi,edit:edit usesdataentry to edit data frames.
# call data entry with variables x and y## Not run: data.entry(x, y)# call data entry with variables x and y## Not run: data.entry(x, y)
Set or unset debugging flags based on a call to a function. Takes intoaccount S3/S4 method dispatch based on the classes of the arguments inthe call.
debugcall(call, once = FALSE)undebugcall(call)debugcall(call, once=FALSE)undebugcall(call)
call | An R expression calling a function. The called functionwill be debugged. See Details. |
once | logical; if |
debugcall debugs the non-generic function, S3 method or S4method that would be called by evaluatingcall. Thus, the userdoes not need to specify the signature when debuggingmethods. Although the call is actually to the generic, it is themethod that is debugged, not the generic, except for non-standard S3generics (seeisS3stdGeneric).
debugcall invisibly returns the debugged call expression.
Non-standard evaluation is used to retrieve thecall (viasubstitute). For this reason, passing a variablecontaining a call expression, rather than the call expression itself,will not work.
debug for the primary debugging interface
## Not run: ## Evaluate call after setting debugging## f <- factor(1:10)res <- eval(debugcall(summary(f))) ## End(Not run)## Not run:## Evaluate call after setting debugging##f<- factor(1:10)res<- eval(debugcall(summary(f)))## End(Not run)
Functions to dump the evaluation environments (frames) and to examinedumped frames.
dump.frames(dumpto = "last.dump", to.file = FALSE, include.GlobalEnv = FALSE)debugger(dump = last.dump)limitedLabels(value, maxwidth = getOption("width") - 5L)dump.frames(dumpto="last.dump", to.file=FALSE, include.GlobalEnv=FALSE)debugger(dump= last.dump)limitedLabels(value, maxwidth= getOption("width")-5L)
dumpto | a character string. The name of the object or file todump to. |
to.file | logical. Should the dump be to anR object or to afile? |
include.GlobalEnv | logical indicating if acopy of the |
dump | anR dump object created by |
value | |
maxwidth | optional length to which to trim the result of |
To use post-mortem debugging, set the optionerror to be a calltodump.frames. By default this dumps to anR objectlast.dump in the workspace, but it can be set to dump to afile (a dump of the object produced by a call tosave).The dumped object contain the call stack, the active environments andthe last error message as returned bygeterrmessage.
When dumping to file,dumpto gives the name of the dumpedobject and the file name has ‘.rda’ appended.
A dump object of class"dump.frames" can be examined by callingdebugger. This will give the error message and a list ofenvironments from which to select repeatedly. When an environment isselected, it is copied and thebrowser called fromwithin the copy. Note that not all the information in the originalframe will be available, e.g. promises which have not yet beenevaluated and the contents of any... argument.
Ifdump.frames is installed as the error handler, executionwill continue even in non-interactive sessions. See the examples forhow to dump and then quit.
limitedLabels(v) takes alist of calls whoseelements may have asrcref attribute and returns a vector thatpastes a formatted version of those attributes onto the formatted versionof the elements, all finallystrtrim()med tomaxwidth.
InvisibleNULL.
Functions such assys.parent andenvironment applied to closures will not work correctlyinsidedebugger.
If the error occurred when computing the default value of a formalargument the debugger will report “recursive default argumentreference” when trying to examine that environment.
Of course post-mortem debugging will not work ifR is too damaged toproduce and save the dump, for example if it has run out of workspace.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
browser for the actions available at theBrowseprompt.
options for settingerror options;recover is an interactive debugger working similarly todebugger but directly after the error occurs.
## Not run: options(error = quote(dump.frames("testdump", TRUE)))f <- function() { g <- function() stop("test dump.frames") g()}f() # will generate a dump on file "testdump.rda"options(error = NULL)## possibly in another R sessionload("testdump.rda")debugger(testdump)Available environments had calls:1: f()2: g()3: stop("test dump.frames")Enter an environment number, or 0 to exitSelection: 1Browsing in the environment with call:f()Called from: debugger.look(ind)Browse[1]> ls()[1] "g"Browse[1]> gfunction() stop("test dump.frames")<environment: 759818>Browse[1]>Available environments had calls:1: f()2: g()3: stop("test dump.frames")Enter an environment number, or 0 to exitSelection: 0## A possible setting for non-interactive sessionsoptions(error = quote({dump.frames(to.file = TRUE); q(status = 1)}))## End(Not run)## Not run:options(error= quote(dump.frames("testdump",TRUE)))f<-function(){ g<-function() stop("test dump.frames") g()}f()# will generate a dump on file "testdump.rda"options(error=NULL)## possibly in another R sessionload("testdump.rda")debugger(testdump)Available environments had calls:1: f()2: g()3: stop("test dump.frames")Enter an environment number, or0 to exitSelection:1Browsingin the environment with call:f()Called from: debugger.look(ind)Browse[1]> ls()[1]"g"Browse[1]> gfunction() stop("test dump.frames")<environment:759818>Browse[1]>Available environments had calls:1: f()2: g()3: stop("test dump.frames")Enter an environment number, or0 to exitSelection:0## A possible setting for non-interactive sessionsoptions(error= quote({dump.frames(to.file=TRUE); q(status=1)}))## End(Not run)
demo is a user-friendly interface to running some demonstrationR scripts.demo() gives the list of available topics.
demo(topic, package = NULL, lib.loc = NULL, character.only = FALSE, verbose = getOption("verbose"), type = c("console", "html"), echo = TRUE, ask = getOption("demo.ask"), encoding = getOption("encoding"))demo(topic, package=NULL, lib.loc=NULL, character.only=FALSE, verbose= getOption("verbose"), type= c("console","html"), echo=TRUE, ask= getOption("demo.ask"), encoding= getOption("encoding"))
topic | the topic which should be demonstrated, given as aname or literal character string, or a character string,depending on whether |
package | a character vector giving the packages to look into fordemos, or |
lib.loc | a character vector of directory names ofR libraries,or |
character.only | logical; if |
verbose | a logical. If |
type | character: whether to show output in the console or abrowser (using the dynamic help system). The latter is honored onlyin interactive sessions and if the |
echo | a logical. If |
ask | a logical (or |
encoding | See |
If no topics are given,demo lists the available demos. Fortype = "console", the corresponding information is returned inan object of class"packageIQR".
source anddevAskNewPage whichare called bydemo.example to run codein the Examples section of help pages.
demo() # for attached packages## All available demos:demo(package = .packages(all.available = TRUE))## Display a demo, pausing between pagesdemo(lm.glm, package = "stats", ask = TRUE)## Display it without pausingdemo(lm.glm, package = "stats", ask = FALSE)## Not run: ch <- "scoping" demo(ch, character = TRUE)## End(Not run)## Find the location of a demosystem.file("demo", "lm.glm.R", package = "stats")demo()# for attached packages## All available demos:demo(package= .packages(all.available=TRUE))## Display a demo, pausing between pagesdemo(lm.glm, package="stats", ask=TRUE)## Display it without pausingdemo(lm.glm, package="stats", ask=FALSE)## Not run: ch<-"scoping" demo(ch, character=TRUE)## End(Not run)## Find the location of a demosystem.file("demo","lm.glm.R", package="stats")
On MS Windows only, return the version of the package and the version ofR used tobuild the DLL, if available.
DLL.version(path)DLL.version(path)
path | character vector of length one giving the complete path tothe DLL. |
If the DLL does not exist,NULL.
A character vector of length two, giving the DLL version and the version ofR used to build the DLL. If the information is not available, thecorresponding string is empty.
This is only available on Windows.
if(.Platform$OS.type == "windows") withAutoprint({ DLL.version(file.path(R.home("bin"), "R.dll")) DLL.version(file.path(R.home(), "library/stats/libs", .Platform$r_arch, "stats.dll"))})if(.Platform$OS.type=="windows") withAutoprint({ DLL.version(file.path(R.home("bin"),"R.dll")) DLL.version(file.path(R.home(),"library/stats/libs", .Platform$r_arch,"stats.dll"))})
This function can be used to download a file from the Internet.
download.file(url, destfile, method, quiet = FALSE, mode = "w", cacheOK = TRUE, extra = getOption("download.file.extra"), headers = NULL, ...)download.file(url, destfile, method, quiet=FALSE, mode="w", cacheOK=TRUE, extra= getOption("download.file.extra"), headers=NULL,...)
url | a |
destfile | a character string (or vector, see the |
method | Method to be used for downloading files. Currentdownload methods are The method can also be set through the option |
quiet | If |
mode | character. The mode with which to write the file. Usefulvalues are |
cacheOK | logical. Is a server-side cached value acceptable? |
extra | character vector of additional command-line arguments forthe |
headers | named character vector of additional HTTP headers touse in HTTP[S] requests. It is ignored for non-HTTP[S] URLs. The |
... | allow additional arguments to be passed, unused. |
The functiondownload.file can be used to download a singlefile as described byurl from the internet and store it indestfile.
Theurl must start with a scheme such as ‘http://’,‘https://’ or ‘file://’. Which methods support whichschemes varies byR version, butmethod = "auto" will try tofind a method which supports the scheme.
Formethod = "auto" (the default) currently the"internal" method is used for ‘file://’ URLs and"libcurl" for all others.
Support for method"libcurl" was optional on Windows prior toR 4.2.0: usecapabilities("libcurl") to see if it issupported on an earlier version. It uses an external library of thatname (https://curl.se/libcurl/) against whichR can becompiled.
When method"libcurl" is used, there is support forsimultaneous downloads, sourl anddestfile can becharacter vectors of the same length greater than one (but the methodhas to be specified explicitly and notvia"auto"). Fora single URL andquiet = FALSE a progress bar is shown ininteractive use.
Nowadays the"internal" method only supports the ‘file://’scheme (for which it is the default). On Windows the"wininet"method currently supports ‘file://’ and (but deprecated with awarning) ‘http://’ and ‘https://’ schemes.
For methods"wget" and"curl" a system call is made tothe tool given bymethod, and the respective program must beinstalled on your system and be in the search path for executables.They will block all other activity on theR process until theycomplete: this may make a GUI unresponsive.
cacheOK = FALSE is useful for ‘http://’ and‘https://’ URLs: it will attempt to get a copy directly from thesite rather than from an intermediate cache. It is used byavailable.packages.
The"libcurl" and"wget" methods follow ‘http://’and ‘https://’ redirections to any scheme they support. (Formethod"curl" use argumentextra = "-L". To disableredirection inwget, useextra = "--max-redirect=0".)The"wininet" method supports some redirections but not all.(For method"libcurl", messages will quote the endpoint ofredirections.)
Seeurl for how ‘file://’ URLs are interpreted,especially on Windows. The"internal" and"wininet"methods do not percent-decode, but the"libcurl" and"curl" methods do: method"wget" does not support them.
Most methods do not percent-encode special characters such as spacesin URLs (seeURLencode), but it seems the"wininet" method does.
The remaining details apply to the"wininet" and"libcurl" methods only.
The timeout for many parts of the transfer can be set by the optiontimeout which defaults to 60 seconds. This is ofteninsufficient for downloads of large files (50MB or more) andso should be increased whendownload.file is used in packagesto do so. Note that the user can set the default timeout by theenvironment variableR_DEFAULT_INTERNET_TIMEOUT in recentversions ofR, so to ensure that this is not decreased packages shoulduse something like
options(timeout = max(300, getOption("timeout")))(It is unrealistic to require download times of less than 1s/MB.)
The level of detail provided during transfer can be set by thequiet argument and theinternet.info option: the detailsdepend on the platform and scheme. For the"libcurl" methodvalues of the option less than 2 give verbose output.
A progress bar tracks the transfer platform-specifically:
If the file length is known, thefull width of the bar is the known length. Otherwise the initialwidth represents 100Kbytes and is doubled whenever the current widthis exceeded. (In non-interactive use this uses a text version. If thefile length is known, an equals sign represents 2% of the transfercompleted: otherwise a dot represents 10Kb.)
If the file length is known, anequals sign represents 2% of the transfer completed: otherwise a dotrepresents 10Kb.
The choice of binary transfer (mode = "wb" or"ab") isimportant on Windows, since unlike Unix-alikes it does distinguishbetween text and binary files and for text transfers changes ‘\n’line endings to ‘\r\n’ (aka ‘CRLF’).
On Windows, ifmode is not supplied (missing())andurl ends in one of ‘.gz’, ‘.bz2’, ‘.xz’,‘.tgz’, ‘.zip’, ‘.jar’, ‘.rda’, ‘.rds’,‘.RData’ or ‘.pdf’,mode = "wb" is set so that a binarytransfer is done to help unwary users.
Code written to download binary files must usemode = "wb" (or"ab"), but the problems incurred by a text transfer will onlybe seen on Windows.
An (invisible) integer code,0 for success and non-zero forfailure. For the"wget" and"curl" methods this is thestatus code returned by the external program. The"internal"method can return1, but will in most cases throw an error. Whensimultaneously downloading two or more files (see theurl argument)and download of at least one file succeeds,0 is returned withattributeretvals, which provides an integer vector of the samelength asurl with a result code for each file (0 forsuccess and non-zero for failure).
What happens to the destination file(s) in the case of error dependson the method andR version. Currently the"internal","wininet" and"libcurl" methods will remove the file ifthe URL is unavailable except whenmode specifiesappending when the file should be unchanged.
For the Windows-only method"wininet", the ‘InternetOptions’ of the system are used to choose proxies and so on; these areset in the Control Panel and are those used for system browsers.
For the"libcurl" and"curl" methods, proxies can be setvia the environment variableshttp_proxy orftp_proxy. Seehttps://curl.se/libcurl/c/libcurl-tutorial.html for furtherdetails.
Methods which access ‘https://’ and (wheresupported) ‘ftps://’ URLs should try to verify the sitecertificates. This is usually done using the CA root certificatesinstalled by the OS (although we have seen instances in which thesegot removed rather than updated). For further information seehttps://curl.se/docs/sslcerts.html.
On Windows withmethod = "libcurl", the CA root certificatesare provided by the OS whenR was linked withlibcurl withSchannel enabled, which is the current default in Rtools. Thiscan be verified by checking thatlibcurlVersion() returns aversion string containing ‘"Schannel"’. If it does not, forverification to be on the environment variableCURL_CA_BUNDLEmust be set to a path to a certificate bundle file, usually named‘ca-bundle.crt’ or ‘curl-ca-bundle.crt’. (This is normallydone automatically for a binary installation ofR, which installs‘R_HOME/etc/curl-ca-bundle.crt’ and setsCURL_CA_BUNDLE to point to it if that environment variable isnot already set.) For an updated certificate bundle, seehttps://curl.se/docs/sslcerts.html. Currently one can downloada copy fromhttps://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crtand setCURL_CA_BUNDLE to the full path to the downloaded file.
On Windows withmethod = "libcurl", whenR was linked withlibcurl withSchannel enabled, the connection fails if itcannot be established that the certificate has not been revoked. SomeMITM proxies present particularly in corporate environments do not workwith this behavior. It can be changed by setting environment variableR_LIBCURL_SSL_REVOKE_BEST_EFFORT toTRUE, with theconsequence of reducing security.
Note that the root certificates used byR may or may not be the sameas used in a browser, and indeed different browsers may use differentcertificate bundles (there is typically a build option to chooseeither their own or the system ones).
Setting themethod should be left to the end user. Neither ofthewget norcurl commands is widely available:you can check if one is availableviaSys.which,and should do so in a package or script.
If you usedownload.file in a package or script, you must checkthe return value, since it is possible that the download will failwith a non-zero status but not anR error.
The supportedmethods do change: methodlibcurl wasintroduced inR 3.2.0 and was optional on Windows untilR 4.2.0 –usecapabilities("libcurl") in a program to see if it isavailable.
Most modern browsers do not support such URLs, and ‘https://’ones are much preferred for use inR. ‘ftps://’ URLs have alwaysbeen rare, and are nowadays even less supported.
It is intended thatR will continue to allow such URLs for as long aslibcurl does, but as they become rarer this is increasinglyuntested. What ‘protocols’ the version oflibcurlbeing used supports can be seen by callinglibcurlVersion().
These URLs are accessed using the FTP protocol which has anumber of variants. One distinction is between ‘active’ and‘(extended) passive’ modes: which is used is chosen by theclient. The"libcurl" method uses passive mode which wasalmost universally used by browsers before they dropped supportaltogether.
Files of more than 2GB are supported on 64-bit builds ofR; theymay be truncated on some 32-bit builds.
Methods"wget" and"curl" are mainly for historicalcompatibility but provide may provide capabilities not supported bythe"libcurl" or"wininet" methods.
Method"wget" can be used with proxy firewalls which requireuser/password authentication if proper values are stored in theconfiguration file forwget.
wget (https://www.gnu.org/software/wget/) is commonlyinstalled on Unix-alikes (but not macOS). Windows binaries areavailable fromMSYS2 and elsewhere.
curl (https://curl.se/) is installed on macOS andincreasingly commonly on Unix-alikes. Windows binaries are availableat that URL.
options to set theHTTPUserAgent,timeoutandinternet.info options used by some of the methods.
url for a finer-grained way to read data from URLs.
url.show,available.packages,download.packages for applications.
Contributed packagesRCurl andcurl provide morecomprehensive facilities to download from URLs.
These functions can be used to automatically compare the versionnumbers of installed packages with the newest available version onthe repositories and update outdated packages on the fly.
download.packages(pkgs, destdir, available = NULL, repos = getOption("repos"), contriburl = contrib.url(repos, type), method, type = getOption("pkgType"), ...)download.packages(pkgs, destdir, available=NULL, repos= getOption("repos"), contriburl= contrib.url(repos, type), method, type= getOption("pkgType"),...)
pkgs | character vector of the names of packages whose latest availableversions should be downloaded from the repositories. |
destdir | directory where downloaded packages are to be stored. |
available | an object as returned by |
repos | character vector, the base URL(s) of the repositoriesto use, i.e., the URL of the CRAN master such as |
contriburl | URL(s) of the contrib sections of therepositories. Use this argument only if your repository mirror isincomplete, e.g., because you burned only the ‘contrib’ section on aCD. Overrides argument |
method | Download method, see |
type | character string, indicate which type of packages: see |
... | additional arguments to be passed to |
download.packages takes a list of package names and adestination directory, downloads the newest versions and saves them indestdir. If the list of available packages is not given asargument, it is obtained from repositories. If a repository is local,i.e. the URL starts with"file:", then the packages are notdownloaded but used directly. Both"file:" and"file:///" are allowed as prefixes to a file path. Use thelatter only for URLs: seeurl for their interpretation.(Other forms of ‘file://’ URLs are not supported.)
Fordownload.packages,type = "both" looks at sourcepackages only.
A two-column matrix of names and destination file names of thosepackages successfully downloaded. If packages are not available orthere is a problem with the download, suitable warnings are given.
available.packages,contrib.url.
The main use is byinstall.packages.
Seedownload.file for how to handle proxies andother options to monitor file transfers.
The ‘R Installation and Administration’ manual for how toset up a repository.
Invoke a text editor on anR object.
edit(name, ...)## Default S3 method:edit(name = NULL, file = "", title = NULL, editor = getOption("editor"), ...)vi(name = NULL, file = "")emacs(name = NULL, file = "")pico(name = NULL, file = "")xemacs(name = NULL, file = "")xedit(name = NULL, file = "")edit(name,...)## Default S3 method:edit(name=NULL, file="", title=NULL, editor= getOption("editor"),...)vi(name=NULL, file="")emacs(name=NULL, file="")pico(name=NULL, file="")xemacs(name=NULL, file="")xedit(name=NULL, file="")
name | a named object that you want to edit. For the defaultmethod, if |
file | a string naming the file to write the edited version to. |
title | a display name for the object being edited. |
editor | usually a character string naming (or giving the pathto) the text editor you want to use. On Unix the default is set fromthe environment variablesEDITOR orVISUAL if either isset, otherwise
|
... | further arguments to be passed to or from methods. |
edit invokes the text editor specified byeditor withthe objectname to be edited. It is a generic function,currently with a default method and one for data frames and matrices.
data.entry can be used to edit data, and is used byeditto edit matrices and data frames on systems for whichdata.entry is available.
It is important to realize thatedit does not change the objectcalledname. Instead, a copy of name is made and it is thatcopy which is changed. Should you want the changes to apply to theobjectname you must assign the result ofedit toname. (Tryfix if you want to make permanentchanges to an object.)
In the formedit(name),edit deparsesname into a temporary file and invokes theeditoreditor on this file. Quitting from the editor causesfile to be parsed and that value returned.Should an error occur in parsing, possibly due to incorrect syntax, novalue is returned. Callingedit(), with no arguments, willresult in the temporary file being reopened for further editing.
Note that deparsing is not perfect, and the object recreated afterediting can differ in subtle ways from that deparsed: seedput and.deparseOpts. (The deparse optionsused are the same as the defaults fordump.) Editing afunction will preserve its environment. Seeedit.data.frame for further changes that can occur whenediting a data frame or matrix.
Currently only the internal editor in Windows makes use of thetitle option; it displays the given name in the windowheader.
The functionsvi,emacs,pico,xemacs,xedit rely on the corresponding editor being available andbeing on the path. This is system-dependent.
edit.data.frame,data.entry,fix.
## Not run: # use xedit on the function mean and assign the changesmean <- edit(mean, editor = "xedit")# use vi on mean and write the result to file mean.outvi(mean, file = "mean.out")## End(Not run)## Not run:# use xedit on the function mean and assign the changesmean<- edit(mean, editor="xedit")# use vi on mean and write the result to file mean.outvi(mean, file="mean.out")## End(Not run)
Use data editor on data frame or matrix contents.
## S3 method for class 'data.frame'edit(name, factor.mode = c("character", "numeric"), edit.row.names = any(row.names(name) != 1:nrow(name)), ...)## S3 method for class 'matrix'edit(name, edit.row.names = !is.null(dn[[1]]), ...)## S3 method for class 'data.frame'edit(name, factor.mode= c("character","numeric"), edit.row.names= any(row.names(name)!=1:nrow(name)),...)## S3 method for class 'matrix'edit(name, edit.row.names=!is.null(dn[[1]]),...)
name | A data frame or (numeric, logical or character) matrix. |
factor.mode | How to handle factors (as integers or usingcharacter levels) in a data frame. Can be abbreviated. |
edit.row.names | logical. Show the row names (if they exist) bedisplayed as a separate editable column? It is an error to ask forthis on a matrix with |
... | further arguments passed to or from other methods. |
At present, this only works on simple data frames containing numeric,logical or character vectors and factors, and numeric, logical orcharacter matrices. Any other mode of matrix will give an error, anda warning is given when the matrix has a class (which will be discarded).
Data frame columns are coerced on input tocharacter unlessnumeric (in the sense ofis.numeric), logical or factor. Awarning is given when classes are discarded. Special characters(tabs, non-printing ASCII, etc.) will be displayed as escape sequences.
Factors columns are represented in the spreadsheet as either numericvectors (which are more suitable for data entry) or character vectors(better for browsing). After editing, vectors are padded withNA to have the same length and factor attributes are restored.The set of factor levels can not be changed by editing in numericmode; invalid levels are changed toNA and a warning is issued.If new factor levels are introduced in character mode, they are addedat the end of the list of levels in the order in which theyencountered.
It is possible to use the data-editor's facilities to select the modeof columns to swap between numerical and factor columns in a dataframe. Changing any column in a numerical matrix to character willcause the result to be coerced to a character matrix. Changingthe mode of logical columns is not supported.
For a data frame, the row names will be taken from the original objectifedit.row.names = FALSE and the number of rows is unchanged,and from the edited output ifedit.row.names = TRUE and thereare no duplicates. (If therow.names column is incomplete, itis extended by entries likerow223.) In all other cases therow names are replaced byseq(length = nrows).
For a matrix, colnames will be added (of the formcol7) ifneeded. The rownames will be taken from the original object ifedit.row.names = FALSE and the number of rows is unchanged(otherwiseNULL), and from the edited output ifedit.row.names = TRUE. (If therow.names column isincomplete, it is extended by entries likerow223.)
Editing a matrix or data frame will lose all attributes apart from therow and column names.
The edited data frame or matrix.
fix(dataframe) works for in-place editing by calling thisfunction.
If the data editor is not available, a dump of the object is presentedfor editing using the default method ofedit.
At present the data editor is limited to 65535 rows.
Peter Dalgaard
## Not run: edit(InsectSprays)edit(InsectSprays, factor.mode = "numeric")## End(Not run)## Not run:edit(InsectSprays)edit(InsectSprays, factor.mode="numeric")## End(Not run)
Run all theR code from theExamples part ofR's online helptopictopic, with possible exceptions due to\dontrun,\dontshow, and\donttest tags, see ‘Details’ below.
example(topic, package = NULL, lib.loc = NULL, character.only = FALSE, give.lines = FALSE, local = FALSE, type = c("console", "html"), echo = TRUE, verbose = getOption("verbose"), setRNG = FALSE, ask = getOption("example.ask"), prompt.prefix = abbreviate(topic, 6), catch.aborts = FALSE, run.dontrun = FALSE, run.donttest = interactive())example(topic, package=NULL, lib.loc=NULL, character.only=FALSE, give.lines=FALSE, local=FALSE, type= c("console","html"), echo=TRUE, verbose= getOption("verbose"), setRNG=FALSE, ask= getOption("example.ask"), prompt.prefix= abbreviate(topic,6), catch.aborts=FALSE, run.dontrun=FALSE, run.donttest= interactive())
topic | name or literal character string: the online |
package | a character vector giving the package names to lookinto for the topic, or |
lib.loc | a character vector of directory names ofR libraries,or |
character.only | a logical indicating whether |
give.lines | logical: if true, thelines of the examplesource code are returned as a character vector. |
local | logical: if |
type | character: whether to show output in the console or abrowser (using the dynamic help system). The latter is honored onlyin interactive sessions and if the |
echo | logical; if |
verbose | logical; if |
setRNG | logical or expression; if not |
ask | logical (or |
prompt.prefix | character; prefixes the prompt to be used if |
catch.aborts | logical, passed on to |
run.dontrun | logical indicating that |
run.donttest | logical indicating that |
Iflib.loc is not specified, the packages are searched foramongst those already loaded, then in the libraries given by.libPaths(). Iflib.loc is specified, packagesare searched for only in the specified libraries, even if they arealready loaded from another library. The search stops at the firstpackage found that has help on the topic.
An attempt is made to load the package before running the examples,but this will not replace a package loaded from another location.
Iflocal = TRUE objects are not created in the workspace and sonot available for examination afterexample completes: on theother hand they cannot overwrite objects of the same name in theworkspace.
As detailed in the ‘Writing R Extensions’ manual, the author ofthe help page can tag parts of the examples with the followingexception rules:
\dontrunencloses code that should not be run.
\dontshowencloses code that is invisible on helppages, but will be run both by the package checking tools,and theexample() function. This was previously\testonly, and that form is still accepted.
\donttestencloses code that typically should be run,but not during package checking. The defaultrun.donttest =interactive()leadsexample() use in other helppage examples to skip\donttest sections appropriately.
The additional\dontdiff tag (inR 4.4.0) producesspecial comments in the code run byexample (forRdiff-based testing of example output), but does notaffect which code is run or displayed on the help page.
The value of the last evaluated expression, unlessgive.linesis true, where acharacter vector is returned.
Martin Maechler and others
example(InsectSprays)## force use of the standard package 'stats':example("smooth", package = "stats", lib.loc = .Library)## set RNG *before* example as when R CMD check is run:r1 <- example(quantile, setRNG = TRUE)x1 <- rnorm(1)u <- runif(1)## identical random numbersr2 <- example(quantile, setRNG = TRUE)x2 <- rnorm(1)stopifnot(identical(r1, r2))## but x1 and x2 differ since the RNG state from before example()## differs and is restored!x1; x2## Exploring examples code:## How large are the examples of "lm...()" functions?lmex <- sapply(apropos("^lm", mode = "function"), example, character.only = TRUE, give.lines = TRUE)lengths(lmex)example(InsectSprays)## force use of the standard package 'stats':example("smooth", package="stats", lib.loc= .Library)## set RNG *before* example as when R CMD check is run:r1<- example(quantile, setRNG=TRUE)x1<- rnorm(1)u<- runif(1)## identical random numbersr2<- example(quantile, setRNG=TRUE)x2<- rnorm(1)stopifnot(identical(r1, r2))## but x1 and x2 differ since the RNG state from before example()## differs and is restored!x1; x2## Exploring examples code:## How large are the examples of "lm...()" functions?lmex<- sapply(apropos("^lm", mode="function"), example, character.only=TRUE, give.lines=TRUE)lengths(lmex)
Utility for shell-style file tests.
file_test(op, x, y)file_test(op, x, y)
op | a character string specifying the test to be performed.Unary tests (only |
x,y | character vectors giving file paths. |
‘Existence’ here means being on the file system and accessibleby thestat system call (or a 64-bit extension) – on aUnix-alike this requires execute permission on all of the directories inthe path that leads to the file, but no permissions on the fileitself.
For the meaning of"-x" on Windows seefile.access.
file.exists which only tests for existence(test -e on some systems) but not for not being a directory.
dir <- file.path(R.home(), "library", "stats")file_test("-d", dir)file_test("-nt", file.path(dir, "R"), file.path(dir, "demo"))dir<- file.path(R.home(),"library","stats")file_test("-d", dir)file_test("-nt", file.path(dir,"R"), file.path(dir,"demo"))
Edit one or more files in a text editor.
file.edit(..., title = file, editor = getOption("editor"), fileEncoding = "")file.edit(..., title= file, editor= getOption("editor"), fileEncoding="")
... | one or more character vectors containing the names of thefiles to be displayed. These will be tilde-expanded: see |
title | the title to use in the editor; defaults to the filename. |
editor | the text editor to be used, usually as a characterstring naming (or giving the path to) the text editor you want touse See ‘Details’. |
fileEncoding | the encoding to assume for the file: the defaultis to assume the native encoding. See the ‘Encoding’ sectionof the help for |
The behaviour of this function is very system-dependent. Currentlyfiles can be opened only one at a time on Unix; on Windows, theinternal editor allows multiple files to be opened, but has a limit of50 simultaneous edit windows.
Thetitle argument is used for the window caption in Windows,and is currently ignored on other platforms.
Any error in re-encoding the files to the native encoding will causethe function to fail.
The default foreditor is system-dependent. OnWindows it defaults to"internal", the script editor, and inthe macOS GUI the document editor is used whatever the value ofeditor. On Unix the default is set from the environmentvariablesEDITOR orVISUAL if either is set, otherwisevi is used.
editor can also be anR function, in which case it is calledwith the argumentsname,file, andtitle. Notethat such a function will need to independently implement alldesired functionality.
On Windows,UTF-8-encoded paths not valid in the current locale can be used.
## Not run: # open two R scripts for editingfile.edit("script1.R", "script2.R")## End(Not run)## Not run:# open two R scripts for editingfile.edit("script1.R","script2.R")## End(Not run)
Find out if a CRAN mirror has been selected for the current session.
findCRANmirror(type = c("src", "web"))findCRANmirror(type= c("src","web"))
type | Is the mirror to be used for package sources or web information? |
Find out if a CRAN mirror has been selected for the current session.If so, return its URL else return ‘"https://CRAN.R-project.org"’.
The mirror is looked for in several places.
The value of the environment variableR_CRAN_SRC orR_CRAN_WEB (depending ontype), if set.
An entry ingetOption("repos") named ‘CRAN’ whichis not the default ‘"@CRAN@")’.
The ‘CRAN’ URL entry in the ‘repositories’ file (seesetRepositories), if it is not the default ‘"@CRAN@"’.
The two types allow for partial local CRAN mirrors, for example thosemirroring only the package sources wheregetOption("repos")might point to the partial mirror andR_CRAN_WEB point toa full (remote) mirror.
A character string.
setRepositories,chooseCRANmirror
c(findCRANmirror("src"), findCRANmirror("web"))Sys.setenv(R_CRAN_WEB = "https://cloud.r-project.org")c(findCRANmirror("src"), findCRANmirror("web"))c(findCRANmirror("src"), findCRANmirror("web"))Sys.setenv(R_CRAN_WEB="https://cloud.r-project.org")c(findCRANmirror("src"), findCRANmirror("web"))
These functions locate objects containing particular lines of sourcecode, using the information saved when the code was parsed withkeep.source = TRUE.
findLineNum(srcfile, line, nameonly = TRUE, envir = parent.frame(), lastenv)setBreakpoint(srcfile, line, nameonly = TRUE, envir = parent.frame(), lastenv, verbose = TRUE, tracer, print = FALSE, clear = FALSE, ...)findLineNum(srcfile, line, nameonly=TRUE, envir= parent.frame(), lastenv)setBreakpoint(srcfile, line, nameonly=TRUE, envir= parent.frame(), lastenv, verbose=TRUE, tracer, print=FALSE, clear=FALSE,...)
srcfile | The name of the file containing the source code. |
line | The line number within the file. See Details for analternate way to specify this. |
nameonly | If |
envir | Where do we start looking for function objects? |
lastenv | Where do we stop? See the Details. |
verbose | Should we print information on where breakpoints were set? |
tracer | An optional |
print | The |
clear | |
... | Additional arguments to pass to |
ThefindLineNum function searches through all objects inenvironmentenvir, its parent, grandparent, etc., all the wayback tolastenv.
lastenv defaults to the global environment ifenvir is not specified, and to theroot environmentemptyenv() ifenvir isspecified. (The first default tends to be quite fast, and willusually find all user code other than S4 methods; the second one isquite slow, as it will typically search all attached systemlibraries.)
For convenience,envir may be specified indirectly: if it isnot an environment, it will be replaced withenvironment(envir).
setBreakpoint is a simple wrapper function fortrace anduntrace. It will set or clearbreakpoints at the locations found byfindLineNum.
Thesrcfile is normally a filename entered as a characterstring, but it may be a"srcfile" object, or it mayinclude a suffix like"filename.R#nn", in which case the numbernn will be used as a default value forline.
As described in the description of thewhere argument on theman page fortrace, theR package system uses acomplicated scheme that may include more than one copy of a functionin a package. The user will typically see the public one on thesearch path, while code in the package will see a private one in thepackage namespace. If you setenvir to the environment of afunction in the package, by defaultfindLineNum will find bothversions, andsetBreakpoint will set the breakpoint in both.(This can be controlled usinglastenv; e.g.,envir = environment(foo),lastenv = globalenv()will find only the private copy, as the search is stopped beforeseeing the public copy.)
S version 4 methods are also somewhat tricky to find. They are storedwith the generic function, which may be in thebase or otherpackage, so it is usually necessary to havelastenv = emptyenv()in order to find them. In some cases transformations are done byRwhen storing them andfindLineNum may not be able to find theoriginal code. Many special cases, e.g. methods on primitivegenerics, are not yet supported.
findLineNum returns a list of objects containing locationinformation. Aprint method is defined for them.
setBreakpoint has no useful return value; it is called for theside effect of callingtrace oruntrace.
Duncan Murdoch
## Not run: # Find what function was defined in the file mysource.R at line 100:findLineNum("mysource.R#100")# Set a breakpoint in both copies of that function, assuming one is in the# same namespace as myfunction and the other is on the search pathsetBreakpoint("mysource.R#100", envir = myfunction)## End(Not run)## Not run:# Find what function was defined in the file mysource.R at line 100:findLineNum("mysource.R#100")# Set a breakpoint in both copies of that function, assuming one is in the# same namespace as myfunction and the other is on the search pathsetBreakpoint("mysource.R#100", envir= myfunction)## End(Not run)
fix invokesedit onx and then assigns the new(edited) version ofx in the user's workspace.
fix(x, ...)fix(x,...)
x | the name of anR object, as a name or a character string. |
... | arguments to pass to editor: see |
The name supplied asx need not exist as anR object, inwhich case a function with no arguments and an empty body is suppliedfor editing.
Editing anR object may change it in ways other than are obvious: seethe comment underedit. Seeedit.data.frame for changes that can occur when editinga data frame or matrix.
## Not run: ## Assume 'my.fun' is a user defined function : fix(my.fun) ## now my.fun is changed ## Also, fix(my.data.frame) # calls up data editor fix(my.data.frame, factor.mode="char") # use of ...## End(Not run)## Not run:## Assume 'my.fun' is a user defined function : fix(my.fun)## now my.fun is changed## Also, fix(my.data.frame)# calls up data editor fix(my.data.frame, factor.mode="char")# use of ...## End(Not run)
This does nothing except on console-based versions ofR.On the macOS and Windows GUIs, it ensures that the display ofoutput in the console is current, even if output buffering is on.
flush.console()flush.console()
Format unordered (itemize) and ordered (enumerate) lists.
formatUL(x, label = "*", offset = 0, width = 0.9 * getOption("width"))formatOL(x, type = "arabic", offset = 0, start = 1, width = 0.9 * getOption("width"))formatUL(x, label="*", offset=0, width=0.9* getOption("width"))formatOL(x, type="arabic", offset=0, start=1, width=0.9* getOption("width"))
x | a character vector of list items. |
label | a character string used for labelling the items. |
offset | a non-negative integer giving the offset (indentation)of the list. |
width | a positive integer giving the target column for wrappinglines in the output. |
type | a character string specifying the ‘type’ of thelabels in the ordered list. If |
start | a positive integer specifying the starting number of thefirst item in an ordered list. |
A character vector with the formatted entries.
formatDL for formatting description lists.
## A simpler recipe.x <- c("Mix dry ingredients thoroughly.", "Pour in wet ingredients.", "Mix for 10 minutes.", "Bake for one hour at 300 degrees.")## Format and output as an unordered list.writeLines(formatUL(x))## Format and output as an ordered list.writeLines(formatOL(x))## Ordered list using lower case roman numerals.writeLines(formatOL(x, type = "i"))## Ordered list using upper case letters and some offset.writeLines(formatOL(x, type = "A", offset = 5))## A simpler recipe.x<- c("Mix dry ingredients thoroughly.","Pour in wet ingredients.","Mix for 10 minutes.","Bake for one hour at 300 degrees.")## Format and output as an unordered list.writeLines(formatUL(x))## Format and output as an ordered list.writeLines(formatOL(x))## Ordered list using lower case roman numerals.writeLines(formatOL(x, type="i"))## Ordered list using upper case letters and some offset.writeLines(formatOL(x, type="A", offset=5))
These functions locate all objects with name matching their argument,whether visible on the search path, registered as an S3 method or in anamespace but not exported.getAnywhere() returns the objectsandargsAnywhere() returns the arguments of any objects thatare functions.
getAnywhere(x)argsAnywhere(x)getAnywhere(x)argsAnywhere(x)
x | a character string or name. |
These functions look at all loaded namespaces, whether or not they areassociated with a package on the search list.
They do not search literally “anywhere”: for example, localevaluation frames and namespaces that are not loaded will not besearched.
Where functions are found as registered S3 methods, an attempt ismade to find which namespace registered them. This may not becorrect, especially if namespaces have been unloaded.
ForgetAnywhere() an object of class"getAnywhere".This is a list with components
name | the name searched for |
objs | a list of objects found |
where | a character vector explaining where the object(s) were found |
visible | logical: is the object visible |
dups | logical: is the object identical to one earlier in thelist. |
In computing whether objects are identical, their environments are ignored.
Normally the structure will be hidden by theprint method.There is a[ method to extract one or more of the objectsfound.
ForargsAnywhere() one or more argument lists as returned byargs.
getS3method to find the method which would be used: thismight not be the one of those returned bygetAnywhere since itmight have come from a namespace which was unloaded or be registeredunder another name.
getAnywhere("format.dist")getAnywhere("simpleLoess") # not exported from statsargsAnywhere(format.dist)getAnywhere("format.dist")getAnywhere("simpleLoess")# not exported from statsargsAnywhere(format.dist)
Utility functions to access and replace the non-exported functions ina namespace, for use in developing packages with namespaces.
They should not be used in production code (except perhapsassignInMyNamespace, but see the ‘Note’).
getFromNamespace(x, ns, pos = -1, envir = as.environment(pos))assignInNamespace(x, value, ns, pos = -1, envir = as.environment(pos))assignInMyNamespace(x, value)fixInNamespace(x, ns, pos = -1, envir = as.environment(pos), ...)getFromNamespace(x, ns, pos=-1, envir= as.environment(pos))assignInNamespace(x, value, ns, pos=-1, envir= as.environment(pos))assignInMyNamespace(x, value)fixInNamespace(x, ns, pos=-1, envir= as.environment(pos),...)
x | an object name (given as a character string). |
value | anR object. |
ns | a namespace, or character string giving the namespace. |
pos | where to look for the object: see |
envir | an alternative way to specify an environment to look in. |
... | arguments to pass to the editor: see |
assignInMyNamespace is intended to be called from functionswithin a package, and chooses the namespace as the environment of thefunction calling it.
The namespace can be specified in several ways. Using, for example,ns = "stats" is the most direct, but a loaded package can bespecified via any of the methods used forget:nscan also be the environment printed as ‘<namespace:foo>’.
getFromNamespace is similar to (but predates) the::: operator: it is more flexible in how the namespaceis specified.
fixInNamespace invokesedit on the object namedx and assigns the revised object in place of the originalobject. For compatibility withfix,x can be unquoted.
getFromNamespace returns the object found (or gives an error).
assignInNamespace,assignInMyNamespace andfixInNamespace are invoked for their side effect of changingthe object in the namespace.
assignInNamespace should not be used in final code, and will infuture throw an error if called from a package. Already certain usesare disallowed.
assignInNamespace,assignInMyNamespace andfixInNamespace change the copy in the namespace, but not anycopies already exported from the namespace, in particular an object ofthat name in the package (if already attached) and any copies alreadyimported into other namespaces. They are really intended to be usedonly for objects which are not exported from the namespace.They do attempt to alter a copy registered as an S3 method if one isfound.
They can only be used to change the values of objects in thenamespace, not to create new objects.
getFromNamespace("findGeneric", "utils")## Not run: fixInNamespace("predict.ppr", "stats")stats:::predict.pprgetS3method("predict", "ppr")## alternativelyfixInNamespace("predict.ppr", pos = 3)fixInNamespace("predict.ppr", pos = "package:stats")## End(Not run)getFromNamespace("findGeneric","utils")## Not run:fixInNamespace("predict.ppr","stats")stats:::predict.pprgetS3method("predict","ppr")## alternativelyfixInNamespace("predict.ppr", pos=3)fixInNamespace("predict.ppr", pos="package:stats")## End(Not run)
If the"keep.source" option isTRUE,R's parserwill attach detailed information on the object it has parsed. Thesefunctions retrieve that information.
getParseData(x, includeText = NA)getParseText(parseData, id)getParseData(x, includeText=NA)getParseText(parseData, id)
x | an expression returned from |
includeText | logical; whether to include the text of parsed items in the result |
parseData | a data frame returned from |
id | a vector of item identifiers whose text is to be retrieved |
In version 3.0.0, theR parser was modified to include code writtenby Romain Francois in hisparser package. This constructs adetailed table of information about every token and higher levelconstruct in parsed code. This table is stored in thesrcfile record associated with source references in theparsed code, and retrieved by thegetParseData function.
ForgetParseData:
If parse data is not present,NULL. Otherwisea data frame is returned, containing the following columns:
line1 | integer. The line number where the item starts. This is theparsed line number called |
col1 | integer. The column number where the item starts. The first characteris column 1. This corresponds to |
line2 | integer. The line number where the item ends. |
col2 | integer. The column number where the item ends. |
id | integer. An identifier associated with this item. |
parent | integer. The |
token | character string. The type of the token. |
terminal | logical. Whether the token is “terminal”, i.e.a leaf in the parse tree. |
text | character string. If |
The rownames of the data frame will be equal to theid values,and the data frame will have a"srcfile" attribute containingthesrcfile record which was used. The rows will beordered by starting position within the source file, with parent itemsoccurring before their children.
ForgetParseText:
A character vector of the same length asid containing the associatedtext items. If they are not included inparseData, they will beretrieved from the original file.
There are a number of differences in the results returned bygetParseData relative to those in the originalparsercode:
Fewer columns are kept.
The internal token number is not returned.
col1 starts counting at 1, not 0.
Theid values are not attached to the elements of the parsetree, they are only retained in the table returned bygetParseData.
#line directives are identified, but other commentmarkup (e.g.,roxygen2 comments) are not.
Parse data by design explore details of the parser implementation, whichare subject to change without notice. Applications computing on the parsedata may require updates for each R release.
Duncan Murdoch
Romain Francois (2012). parser: Detailed R source code parser. Rpackage version 0.0-16.https://github.com/halpo/parser.
fn <- function(x) { x + 1 # A comment, kept as part of the source}d <- getParseData(fn)if (!is.null(d)) { plus <- which(d$token == "'+'") sum <- d$parent[plus] print(d[as.character(sum),]) print(getParseText(d, sum))}fn<-function(x){ x+1# A comment, kept as part of the source}d<- getParseData(fn)if(!is.null(d)){ plus<- which(d$token=="'+'") sum<- d$parent[plus] print(d[as.character(sum),]) print(getParseText(d, sum))}
Get a method for an S3 generic, possibly from a namespace or thegeneric's registry.
getS3method(f, class, optional = FALSE, envir = parent.frame())getS3method(f, class, optional=FALSE, envir= parent.frame())
f | a character string giving the name of the generic. |
class | a character string giving the name of the class. |
optional | logical: should failure to find the generic or amethod be allowed? |
envir | the |
S3 methods may be hidden in namespaces, and will notthen be found byget: this function can retrievesuch functions, primarily for debugging purposes.
Further, S3 methods can be registered on the generic when a namespaceis loaded, and the registered method will be used if none is visible(using namespace scoping rules).
It is possible that which S3 method will be used may depend on wherethe genericf is called from:getS3method returns themethod found iff were called from the same environment.
The function found, orNULL if no function is found andoptional = TRUE.
require(stats)exists("predict.ppr") # falsegetS3method("predict", "ppr")require(stats)exists("predict.ppr")# falsegetS3method("predict","ppr")
Get the Windows handle of a window or of theR process in MS Windows.
getWindowsHandle(which = "Console")getWindowsHandle(which="Console")
which | a string (see below), or the number of a graphics devicewindow (which must a |
getWindowsHandle gets the Windows handle.Possible choices forwhich are:
"Console" | The console window handle. |
"Frame" | The MDI frame window handle. |
"Process" | The process pseudo-handle. |
| A device number | The window handle of a graphics device |
These values are not normally useful to users, but may be used bydevelopers making addons toR.
NULL is returned for the Frame handle if not running in MDI mode,for the Console handle when running Rterm, for any unrecognizedstring forwhich, or for a graphics device with nocorresponding window.
Other windows (help browsers, etc.) are not accessiblethrough this function.
An external pointer holding the Windows handle, orNULL.
This is only available on Windows.
getIdentification,getWindowsHandles
if(.Platform$OS.type == "windows") print( getWindowsHandle() )if(.Platform$OS.type=="windows") print( getWindowsHandle())
This function gets the Windows handles of visible top level windows orwindows within theR MDI frame (when using theRgui).
getWindowsHandles(which = "R", pattern = "", minimized = FALSE)getWindowsHandles(which="R", pattern="", minimized=FALSE)
which | A vector of strings "R" or "all" (possibly with repetitions). Seethe Details section. |
pattern | A vector of patterns that the titles of the windows must match. |
minimized | A logical vector indicating whether minimized windows should be considered. |
This function will search for Windows handles, for passing to externalGUIs or to thearrangeWindows function. Each of thearguments may be a vector of values. These will be treated asfollows:
The arguments will all be recycled to the same length.
The corresponding elements of each argument will be applied inseparate searches.
The final result will be the union of the windows identified ineach of the searches.
If an element ofwhich is"R", only windows belonging tothe currentR process will be returned. In MDI mode, those will be thechild windows within theR GUI (Rgui) frame. In SDI mode,all windows belonging to the process will be included.
If the element is"all", then top level windows will be returned.
The elements ofpattern will be used to make a subset of windowswhose title text matches (according togrep) the pattern.
Ifminimized = FALSE, minimized windows will be ignored.
A list of external pointers containing the window handles.
This is only available on Windows.
Duncan Murdoch
arrangeWindows,getWindowsHandle (singular).
if(.Platform$OS.type == "windows") withAutoprint({ getWindowsHandles() getWindowsHandles("all")})if(.Platform$OS.type=="windows") withAutoprint({ getWindowsHandles() getWindowsHandles("all")})
Changewildcard akaglobbing patterns into thecorresponding regular expressions (regexp).
This is also a practical didactical example for the use ofsub() and regular expressions.
glob2rx(pattern, trim.head = FALSE, trim.tail = TRUE)glob2rx(pattern, trim.head=FALSE, trim.tail=TRUE)
pattern | character vector |
trim.head | logical specifying if leading |
trim.tail | logical specifying if trailing |
This takes a wildcard as used by most shells and returns an equivalentregular expression. ‘?’ is mapped to ‘.’ (match a singlecharacter), ‘*’ to ‘.*’ (match any string, including anempty one), and the pattern is anchored (it must start at thebeginning and end at the end). Optionally, the resulting regexp issimplified.
Note that now even ‘(’, ‘[’ and ‘{’ can be usedinpattern, butglob2rx() may not work correctly witharbitrary characters inpattern, for example escaped specialcharacters.
A character vector of the same length as the inputpatternwhere each wildcard is translated to the correspondingregular expression.
Martin Maechler, Unix/sed based version, 1991; current: 2004
regexp about regular expression,sub, etc about substitutions using regexps.Sys.glob does wildcard expansion, i.e., “globbing” onfile paths more subtly, e.g., allowing to escape special characters.
stopifnot(glob2rx("abc.*") == "^abc\\.", glob2rx("a?b.*") == "^a.b\\.", glob2rx("a?b.*", trim.tail = FALSE) == "^a.b\\..*$", glob2rx("*.doc") == "^.*\\.doc$", glob2rx("*.doc", trim.head = TRUE) == "\\.doc$", glob2rx("*.t*") == "^.*\\.t", glob2rx("*.t??") == "^.*\\.t..$", glob2rx("*[*") == "^.*\\[")stopifnot(glob2rx("abc.*")=="^abc\\.", glob2rx("a?b.*")=="^a.b\\.", glob2rx("a?b.*", trim.tail=FALSE)=="^a.b\\..*$", glob2rx("*.doc")=="^.*\\.doc$", glob2rx("*.doc", trim.head=TRUE)=="\\.doc$", glob2rx("*.t*")=="^.*\\.t", glob2rx("*.t??")=="^.*\\.t..$", glob2rx("*[*")=="^.*\\[")
ForglobalVariables, the names supplied are of functions orother objects that should be regarded as defined globally when thecheck tool is applied to this package. The call toglobalVariables will be included in the package's source.Repeated calls in the same package accumulate the names of theglobal variables.
Typical examples are the fields and methods in reference classes,which appear to be global objects tocodetools.(This case is handled automatically bysetRefClass() andfriends, using the supplied field and method names.)
ForsuppressForeignCheck, the names supplied are of variablesused as.NAME in foreign function calls which should not bechecked bycheckFF(registration = TRUE). Without thisdeclaration, expressions other than simple character strings areassumed to evaluate to registered native symbol objects. The type ofcall (.Call,.External, etc.) and argument counts willbe checked. With this declaration, checks on those names will usuallybe suppressed. (If the code uses an expression that should only beevaluated at runtime, the message can be suppressed by wrapping it inadontCheck function call, or by saving it to a localvariable, and suppressing messages about that variable. See theexample below.)
globalVariables(names, package, add = TRUE)suppressForeignCheck(names, package, add = TRUE)globalVariables(names, package, add=TRUE)suppressForeignCheck(names, package, add=TRUE)
names | The character vector of object names. If omitted, the current list ofglobal variables declared in the package will be returned, unchanged. |
package | The relevant package, usually the character string name of the packagebut optionally its corresponding namespace environment. When the call to |
add | Should the contents of |
The lists of declared global variables and native symbol objects arestored in a metadata object in the package's namespace, assuming theglobalVariables orsuppressForeignCheck call(s) occuras top-level calls in the package's source code.
The check command, as implemented in packagetools, queriesthe list before checking theR source code in the package forpossible problems.
globalVariables was introduced inR 2.15.1 andsuppressForeignCheck was introduced inR 3.1.0 so bothshould be used conditionally: see the example.
globalVariables returns the current list of declared globalvariables, possibly modified by this call.
suppressForeignCheck returns the current list of nativesymbol objects which are not to be checked.
The global variables list really belongs to a restricted scope (afunction or a group of method definitions, for example) rather thanthe package as a whole. However, implementing finer control wouldrequire changes incheck and/or incodetools, so in thisversion the information is stored at the package level.
John Chambers and Duncan Murdoch
## Not run: ## assume your package has some code that assigns ".obj1" and ".obj2"## but not in a way that codetools can find.## In the same source file (to remind you that you did it) add:if(getRversion() >= "2.15.1") utils::globalVariables(c(".obj1", "obj2"))## To suppress messages about a run-time calculated native symbol, ## save it to a local variable.## At top level, put this:if(getRversion() >= "3.1.0") utils::suppressForeignCheck("localvariable")## Within your function, do the call like this:localvariable <- if (condition) entry1 else entry2.Call(localvariable, 1, 2, 3)## HOWEVER, it is much better practice to write code## that can be checked thoroughly, e.g.if(condition) .Call(entry1, 1, 2, 3) else .Call(entry2, 1, 2, 3)## End(Not run)## Not run:## assume your package has some code that assigns ".obj1" and ".obj2"## but not in a way that codetools can find.## In the same source file (to remind you that you did it) add:if(getRversion()>="2.15.1") utils::globalVariables(c(".obj1","obj2"))## To suppress messages about a run-time calculated native symbol,## save it to a local variable.## At top level, put this:if(getRversion()>="3.1.0") utils::suppressForeignCheck("localvariable")## Within your function, do the call like this:localvariable<-if(condition) entry1else entry2.Call(localvariable,1,2,3)## HOWEVER, it is much better practice to write code## that can be checked thoroughly, e.g.if(condition) .Call(entry1,1,2,3)else .Call(entry2,1,2,3)## End(Not run)
Create and manipulate mutable hash tables.
hashtab(type = c("identical", "address"), size)gethash(h, key, nomatch = NULL)sethash(h, key, value)remhash(h, key)numhash(h)typhash(h)maphash(h, FUN)clrhash(h)is.hashtab(x)## S3 method for class 'hashtab'h[[key, nomatch = NULL, ...]]## S3 replacement method for class 'hashtab'h[[key, ...]] <- value## S3 method for class 'hashtab'print(x, ...)## S3 method for class 'hashtab'format(x, ...)## S3 method for class 'hashtab'length(x)## S3 method for class 'hashtab'str(object, ...)hashtab(type= c("identical","address"), size)gethash(h, key, nomatch=NULL)sethash(h, key, value)remhash(h, key)numhash(h)typhash(h)maphash(h, FUN)clrhash(h)is.hashtab(x)## S3 method for class 'hashtab'h[[key, nomatch=NULL,...]]## S3 replacement method for class 'hashtab'h[[key,...]]<- value## S3 method for class 'hashtab'print(x,...)## S3 method for class 'hashtab'format(x,...)## S3 method for class 'hashtab'length(x)## S3 method for class 'hashtab'str(object,...)
type |
|
size | an integer specifying the expected number of entries. |
h,object | a hash table. |
key | anR object to use as a key. |
nomatch | value to return if |
value | new value to associate with |
FUN | a |
x | object to be tested, printed, or formatted. |
... | additional arguments. |
Hash tables are a data structure for efficiently associating keys withvalues. Hash tables are similar toenvironments, butkeys can be arbitrary objects. Like environments, and unlike namedlists and most other objects in R, hash tables are mutable, i.e., theyarenot copied when modified and assignment means just giving anew name to the same object.
New hash tables are created byhashtab. Two variants areavailable: keys can be considered to match if they areidentical() (type = "identical", the default), orif their addresses in memory are equal (type = "address"). Thedefault"identical" type is almost always the right choice.Thesize argument provides a hint for setting the initialhash table size. The hash table will grow if necessary, but specifyingan expected size can be more efficient.
gethash returns the value associated withkey. Ifkey is not present in the table, then the value ofnomatch is returned.
sethash adds a new key/value association or changes the currentvalue for an existing key.remhash removes the entry forkey, if there is one.
maphash callsFUN for each entry in the hash table withtwo arguments, the entry key and the entry value. The order in whichthe entries are processed is not predictable. The consequence ofFUN adding entries to the table or deleting entries from thetable is also not predictable, except that removing the entrycurrently being processed will have the desired effect.
clrhash removes all entries from the hash table.
hashtab returns a new hash table of the specifiedtype.
gethash returns the value associated withkey, ornomatch if there is no such value.
sethash returnsvalue invisibly.
remhash invisibly returnsTRUE if an entry forkey was found and removed, andFALSE if no entry wasfound.
numhash returns the current number of entries in the table.
typhash returns a character string specifying the type of thehash table, one of"identical" or"address".
maphash andclrhash returnNULL invisibly.
The interface design is based loosely on hash table support in CommonLisp.
The hash function and equality test used for"identical" hashtables are the same as the ones used internally byduplicated andunique, with twoexceptions:
Closure environments are not ignored when comparing closures.This corresponds to callingidentical() withignore.environment = FALSE, which is the default foridentical().
External pointer objects are compared as reference objects,corresponding to callingidentical() withextptr.as.ref = TRUE. This ensures that hash tables withkeys containing external pointers behave reasonably whenserialized and unserialized.
As an experimental feature, the element operator[[ can also beused to get or set hash table entries, andlength can be used toobtain the number of entries. It is not yet clear whether this is agood idea.
## Create a new empty hash table.h1 <- hashtab()h1## Add some key/value pairs.sethash(h1, NULL, 1)sethash(h1, .GlobalEnv, 2)for (i in seq_along(LETTERS)) sethash(h1, LETTERS[i], i)## Look up values for some keys.gethash(h1, NULL)gethash(h1, .GlobalEnv)gethash(h1, "Q")## Remove an entry.(remhash(h1, NULL))gethash(h1, NULL)(remhash(h1, "XYZ"))## Using the element operator.h1[["ABC"]]h1[["ABC", nomatch = 77]]h1[["ABC"]] <- "DEF"h1[["ABC"]]## Integers and real numbers that are equal are considered different## (not identical) as keys:identical(3, 3L)sethash(h1, 3L, "DEF")gethash(h1, 3L)gethash(h1, 3)## Two variables can refer to the same hash table.h2 <- h1identical(h1, h2)## set in one, see in the "other" <==> really one object with 2 namessethash(h2, NULL, 77)gethash(h1, NULL)str(h1)## An example of using maphash(): get all hashkeys of a hash table:hashkeys <- function(h) { val <- vector("list", numhash(h)) idx <- 0 maphash(h, function(k, v) { idx <<- idx + 1 val[idx] <<- list(k) }) val}kList <- hashkeys(h1)str(kList) # the *order* is "arbitrary" & cannot be "known"## Create a new empty hash table.h1<- hashtab()h1## Add some key/value pairs.sethash(h1,NULL,1)sethash(h1, .GlobalEnv,2)for(iin seq_along(LETTERS)) sethash(h1, LETTERS[i], i)## Look up values for some keys.gethash(h1,NULL)gethash(h1, .GlobalEnv)gethash(h1,"Q")## Remove an entry.(remhash(h1,NULL))gethash(h1,NULL)(remhash(h1,"XYZ"))## Using the element operator.h1[["ABC"]]h1[["ABC", nomatch=77]]h1[["ABC"]]<-"DEF"h1[["ABC"]]## Integers and real numbers that are equal are considered different## (not identical) as keys:identical(3,3L)sethash(h1,3L,"DEF")gethash(h1,3L)gethash(h1,3)## Two variables can refer to the same hash table.h2<- h1identical(h1, h2)## set in one, see in the "other" <==> really one object with 2 namessethash(h2,NULL,77)gethash(h1,NULL)str(h1)## An example of using maphash(): get all hashkeys of a hash table:hashkeys<-function(h){ val<- vector("list", numhash(h)) idx<-0 maphash(h,function(k, v){ idx<<- idx+1 val[idx]<<- list(k)}) val}kList<- hashkeys(h1)str(kList)# the *order* is "arbitrary" & cannot be "known"
hasName is a convenient way to test for one or more namesin an R object.
hasName(x, name)hasName(x, name)
x | Any object. |
name | One or more character values to look for. |
hasName(x, name) is defined to be equivalent toname %in% names(x), though it will evaluate slightly morequickly. It is intended to replace the common idiom!is.null(x$name). The latter can be unreliable due to partialname matching; see the example below.
A logical vector of the same length asname containingTRUE if the corresponding entry is innames(x).
x <- list(abc = 1, def = 2)!is.null(x$abc) # correct!is.null(x$a) # this is the wrong test!hasName(x, "abc")hasName(x, "a")x<- list(abc=1, def=2)!is.null(x$abc)# correct!is.null(x$a)# this is the wrong test!hasName(x,"abc")hasName(x,"a")
Returns the first or last parts of a vector, matrix, array, table, data frameor function. Sincehead() andtail() are genericfunctions, they have been extended to other classes, including"ts" fromstats.
head(x, ...)## Default S3 method:head(x, n = 6L, ...)## S3 method for class 'matrix'head(x, n = 6L, ...) # is exported as head.matrix()## NB: The methods for 'data.frame' and 'array' are identical to the 'matrix' one## S3 method for class 'ftable'head(x, n = 6L, ...)## S3 method for class 'function'head(x, n = 6L, ...)tail(x, ...)## Default S3 method:tail(x, n = 6L, keepnums = FALSE, addrownums, ...)## S3 method for class 'matrix'tail(x, n = 6L, keepnums = TRUE, addrownums, ...) # exported as tail.matrix()## NB: The methods for 'data.frame', 'array', and 'table'## are identical to the 'matrix' one## S3 method for class 'ftable'tail(x, n = 6L, keepnums = FALSE, addrownums, ...)## S3 method for class 'function'tail(x, n = 6L, ...).checkHT(n, d)head(x,...)## Default S3 method:head(x, n=6L,...)## S3 method for class 'matrix'head(x, n=6L,...)# is exported as head.matrix()## NB: The methods for 'data.frame' and 'array' are identical to the 'matrix' one## S3 method for class 'ftable'head(x, n=6L,...)## S3 method for class 'function'head(x, n=6L,...)tail(x,...)## Default S3 method:tail(x, n=6L, keepnums=FALSE, addrownums,...)## S3 method for class 'matrix'tail(x, n=6L, keepnums=TRUE, addrownums,...)# exported as tail.matrix()## NB: The methods for 'data.frame', 'array', and 'table'## are identical to the 'matrix' one## S3 method for class 'ftable'tail(x, n=6L, keepnums=FALSE, addrownums,...)## S3 method for class 'function'tail(x, n=6L,...).checkHT(n, d)
x | an object |
n | an integer vector of length up to |
keepnums | in each dimension, if no names in that dimension arepresent, create them using the indices included in that dimension.Ignored if |
addrownums | deprecated - |
... | arguments to be passed to or from other methods. |
d | typically |
For vector/array based objects,head() (tail()) returnsa subset of the same dimensionality asx, usually ofthe same class. For historical reasons, by default they select thefirst (last) 6 indices in the first dimension ("rows") or along thelength of a non-dimensioned vector, and the full extent (all indices)in any remaining dimensions.head.matrix() andtail.matrix() are exported.
The default and array(/matrix) methods forhead() andtail() are quite general. They will work as is for any classwhich has adim() method, alength() method (onlyrequired ifdim() returnsNULL), and a[ method(that accepts thedrop argument and can subset in alldimensions in the dimensioned case).
For functions, the lines of the deparsed function are returned ascharacter strings.
Whenx is an array(/matrix) of dimensionality two and more,tail() will add dimnames similar to how they would appear in afull printing ofx for all dimensionsk wheren[k] is specified and non-missing anddimnames(x)[[k]](ordimnames(x) itself) isNULL. Specifically, theform of the added dimnames will vary for different dimensions as follows:
k=1 (rows):"[n,]" (right justified withwhitespace padding)
k=2 (columns):"[,n]" (withno whitespacepadding)
k>2 (higher dims):"n", i.e., the indices ascharacter values
Settingkeepnums = FALSE suppresses this behaviour.
Asdata.frame subsetting (‘indexing’) keepsattributes, so do thehead() andtail()methods for data frames.
The auxiliary function.checkHT(d, n) is useful inhead(x, n) ortail(x, n) methods, checking validity ofd <- dim(x) andn.
An object (usually) likex but generally smaller. Hence, forarrays, the result corresponds tox[.., drop=FALSE].Forftable objectsx, a transformedformat(x).
For array inputs the output oftail whenkeepnums isTRUE,any dimnames vectors added for dimensions>2 are the originalnumeric indices in that dimensionas character vectors. Thismeans that, e.g., for 3-dimensional arrayarr,tail(arr, c(2,2,-1))[ , , 2] andtail(arr, c(2,2,-1))[ , , "2"] may both be valid but havecompletely different meanings.
Patrick Burns, improved and corrected by R-Core. Negative argumentadded by Vincent Goulet. Multi-dimension support added by Gabriel Becker.
head(letters)head(letters, n = -6L)head(freeny.x, n = 10L)head(freeny.y)head(gait) # 3d arrayhead(gait, c(6L, 2L))head(gait, c(6L, 2L, -1L))tail(letters)tail(letters, n = -6L)tail(freeny.x)## the bottom-right "corner" :tail(freeny.x, n = c(4, 2))tail(freeny.y)tail(gait)tail(gait, c(6L, 2L))tail(gait, c(6L, 2L, -1L))## gait without dimnames --> keepnums showing original row/col numbersa3 <- gait ; dimnames(a3) <- NULLtail(a3, c(6, 2, -1))# keepnums = TRUE is default here!tail(a3, c(6, 2, -1), keepnums = FALSE)## data frame w/ a (non-standard) attribute:treeS <- structure(trees, foo = "bar")(n <- nrow(treeS))stopifnot(exprs = { # attribute is kept identical(htS <- head(treeS), treeS[1:6, ]) identical(attr(htS, "foo") , "bar") identical(tlS <- tail(treeS), treeS[(n-5):n, ]) ## BUT if I use "useAttrib(.)", this is *not* ok, when n is of length 2: ## --- because [i,j]-indexing of data frames *also* drops "other" attributes .. identical(tail(treeS, 3:2), treeS[(n-2):n, 2:3] )})tail(library) # last lines of functionhead(stats::ftable(Titanic))## 1d-array (with named dim) :a1 <- array(1:7, 7); names(dim(a1)) <- "O2"stopifnot(exprs = { identical( tail(a1, 10), a1) identical( head(a1, 10), a1) identical( head(a1, 1), a1 [1 , drop=FALSE] ) # was a1[1] in R <= 3.6.x identical( tail(a1, 2), a1[6:7]) identical( tail(a1, 1), a1 [7 , drop=FALSE] ) # was a1[7] in R <= 3.6.x})head(letters)head(letters, n=-6L)head(freeny.x, n=10L)head(freeny.y)head(gait)# 3d arrayhead(gait, c(6L,2L))head(gait, c(6L,2L,-1L))tail(letters)tail(letters, n=-6L)tail(freeny.x)## the bottom-right "corner" :tail(freeny.x, n= c(4,2))tail(freeny.y)tail(gait)tail(gait, c(6L,2L))tail(gait, c(6L,2L,-1L))## gait without dimnames --> keepnums showing original row/col numbersa3<- gait; dimnames(a3)<-NULLtail(a3, c(6,2,-1))# keepnums = TRUE is default here!tail(a3, c(6,2,-1), keepnums=FALSE)## data frame w/ a (non-standard) attribute:treeS<- structure(trees, foo="bar")(n<- nrow(treeS))stopifnot(exprs={# attribute is kept identical(htS<- head(treeS), treeS[1:6,]) identical(attr(htS,"foo"),"bar") identical(tlS<- tail(treeS), treeS[(n-5):n,])## BUT if I use "useAttrib(.)", this is *not* ok, when n is of length 2:## --- because [i,j]-indexing of data frames *also* drops "other" attributes .. identical(tail(treeS,3:2), treeS[(n-2):n,2:3])})tail(library)# last lines of functionhead(stats::ftable(Titanic))## 1d-array (with named dim) :a1<- array(1:7,7); names(dim(a1))<-"O2"stopifnot(exprs={ identical( tail(a1,10), a1) identical( head(a1,10), a1) identical( head(a1,1), a1[1, drop=FALSE])# was a1[1] in R <= 3.6.x identical( tail(a1,2), a1[6:7]) identical( tail(a1,1), a1[7, drop=FALSE])# was a1[7] in R <= 3.6.x})
help is the primary interface to the help systems.
help(topic, package = NULL, lib.loc = NULL, verbose = getOption("verbose"), try.all.packages = getOption("help.try.all.packages"), help_type = getOption("help_type"))help(topic, package=NULL, lib.loc=NULL, verbose= getOption("verbose"), try.all.packages= getOption("help.try.all.packages"), help_type= getOption("help_type"))
topic | usually, aname or character string specifying thetopic for which help is sought. A character string (enclosed inexplicit single or double quotes) is always taken as naming a topic. If the value of See ‘Details’ for what happens if this is omitted. |
package | a name or character vector giving the packages to lookinto for documentation, or |
lib.loc | a character vector of directory names ofR libraries,or |
verbose | logical; if |
try.all.packages | logical; see |
help_type | character string: the type of help required.Possible values are |
The following types of help are available:
Plain text help
HTML help pages with hyperlinks to other topics, shown in abrowser bybrowseURL.
(On Unix-alikes,where possible an existing browser window is re-used: the macOSGUI uses its own browser window.)
If for some reason HTML help is unavailable (seestartDynamicHelp), plain text help will be usedinstead.
Forhelp only, typeset as PDF –see the section on ‘Offline help’.
The ‘factory-fresh’ default is text help except from the macOSGUI, which uses HTML help displayed in its own browser window.
The default for the type of help is selected whenR is installed –the ‘factory-fresh’ default is HTML help.
The rendering of text help will use directional quotes in suitablelocales (UTF-8 and single-byte Windows locales): sometimes the fontsused do not support these quotes so this can be turned off by settingoptions(useFancyQuotes = FALSE).
topic is not optional: if it is omittedR will give
If a package is specified, (text or, in interactive use only,HTML) information on the package, including hints/links to suitablehelp topics.
Iflib.loc only is specified, a (text) list of availablepackages.
Help onhelp itself if none of the first threearguments is specified.
Some topics need to be quoted (bybackticks) or given as acharacter string. These include those which cannot syntacticallyappear on their own such as unary and binary operators,function and control-flowreserved words (includingif,elsefor,in,repeat,while,break andnext). The otherreservedwords can be used as if they were names, for exampleTRUE,NA andInf.
If multiple help files matchingtopic are found, in interactiveuse a menu is presented for the user to choose one: in batch use thefirst on the search path is used. (For HTML help the menu will be anHTML page, otherwise a graphical menu if possible ifgetOption("menu.graphics") is true, the default.)
Note that HTML help does not make use oflib.loc: it willalways look first in the loaded packages and then along.libPaths().
Typeset documentation is produced by running the LaTeX version of thehelp page throughpdflatex: this will produce a PDF file.
The appearance of the output can be customized through a file‘Rhelp.cfg’ somewhere in your LaTeX search path: this will beinput as a LaTeX style file afterRd.sty. Someenvironment variables are consulted, notablyR_PAPERSIZE(viagetOption("papersize")) andR_RD4PDF (see‘Making manuals’ in the‘R Installation and Administration’ manual).
If there is a functionoffline_help_helper in the workspace orfurther down the search path it is used to do the typesetting,otherwise the function of that name in theutils namespace (towhich the first paragraph applies). It should accept at least twoarguments, the name of the LaTeX file to be typeset and the type(which is nowadays ignored). It accepts a third argument,texinputs, which will give the graphics path when the helpdocument contains figures, and will otherwise not be supplied.
Unlesslib.loc is specified explicitly, the loaded packages aresearched before those in the specified libraries. This ensures thatif a library is loaded from a library not in the known library trees,then the help from the loaded library is used. Iflib.loc isspecified explicitly, the loaded packages arenot searched.
If this search fails and argumenttry.all.packages isTRUE and neitherpackages norlib.loc isspecified, then all the packages in the known library trees aresearched for help ontopic and a list of (any) packages wherehelp may be found is displayed (with hyperlinks forhelp_type = "html").NB: searching all packages can be slow, especiallythe first time (caching of files by the OS can expedite subsequentsearches dramatically).
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
? for shortcuts to help topics.
help.search() or?? for finding help pageson a vague topic;help.start() which opens the HTML version of theRhelp pages;library() for listing available packages and thehelp objects they contain;data() for listing available data sets;methods().
Useprompt() to get a prototype for writinghelppages of your own package.
help()help(help) # the samehelp(lapply)help("for") # or ?"for", but quotes/backticks are neededtry({# requires working TeX installation: help(dgamma, help_type = "pdf") ## -> nicely formatted pdf -- including math formula -- for help(dgamma): system2(getOption("pdfviewer"), "dgamma.pdf", wait = FALSE)})help(package = "splines") # get help even when package is not loadedtopi <- "women"help(topi)try(help("bs", try.all.packages = FALSE)) # reports not found (an error)help("bs", try.all.packages = TRUE) # reports can be found # in package 'splines'## For programmatic use:topic <- "family"; pkg_ref <- "stats"help((topic), (pkg_ref))help()help(help)# the samehelp(lapply)help("for")# or ?"for", but quotes/backticks are neededtry({# requires working TeX installation: help(dgamma, help_type="pdf")## -> nicely formatted pdf -- including math formula -- for help(dgamma): system2(getOption("pdfviewer"),"dgamma.pdf", wait=FALSE)})help(package="splines")# get help even when package is not loadedtopi<-"women"help(topi)try(help("bs", try.all.packages=FALSE))# reports not found (an error)help("bs", try.all.packages=TRUE)# reports can be found# in package 'splines'## For programmatic use:topic<-"family"; pkg_ref<-"stats"help((topic),(pkg_ref))
Prompts the user to check they have done all that is expected of thembefore sending a post to the R-help mailing list, provides a templatefor the post with session information included and optionally sendsthe email (on Unix systems).
help.request(subject = "", address = "[email protected]", file = "R.help.request", ...)help.request(subject="", address="[email protected]", file="R.help.request",...)
subject | subject of the email. Please do not use single quotes(') in the subject! Post separate help requests for multiplequeries. |
address | recipient's email address. |
file | filename to use (if needed) for setting up the email. |
... | additional named arguments such as |
This function is not intended to replace the postingguide. Please read the guide before posting to R-help or using thisfunction (seehttps://www.r-project.org/posting-guide.html).
Thehelp.request function:
asks whether the user has consulted relevant resources,stopping and opening the relevant URL if a negative response ifgiven.
checks whether the current version ofR is being used andwhether the add-on packages are up-to-date, giving the option ofupdating where necessary.
asks whether the user has prepared appropriate (minimal,reproducible, self-contained, commented) example code ready topaste into the post.
Once this checklist has been completed a template post is preparedincluding current session information, and passed tocreate.post.
Nothing useful.
Heather Turner, based on the then current code and help page ofbug.report().
The posting guide (https://www.r-project.org/posting-guide.html),alsosessionInfo() from which you may add to the help request.
Allows for searching the help system for documentation matching agiven character string in the (file) name, alias, title, concept orkeyword entries (or any combination thereof), using eitherfuzzy matching orregular expression matching. Namesand titles of the matched help entries are displayed nicely formatted.
Vignette names, titles and keywords and demo names and titlesmay also be searched.
help.search(pattern, fields = c("alias", "concept", "title"), apropos, keyword, whatis, ignore.case = TRUE, package = NULL, lib.loc = NULL, help.db = getOption("help.db"), verbose = getOption("verbose"), rebuild = FALSE, agrep = NULL, use_UTF8 = FALSE, types = getOption("help.search.types"))??patternfield??patternhelp.search(pattern, fields= c("alias","concept","title"), apropos, keyword, whatis, ignore.case=TRUE, package=NULL, lib.loc=NULL, help.db= getOption("help.db"), verbose= getOption("verbose"), rebuild=FALSE, agrep=NULL, use_UTF8=FALSE, types= getOption("help.search.types"))??patternfield??pattern
pattern | a character string to be matched in the specifiedfields. If this is given, the arguments |
fields | a character vector specifying the fields of the helpdatabase to be searched. The entries must be abbreviations of |
apropos | a character string to be matched in the help pagetopics and title. |
keyword | a character string to be matched in the help page‘keywords’. ‘Keywords’ are really categories: thestandard categories are listed in file ‘R.home("doc")/KEYWORDS’(see also the example) and some package writers have defined theirown. If |
whatis | a character string to be matched inthe help page topics. |
ignore.case | a logical. If |
package | a character vector with the names of packages tosearch through, or |
lib.loc | a character vector describing the location ofRlibrary trees to search through, or |
help.db | a character string giving the file path to a previouslybuilt and saved help database, or |
verbose | logical; if |
rebuild | a logical indicating whether the help database shouldbe rebuilt. This will be done automatically if |
agrep | if |
use_UTF8 | logical: should results be given in UTF-8 encoding?Also changes the meaning of regexps in |
types | a character vector listing the types of documentationto search. The entries must be abbreviations of |
field | a single value of |
Upon installation of a package, a pre-built help.search index isserialized as ‘hsearch.rds’ in the ‘Meta’ directory(provided the package has any help pages). Vignettes are alsoindexed in the ‘Meta/vignette.rds’ file. These files are used tocreate the help search database viahsearch_db.
The argumentsapropos andwhatis play a role similar tothe Unix commands with the same names.
Searching withagrep = FALSE will be several times faster thanthe default (once the database is built). However, approximatesearches should be fast enough (around a second with 5000 packagesinstalled).
If possible, the help database is saved in memory for use bysubsequent calls in the session.
Note that currently the aliases in the matching help files are notdisplayed.
As with?, in?? the pattern may be prefixed with apackage name followed by:: or::: to limit the searchto that package.
For help files, ‘\keyword’ entries which are not among thestandard keywords as listed in file ‘KEYWORDS’ in theRdocumentation directory are taken as concepts. For standard keywordentries different from ‘internal’, the corresponding descriptionsfrom file ‘KEYWORDS’ are additionally taken as concepts. All‘\concept’ entries are used as concepts.
Vignettes are searched as follows. The"name" and"alias" are both the base of the vignette filename, and the"concept" entries are taken from the ‘\VignetteKeyword’entries. Vignettes are not classified using the help system"keyword" classifications. Demos are handledsimilarly to vignettes, without the"concept" search.
The results are returned in a list object of class"hsearch",which has a print method for nicely formatting the results of thequery. This mechanism is experimental, and may change in futureversions ofR.
InR.app on macOS, this will show up a browser with selectableitems. On exiting this browser, the help pages for the selected itemswill be shown in separate help windows.
The internal format of the class is undocumented and subject to change.
hsearch_db for more information on the help searchdatabase employed, and for utilities to inspect available concepts andkeywords.
help;help.start for starting the hypertext (currently HTML)version ofR's online documentation, which offers a similar searchmechanism.
RSiteSearch to access an on-line search ofR resources.
apropos uses regexps and has nice examples.
## Not run: help.search("linear models") # In case you forgot how to fit linear models## End(Not run)help.search("non-existent topic")??utils::help # All the topics matching "help" in the utils package## Documentation with topic/concept/title matching 'print'## (disabling fuzzy matching to not also match 'point')help.search("print", agrep = FALSE)help.search(apropos = "print", agrep = FALSE) # ignores concepts## Help pages with documented topics starting with 'try':help.search("^try", fields = "alias")alias??"^try" # the same## Help pages documenting high-level plots:help.search(keyword = "hplot")RShowDoc("KEYWORDS") # show all keywords## Not run:help.search("linear models")# In case you forgot how to fit linear models## End(Not run)help.search("non-existent topic")??utils::help# All the topics matching "help" in the utils package## Documentation with topic/concept/title matching 'print'## (disabling fuzzy matching to not also match 'point')help.search("print", agrep=FALSE)help.search(apropos="print", agrep=FALSE)# ignores concepts## Help pages with documented topics starting with 'try':help.search("^try", fields="alias")alias??"^try"# the same## Help pages documenting high-level plots:help.search(keyword="hplot")RShowDoc("KEYWORDS")# show all keywords
Start the hypertext (currently HTML) version ofR's onlinedocumentation.
help.start(update = FALSE, gui = "irrelevant", browser = getOption("browser"), remote = NULL)help.start(update=FALSE, gui="irrelevant", browser= getOption("browser"), remote=NULL)
update | logical: should this attempt to update the package index toreflect the currently available packages. (Not attempted if |
gui | just for compatibility with S-PLUS. |
browser | the name of the program to be used as hypertextbrowser. It should be in thePATH, or a full path specified.Alternatively, it can be anR function which will be called with aURL as its only argument. This option is normally unset on Windows,when the file-association mechanism will be used. |
remote | A character string giving a valid URL for the‘R_HOME’ directory on a remote location. |
Unlessremote is specified this requires the HTTP server to beavailable (it will be started if possible: seestartDynamicHelp).
One of the links on the index page is the HTML package index,‘R_DOC_DIR/html/packages.html’, which can be remade bymake.packages.html(). For local operation,the HTTP server will remake a temporary version of this list when thelink is first clicked, and each time thereafter check if updating isneeded (if.libPaths has changed or any of thedirectories has been changed). This can be slow, and usingupdate = TRUE will ensure that the packages list is updatedbefore launching the index page.
Argumentremote can be used to point to HTML help published byanotherR installation: it will typically only show packages from themain library of that installation.
help() for on- and off-line help in other formats.
browseURL for how the help file is displayed.
RSiteSearch to access an on-line search ofR resources.
help.start()## the 'remote' arg can be tested byhelp.start(remote = paste0("file://", R.home()))help.start()## the 'remote' arg can be tested byhelp.start(remote= paste0("file://", R.home()))
Utilities for searching the help system.
hsearch_db(package = NULL, lib.loc = NULL, types = getOption("help.search.types"), verbose = getOption("verbose"), rebuild = FALSE, use_UTF8 = FALSE)hsearch_db_concepts(db = hsearch_db())hsearch_db_keywords(db = hsearch_db())hsearch_db(package=NULL, lib.loc=NULL, types= getOption("help.search.types"), verbose= getOption("verbose"), rebuild=FALSE, use_UTF8=FALSE)hsearch_db_concepts(db= hsearch_db())hsearch_db_keywords(db= hsearch_db())
package | a character vector with the names of packages tosearch through, or |
lib.loc | a character vector describing the location ofRlibrary trees to search through, or |
types | a character vector listing the types of documentationto search.See |
verbose | a logical controlling the verbosity of building thehelp search database.See |
rebuild | a logical indicating whether the help search databaseshould be rebuilt.See |
use_UTF8 | logical: should results be given in UTF-8 encoding? |
db | a help search database as obtained by calls to |
hsearch_db() builds and caches the help search database forsubsequent use byhelp.search. (In fact, re-builds onlywhen forced (rebuild = TRUE) or “necessary”.)
The format of the help search database is still experimental, and maychange in future versions. Currently, it consists of four tables: onewith base information about all documentation objects found, includingtheir names and titles and unique ids; three more tables contain theindividual aliases, concepts and keywords together with the ids of thedocumentation objects they belong to. Separating out the latter threetables accounts for the fact that a single documentation object mayprovide several of these entries, and allows for efficient searching.
See the details inhelp.search for how searchableentries are interpreted according to help type.
hsearch_db_concepts() andhsearch_db_keywords() extractall concepts or keywords, respectively, from a help search database,and return these in a data frame together with their total frequenciesand the numbers of packages they are used in, with entries sorted indecreasing total frequency.
db <- hsearch_db()## Total numbers of documentation objects, aliases, keywords and## concepts (using the current format):sapply(db, NROW)## Can also be obtained from print method:db## 10 most frequent concepts:head(hsearch_db_concepts(), 10)## 10 most frequent keywords:head(hsearch_db_keywords(), 10)db<- hsearch_db()## Total numbers of documentation objects, aliases, keywords and## concepts (using the current format):sapply(db, NROW)## Can also be obtained from print method:db## 10 most frequent concepts:head(hsearch_db_concepts(),10)## 10 most frequent keywords:head(hsearch_db_keywords(),10)
Utility for installing add-on packages.
R CMD INSTALL [options] [-l lib] pkgsR CMD INSTALL[options][-l lib] pkgs
pkgs | a space-separated list with the path names of the packages to beinstalled. See ‘Details’. |
lib | the path name of theR library tree to install to. Alsoaccepted in the form ‘--library=lib’. Paths including spaces shouldbe quoted, using the conventions for the shell in use. |
options | a space-separated list of options through which inparticular the process for building the help files can be controlled.Use |
This will stop at the first error, so if you want all thepkgsto be tried, call this via a shell loop.
If used asR CMD INSTALL pkgs without explicitly specifyinglib, packages are installed into the library tree rooted at thefirst directory in the library path which would be used byR run inthe current environment.
To install into the library treelib, useR CMD INSTALL -llibpkgs.This prependslib to the library path forduration of the install, so required packages in the installationdirectory will be found (and used in preference to those in otherlibraries).
Bothlib and the elements ofpkgs may be absolute orrelative path names of directories.pkgs may also containnames of package archive files: these are then extracted to atemporary directory. These are tarballs containing a singledirectory, optionally compressed bygzip,bzip2,xz orcompress.Finally, binary package archive files (as created byR CMD INSTALL --build) can be supplied.
Tarballs are by default unpackaged by the internaluntarfunction: if needed an externaltar command can be specifiedby the environment variableR_INSTALL_TAR: please ensure that itcan handle the type of compression used on the tarball. (This issometimes needed for tarballs containing invalid or unsupportedsections, and can be faster on very large tarballs. SettingR_INSTALL_TAR to ‘tar.exe’ has been needed to overcomepermissions issues on some Windows systems.)
The package sources can be cleaned up prior to installation by--preclean or after by--clean: cleaning isessential if the sources are to be used with more than onearchitecture or platform.
Some package sources contain a ‘configure’ script that can bepassed arguments or variables via the option--configure-argsand--configure-vars, respectively, if necessary. The latteris useful in particular if libraries or header files needed for thepackage are in non-system directories. In this case, one can use theconfigure variablesLIBS andCPPFLAGS to specify theselocations (and set these via--configure-vars), see section‘Configuration variables’ in‘R Installation and Administration’ for more information.(If these are used more thanonce on the command line they are concatenated.) The configuremechanism can be bypassed using the option--no-configure.
If the attempt to install the package fails, leftovers are removed.If the package was already installed, the old version is restored.This happens either if a command encounters an error or if theinstall is interrupted from the keyboard: after cleaning up the scriptterminates.
For details of the locking which is done, see the section‘Locking’ in the help forinstall.packages.
Option--build can be used to tar up the installed packagefor distribution as a binary package (as used on macOS). This is donebyutils::tar unless environment variableR_INSTALL_TARis set.
By default a package is installed with static HTML help pages if andonly ifR was: use options--html and--no-html tooverride this.
Packages are not by default installed keeping the source formatting(see thekeep.source argument tosource): thiscan be enabled by the option--with-keep.source or by settingenvironment variableR_KEEP_PKG_SOURCE toyes.
Specifying the--install-tests option copies the contentsof the ‘tests’ directory into the package installation. If theR_ALWAYS_INSTALL_TESTS environment variable is set to a truevalue, the tests will be installed even if--install-tests isomitted.
UseR CMD INSTALL --help for concise usage information,including all the available options.
AnR installation can support more than one sub-architecture:currently this is most commonly used for 32- and 64-bit builds onWindows.
For such installations, the default behaviour is to try to installsource packages for all installed sub-architectures unless the packagehas a configure script or a ‘src/Makefile’ (or‘src/Makefile.win’ on Windows), when only compiled code for thesub-architecture runningR CMD INSTALL is installed.
To install a source package with compiled code only for thesub-architecture used byR CMD INSTALL, use--no-multiarch. To install just the compiled code foranother sub-architecture, use--libs-only.
There are two ways to install for all available sub-architectures. Ifthe configure script is known to work for both Windows architectures,use flag--force-biarch (and packages can specify thisvia a ‘Biarch: yes’ field in theirDESCRIPTION files).Second, a single tarball can be installed with
R CMD INSTALL --merge-multiarch mypkg_version.tar.gz
The default way to install source packages changed inR 3.6.0, sopackages are first installed to a temporary location and then (ifsuccessful) moved to the destination library directory. Some olderpackages were written in ways that assume direct installation to thedestination library.
Staged installation can currently be overridden by having a line‘StagedInstall: no’ in the package's ‘DESCRIPTION’ file,via flag--no-staged-install or by setting environmentvariableR_INSTALL_STAGED to a false value(e.g. ‘false’ or ‘no’).
Staged installation requires either--pkglock or--lock, one of which is used by default.
The options do not have to precede ‘pkgs’ on the command line,although it will be more legible if they do. All the options areprocessed before any packages, and where options have conflictingeffects the last one will win.
Some parts of the operation ofINSTALL depend on theRtemporary directory (seetempdir, usually under‘/tmp’) having both write and execution access to the accountrunningR. This is usually the case, but if ‘/tmp’ has beenmounted asnoexec, environment variableTMPDIR may needto be set to a directory from which execution is allowed.
REMOVE;.libPaths for information on using several library trees;install.packages forR-level installation of packages;update.packages for automatic update of packages usingthe Internet or a local repository.
The chapter on ‘Add-on packages’ in‘R Installation and Administration’and the chapter on ‘Creating R packages’ in‘Writing R Extensions’
viaRShowDoc or in the ‘doc/manual’subdirectory of theR source tree.
Download and install packages from CRAN-like repositories or fromlocal files.
install.packages(pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos, type), method, available = NULL, destdir = NULL, dependencies = NA, type = getOption("pkgType"), configure.args = getOption("configure.args"), configure.vars = getOption("configure.vars"), clean = FALSE, Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"), libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE, ...)install.packages(pkgs, lib, repos= getOption("repos"), contriburl= contrib.url(repos, type), method, available=NULL, destdir=NULL, dependencies=NA, type= getOption("pkgType"), configure.args= getOption("configure.args"), configure.vars= getOption("configure.vars"), clean=FALSE, Ncpus= getOption("Ncpus",1L), verbose= getOption("verbose"), libs_only=FALSE, INSTALL_opts, quiet=FALSE, keep_outputs=FALSE,...)
pkgs | character vector of the names of packages whosecurrent versions should be downloaded from the repositories. If
If this is missing, a listbox ofavailable packages is presented where possible in an interactiveRsession. |
lib | character vector giving the library directories where toinstall the packages. Recycled as needed. If missing, defaults tothe first element of |
repos | character vector, the base URL(s) of the repositoriesto use, e.g., the URL of a CRAN mirror such as Can be |
contriburl | URL(s) of the contrib sections of the repositories. Use thisargument if your repository mirror is incomplete, e.g., becauseyou mirrored only the ‘contrib’ section, or only havebinary packages. Overrides argument |
method | download method, see |
available | a matrix as returned by |
destdir | directory where downloaded packages are stored. If it is |
dependencies | logical indicating whether to also installuninstalled packages which these packages depend on/linkto/import/suggest (and so on recursively).Not used if Only supported if The default,
In all of these, |
type | character, indicating the type of package to download andinstall. Will be |
configure.args | (Used only for source installs.) A character vector or a named list.If a character vector with no names is supplied, the elements areconcatenated into a single string (separated by a space) and used asthe value for the--configure-args flag in the call to A named list can be used also to the same effect, and thatallows multi-element character strings for each packagewhich are concatenated to a single string to be used as thevalue for--configure-args. |
configure.vars | (Used only for source installs.) Analogous to |
clean | a logical value indicating whether to add the--clean flag to the call to |
Ncpus | the number of parallel processes to use for a parallelinstall of more than one source package. Values greater than oneare supported if the |
verbose | a logical indicating if some “progress report” should be given. |
libs_only | a logical value: should the--libs-only option be used toinstall only additional sub-architectures for source installs? (See also |
INSTALL_opts | an optional character vector of additional option(s) to be passed to Can also be a named list of character vectors to be used asadditional options, with names the respective package names. |
quiet | logical: if true, reduce the amount of output. This isnotpassed to |
keep_outputs | a logical: if true, keep the outputs from installing source packagesin the current working directory, with the names of the output filesthe package names with ‘.out’ appended (overwriting existingfiles, possibly from previous installation attempts). Alternatively, acharacter string giving the directory in which to save the outputs.Ignored when installing from local files. |
... | further arguments to be passed to |
This is the main function to install packages. It takes a vector ofnames and a destination library, downloads the packages from therepositories and installs them. (If the library is omitted itdefaults to the first directory in.libPaths(), with a messageif there is more than one.) Iflib is omitted or is of lengthone and is not a (group) writable directory, in interactive use thecode offers to create a personal library tree (the first element ofSys.getenv("R_LIBS_USER")) and install there.
Detection of a writable directory is problematic on Windows: see the‘Note’ section.
For installs from a repository an attempt is made to install thepackages in an order that respects their dependencies. This doesassume that all the entries inlib are on the default librarypath for installs (set by environment variableR_LIBS).
You are advised to runupdate.packages beforeinstall.packages to ensure that any already installeddependencies have their latest versions.
InvisibleNULL.
This section applies only to platforms where binary packages areavailable: Windows and CRAN builds for macOS.
R packages are primarily distributed assource packages, butbinary packages (a packaging up of the installed package) arealso supported, and the type most commonly used on Windows and by theCRAN builds for macOS. This function can install either type, either bydownloading a file from a repository or from a local file.
Possible values oftype for binary packages are either simply"binary" to denote the binaries matching the current R, ora string consisting of two or three parts separated by periods: theoperating system ("win" or"mac"), the string"binary" and optional build name (e.g.,"big-sur-arm64").The last part is optional and currently only used on macOS todisambiguate builds targeting different macOS versions orarchitectures. Example values:"win.binary" for Windows binaries and"mac.binary.big-sur-arm64" for macOS 11 (Big Sur) arm64 binaries.The corresponding binary type for the running R can be obtained via.Platform$pkgType, however, it may be"source" ifthe build does not support package binaries.
For a binary install from a repository, the function checks for theavailability of a source package on the same repository, and reportsif the source package has a later version, or is available but nobinary version is. This check can be suppressed by using
options(install.packages.check.source = "no")
and should be if there is a partial repository containing only binaryfiles.
An alternative (and the current default) is"both" which means‘use binary if available and current, otherwise trysource’. The action if there are source packages which are preferredbut may contain code which needs to be compiled is controlled bygetOption("install.packages.compile.from.source").type = "both" will be silently changed to"binary" ifeithercontriburl oravailable is specified.
Using packages withtype = "source" always works provided thepackage contains no C/C++/Fortran code that needs compilation.Otherwise,
you will need to have installed the Rtoolscollection as described in the ‘R for Windows FAQ’andyou must have thePATH environment variable set up as requiredby Rtools.
For a 32/64-bit installation ofR on Windows, a small minority ofpackages with compiled code need eitherINSTALL_opts = "--force-biarch" orINSTALL_opts = "--merge-multiarch" for asource installation. (It is safe to always set the latter wheninstalling from a repository or tarballs, although it will be a littleslower.)
When installing a package on Windows,install.packages will abortthe install if it detects that the package is already installed and iscurrently in use. In some circumstances (e.g., multiple instances ofR running at the same time and sharing a library) it will not detect aproblem, but the installation may fail as Windows locks files in use.
when the package contains C/C++/Fortran codethat needs compilation, suitable compilers and related tools needto be installed. On macOS you need to have installed the‘Command-line tools for Xcode’ (see the‘R Installation and Administration’ manual) and if neededby the package a Fortran compiler, and have them in your path.
There are various options for locking: these differ between source andbinary installs.
By default for a source install, the library directory is‘locked’ by creating a directory ‘00LOCK’ within it. Thishas two purposes: it prevents any other process installing into thatlibrary concurrently, and is used to store any previous version of thepackage to restore on error. A finer-grained locking is provided bythe option--pkglock which creates a separate lock for eachpackage: this allows enough freedom for parallelinstallation. Per-package locking is the default when installing asingle package, and for multiple packages whenNcpus > 1L.Finally locking (and restoration on error) can be suppressed by--no-lock.
For a macOS binary install, no locking is done by default. Settingargumentlock toTRUE (it defaults to the value ofgetOption("install.lock", FALSE)) will use per-directorylocking as described for source installs. For Windows binary install,per-directory locking is used by default (lock defaults to thevalue ofgetOption("install.lock", TRUE)). If the value is"pkglock" per-package locking will be used.
If package locking is used on Windows withlibs_only = TRUE andthe installation fails, the package will be restored to its previousstate.
Note that it is possible for the package installation to fail so badlythat the lock directory is not removed: this inhibits any furtherinstalls to the library directory (or for--pkglock, of thepackage) until the lock directory is removed manually.
Parallel installs are attempted ifpkgs has length greater thanone andNcpus > 1. It makes use of a parallelmake,so themake specified (defaultmake) whenR wasbuilt must be capable of supportingmake -jN: GNU make,dmake andpmake do, but Solarismake andolder FreeBSDmake do not: if necessary environment variableMAKE can be set for the current session to select a suitablemake.
install.packages needs to be able to compute all thedependencies ofpkgs fromavailable, including if oneelement ofpkgs depends indirectly on another. This means thatif for example you are installingCRAN packages which dependon Bioconductor packages which in turn depend onCRANpackages,available needs to cover bothCRAN andBioconductor packages.
A limit on the elapsed time for each call toR CMD INSTALL(so for source installs) can be setvia environment variable_R_INSTALL_PACKAGES_ELAPSED_TIMEOUT_: in seconds (or in minutesor hours with optional suffix ‘m’ or ‘h’, suffix ‘s’being allowed for the default seconds) with0 meaning no limit.
For non-parallel installs this is implementedvia thetimeout argument ofsystem2: for parallelinstallsvia the OS'stimeout command. (The onetested is from GNU coreutils, commonly available on Linux butnot other Unix-alikes. If no such command is available the timeoutrequest is ignored, with a warning. On Windows, one needs to specifya suitabletimeout command via environment variableR_TIMEOUT, because ‘c:/Windows/system32/timeout.exe’ isnot.) For parallel installs a‘Error 124’ message frommake indicates that timeoutoccurred.
Timeouts during installation might leave lock directories behind andnot restore previous versions.
If you are not running an up-to-date version ofR you may see amessage like
package 'RODBC' is not available (for R version 3.5.3)
One possibility is that the package is not available in any of theselected repositories; another is that is available but only forcurrent or recent versions ofR. ForCRAN packages takea look at the package's CRAN page (e.g.,https://cran.r-project.org/package=RODBC). If that indicates inthe ‘Depends’ field a dependence on a later version ofR youwill need to look in the ‘Old sources’ section and select the URLof a version of comparable age to yourR. Then you can supply thatURL as the first argument ofinstall.packages(): you mayneed to first manually install its dependencies.
For other repositories, usingavailable.packages(filters = "OS_type")[pkgname, ] will show if the package is availablefor anyR version (for your OS).
Some binary distributions ofR haveINSTALL in a separatebundle, e.g. anR-devel RPM.install.packages willgive an error if called withtype = "source" on such a system.
Some binary Linux distributions ofR can be installed on a machinewithout the tools needed to install packages: a possible remedy is todo a complete install ofR which should bring in all those tools asdependencies.
install.packages tries to detect if you have write permissionon the library directories specified, but Windows reports unreliably.If there is only one library directory (the default),R tries tofind out by creating a test directory, but even this need not be thewhole story: you may have permission to write in a library directorybut lack permission to write binary files (such as ‘.dll’ files)there. See the ‘R for Windows FAQ’ for workarounds.
update.packages,available.packages,download.packages,installed.packages,contrib.url.
Seedownload.file for how to handle proxies andother options to monitor file transfers.
untar for manually unpacking source package tarballs.
INSTALL,REMOVE,remove.packages,library,.packages,read.dcf
The ‘R Installation and Administration’ manual for how toset up a repository.
## Not run: ## A Linux example for Fedora's layout of udunits2 headers.install.packages(c("ncdf4", "RNetCDF"), configure.args = c(RNetCDF = "--with-netcdf-include=/usr/include/udunits2"))## End(Not run)## Not run:## A Linux example for Fedora's layout of udunits2 headers.install.packages(c("ncdf4","RNetCDF"), configure.args= c(RNetCDF="--with-netcdf-include=/usr/include/udunits2"))## End(Not run)
Find (or retrieve) details of all packages installed in the specifiedlibraries.
installed.packages(lib.loc = NULL, priority = NULL, noCache = FALSE, cache_user_dir = str2logical(Sys.getenv("R_PACKAGES_CACHE_USER_DIR", FALSE)), fields = NULL, subarch = .Platform$r_arch, ...)installed.packages(lib.loc=NULL, priority=NULL, noCache=FALSE, cache_user_dir= str2logical(Sys.getenv("R_PACKAGES_CACHE_USER_DIR",FALSE)), fields=NULL, subarch= .Platform$r_arch,...)
lib.loc | character vector describing the location ofR library trees tosearch through, or |
priority | character vector or |
noCache | do not use cached information, nor cache it. |
cache_user_dir |
|
fields | a character vector giving the fields to extract fromeach package's ‘DESCRIPTION’ file in addition to the defaultones, or |
subarch | character string or |
... | allows unused arguments to be passed down from other functions. |
installed.packages scans the ‘DESCRIPTION’ files of eachpackage found alonglib.loc and returns a matrix of packagenames, library paths and version numbers.
The information found is cached (by library) for theR session andspecifiedfields argument, and updated only if the top-levellibrary directory has been altered, for example by installing orremoving a package. If the cached information becomes confused, itcan be avoided by specifyingnoCache = TRUE.
A matrix with one row per package, row names the package names andcolumn names (currently)"Package","LibPath","Version","Priority","Depends","Imports","LinkingTo","Suggests","Enhances","OS_type","License" and"Built" (theR version the package was built under).Additional columns can be specified using thefieldsargument.
This needs to read several files per installed package, which will beslow on Windows and on some network-mounted file systems.
It will be slow when thousands of packages are installed, so do notuse it to find out if a named package is installed (usefind.package orsystem.file) nor to findout if a package is usable (callrequireNamespace orrequire and check the return value) nor to find detailsof a small number of packages (usepackageDescription).
update.packages,install.packages,INSTALL,REMOVE.
## confine search to .Library for speedstr(ip <- installed.packages(.Library, priority = "high"))ip[, c(1,3:5)]plic <- installed.packages(.Library, priority = "high", fields = "License")## what licenses are there:table( plic[, "License"] )## Recommended setup (by many pros):## Keep packages that come with R (priority="high") and all others separate!## Consequently, .Library, R's "system" library, shouldn't have any## non-"high"-priority packages :pSys <- installed.packages(.Library, priority = NA_character_)length(pSys) == 0 # TRUE under such a setup## confine search to .Library for speedstr(ip<- installed.packages(.Library, priority="high"))ip[, c(1,3:5)]plic<- installed.packages(.Library, priority="high", fields="License")## what licenses are there:table( plic[,"License"])## Recommended setup (by many pros):## Keep packages that come with R (priority="high") and all others separate!## Consequently, .Library, R's "system" library, shouldn't have any## non-"high"-priority packages :pSys<- installed.packages(.Library, priority=NA_character_)length(pSys)==0# TRUE under such a setup
Checks ifmethod is the name of a valid / registered S3method. Alternatively, whenf andclass are specified,it is checked iff is the name of an S3 generic function andpaste(f, class, sep=".") is a valid S3 method.
isS3method(method, f, class, envir = parent.frame())isS3method(method, f, class, envir= parent.frame())
method | a character string, typically of the form |
f | optional character string, typically specifying an S3 genericfunction. Used, when |
class | optional character string, typically specifying an S3class name. Used, when |
envir | the |
logicalTRUE orFALSE
isS3method("t") # FALSE - it is an S3 genericisS3method("t.default") # TRUEisS3method("t.ts") # TRUEisS3method("t.test") # FALSEisS3method("t.data.frame")# TRUEisS3method("t.lm") # FALSE - not existingisS3method("t.foo.bar") # FALSE - not existing## S3 methods with "4 parts" in their name:ff <- c("as.list", "as.matrix", "is.na", "row.names", "row.names<-")for(m in ff) if(isS3method(m)) stop("wrongly declared an S3 method: ", m)(m4 <- paste(ff, "data.frame", sep="."))for(m in m4) if(!isS3method(m)) stop("not an S3 method: ", m)isS3method("t")# FALSE - it is an S3 genericisS3method("t.default")# TRUEisS3method("t.ts")# TRUEisS3method("t.test")# FALSEisS3method("t.data.frame")# TRUEisS3method("t.lm")# FALSE - not existingisS3method("t.foo.bar")# FALSE - not existing## S3 methods with "4 parts" in their name:ff<- c("as.list","as.matrix","is.na","row.names","row.names<-")for(min ff)if(isS3method(m)) stop("wrongly declared an S3 method: ", m)(m4<- paste(ff,"data.frame", sep="."))for(min m4)if(!isS3method(m)) stop("not an S3 method: ", m)
Determines whetherf acts as a standard S3-style genericfunction.
isS3stdGeneric(f)isS3stdGeneric(f)
f | a function object |
A closure is considered a standard S3 generic if the first expressionin its body callsUseMethod. Functions which performoperations before callingUseMethod will not be considered“standard” S3 generics.
Iff is currently being traced, i.e., inheriting from class"traceable", the definition of the original untraced version ofthe function is used instead.
Iff is an S3 generic, a logical containingTRUEwith the name of the S3 generic (the string passed toUseMethod). Otherwise,FALSE (unnamed).
Front-end for creating executable programs on unix-alikes, i.e., noton Windows.
R CMD LINK [options] linkcmdR CMD LINK[options] linkcmd
linkcmd | a list of commands to link together suitable objectfiles (include library objects) to create the executable program. |
options | further options to control the linking, or forobtaining information about usage and version. |
The linker front-end is useful in particular when linking against theR shared or static library: see the examples.
The actual linking command is constructed by the version oflibtool installed at ‘R_HOME/bin’.
R CMD LINK --help gives usage information.
Some binary distributions ofR haveLINK in a separatebundle, e.g. anR-devel RPM.
This is not available on Windows.
## Not run: ## examples of front-ends linked against R.## First a C programCC=`R CMD config CC`R CMD LINK $CC -o foo foo.o `R CMD config --ldflags`## if Fortran code has been compiled into ForFoo.oFLIBS=`R CMD config FLIBS`R CMD LINK $CC -o foo foo.o ForFoo.o `R CMD config --ldflags` $FLIBS## And for a C++ front-endCXX=`R CMD config CXX`R CMD COMPILE foo.ccR CMD LINK $CXX -o foo foo.o `R CMD config --ldflags`## End(Not run)## Not run: ## examples of front-ends linked against R.## First a C programCC=`R CMD config CC`R CMD LINK$CC-o foo foo.o `R CMD config--ldflags`## if Fortran code has been compiled into ForFoo.oFLIBS=`R CMD config FLIBS`R CMD LINK$CC-o foo foo.o ForFoo.o `R CMD config--ldflags`$FLIBS## And for a C++ front-endCXX=`R CMD config CXX`R CMD COMPILE foo.ccR CMD LINK$CXX-o foo foo.o `R CMD config--ldflags`## End(Not run)
This functions aims to find a suitable coding for the locale named, bydefault the current locale, and if it is a UTF-8 locale a suitablesingle-byte encoding.
localeToCharset(locale = Sys.getlocale("LC_CTYPE"))localeToCharset(locale= Sys.getlocale("LC_CTYPE"))
locale | character string naming a locale. |
The operation differs by OS.
a locale is specified like"English_United Kingdom.1252".The final component gives the codepage, and this defines the encoding.
Locale names are normally likees_MX.iso88591. If finalcomponent indicates an encoding and it is notutf8 we just needto look up the equivalent encoding name. Otherwise, the language(herees) is used to choose a primary or fallback encoding.
In theC locale the answer will be"ASCII".
A character vector naming an encoding and possibly a fallbacksingle-encoding,NA if unknown.
The encoding names are those used bylibiconv, and ought alsoto work withglibc but maybe not with commercial Unixen.
localeToCharset()localeToCharset()
ls.str andlsf.str are variations oflsapplyingstr() to each matched name: see section Value.
ls.str(pos = -1, name, envir, all.names = FALSE, pattern, mode = "any")lsf.str(pos = -1, envir, ...)## S3 method for class 'ls_str'print(x, max.level = 1, give.attr = FALSE, ..., digits = max(1, getOption("str")$digits.d))ls.str(pos=-1, name, envir, all.names=FALSE, pattern, mode="any")lsf.str(pos=-1, envir,...)## S3 method for class 'ls_str'print(x, max.level=1, give.attr=FALSE,..., digits= max(1, getOption("str")$digits.d))
pos | integer indicating |
name | |
envir | environment to use, see |
all.names | logical indicating if names which begin with a |
pattern | aregular expression passed to |
max.level | maximal level of nesting which is applied fordisplaying nested structures, e.g., a list containing sub lists.Default 1: Display only the first nested level. |
give.attr | logical; if |
mode | character specifying the |
x | an object of class |
... | further arguments to pass. |
digits | the number of significant digits to use for printing. |
ls.str andlsf.str return an object of class"ls_str", basically the character vector of matching names(functions only forlsf.str), similarly tols, with aprint() method that callsstr()on each object.
Martin Maechler
require(stats)lsf.str() #- how do the functions look like which I am using?ls.str(mode = "list") #- what are the structured objects I have defined?## create a few objectsexample(glm, echo = FALSE)ll <- as.list(LETTERS)print(ls.str(), max.level = 0)# don't show details## which base functions have "file" in their name ?lsf.str(pos = length(search()), pattern = "file")## demonstrating that ls.str() works inside functions## ["browser/debug mode"]:tt <- function(x, y = 1) { aa <- 7; r <- x + y; ls.str() }(nms <- sapply(strsplit(capture.output(tt(2))," *: *"), `[`, 1))stopifnot(setequal(nms, c("aa", "r","x","y")))require(stats)lsf.str()#- how do the functions look like which I am using?ls.str(mode="list")#- what are the structured objects I have defined?## create a few objectsexample(glm, echo=FALSE)ll<- as.list(LETTERS)print(ls.str(), max.level=0)# don't show details## which base functions have "file" in their name ?lsf.str(pos= length(search()), pattern="file")## demonstrating that ls.str() works inside functions## ["browser/debug mode"]:tt<-function(x, y=1){ aa<-7; r<- x+ y; ls.str()}(nms<- sapply(strsplit(capture.output(tt(2))," *: *"), `[`,1))stopifnot(setequal(nms, c("aa","r","x","y")))
Show the name and email address of the maintainer of an installed package.
maintainer(pkg)maintainer(pkg)
pkg | a character string, the name of an installed package. |
Accesses thepackage descriptionto return the name and email address of the maintainer.
Questions about contributed packages should often be addressed tothe package maintainer; questions about base packages shouldusually be addressed to the R-help or R-devel mailing lists. Bugreports should be submitted using thebug.reportfunction.
A character string giving the name and email address of the maintainerof the package, orNA_character_ if no such package is installed.
David Scott[email protected] from code on R-help originallydue to Charlie Sharpsteen[email protected]; multiple correctionsby R-core.
https://stat.ethz.ch/pipermail/r-help/2010-February/230027.html
maintainer("MASS")maintainer("MASS")
Re-create the HTML list of packages.
make.packages.html(lib.loc = .libPaths(), temp = FALSE, verbose = TRUE, docdir = R.home("doc"))make.packages.html(lib.loc= .libPaths(), temp=FALSE, verbose=TRUE, docdir= R.home("doc"))
lib.loc | character vector. List of libraries to be included. |
temp | logical: should the package indices be created in atemporary location for use by the HTTP server? |
verbose | logical. If true, print out a message. |
docdir | If |
This creates the ‘packages.html’ file, either a temporary copyfor use byhelp.start, or the copy in‘R.home("doc")/html’
(for which you will need write permission).
It can be very slow, as all the package ‘DESCRIPTION’ files inall the library trees are read.
Fortemp = TRUE there is some caching of information, so thefile will only be re-created iflib.loc or any of thedirectories it lists have been changed.
Invisible logical, withFALSE indicating a failure to createthe file, probably due to lack of suitable permissions.
## Not run: make.packages.html()# this can be slow for large numbers of installed packages.## End(Not run)## Not run:make.packages.html()# this can be slow for large numbers of installed packages.## End(Not run)
Withserver = FALSE attempts to open a client socket to thespecified port and host. Withserver = TRUE theR processlistens on the specified port for a connection and then returns aserver socket. It is a good idea to useon.exit toensure that a socket is closed, as you only get 64 of them.
make.socket(host = "localhost", port, fail = TRUE, server = FALSE)make.socket(host="localhost", port, fail=TRUE, server=FALSE)
host | name of remote host |
port | port to connect to/listen on |
fail | failure to connect is an error? |
server | a server socket? |
An object of class"socket", a list with components:
socket | socket number. This is for internal use. On aUnix-alike it is a file descriptor. |
port | port number of the connection. |
host | name of remote computer. |
I don't know if the connecting host name returnedwhenserver = TRUE can be trusted. I suspect not.
Thomas Lumley
Adapted from Luke Tierney's code forXLISP-Stat, in turnbased on code from Robbins and Robbins “Practical UNIX Programming”.
Compiling in support for sockets was optional prior toR 3.3.0: seecapabilities("sockets") to see if it is available.
daytime <- function(host = "localhost"){ a <- make.socket(host, 13) on.exit(close.socket(a)) read.socket(a)}## Official time (UTC) from US Naval Observatory## Not run: daytime("tick.usno.navy.mil")daytime<-function(host="localhost"){ a<- make.socket(host,13) on.exit(close.socket(a)) read.socket(a)}## Official time (UTC) from US Naval Observatory## Not run: daytime("tick.usno.navy.mil")
menu presents the user with a menu of choices labelled from 1to the number of choices. To exit without choosing an item one canselect ‘0’.
menu(choices, graphics = FALSE, title = NULL)menu(choices, graphics=FALSE, title=NULL)
choices | a character vector of choices |
graphics | a logical indicating whether a graphics menu should beused if available. |
title | a character string to be used as the title of the menu. |
Ifgraphics = TRUE and a windowing system is available(Windows, macOS or X11via Tcl/Tk) a listbox widget isused, otherwise a text menu. It is an error to usemenu in anon-interactive session.
Ten or fewer items will be displayed in a single column, more inmultiple columns if possible within the current display width.
No title is displayed iftitle isNULL or"".
The number corresponding to the selected item, or 0 if no choice wasmade.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
select.list, which is used to implement the graphicalmenu, and allows multiple selections.
## Not run: switch(menu(c("List letters", "List LETTERS")) + 1, cat("Nothing done\n"), letters, LETTERS)## End(Not run)## Not run:switch(menu(c("List letters","List LETTERS"))+1, cat("Nothing done\n"), letters, LETTERS)## End(Not run)
List all available methods for a S3 and S4 generic function, or allmethods for an S3 or S4 class.
methods(generic.function, class, all.names = FALSE, dropPath = FALSE).S3methods(generic.function, class, envir = parent.frame(), all.names = FALSE, dropPath = FALSE, useEnv = FALSE)## S3 method for class 'MethodsFunction'format(x, byclass = attr(x, "byclass"), ...)## S3 method for class 'MethodsFunction'print(x, byclass = attr(x, "byclass"), ...)methods(generic.function, class, all.names=FALSE, dropPath=FALSE).S3methods(generic.function, class, envir= parent.frame(), all.names=FALSE, dropPath=FALSE, useEnv=FALSE)## S3 method for class 'MethodsFunction'format(x, byclass= attr(x,"byclass"),...)## S3 method for class 'MethodsFunction'print(x, byclass= attr(x,"byclass"),...)
generic.function | a generic function, or a character string naming ageneric function. |
class | a symbol or character string naming a class: only used if |
envir | the environment in which to look for the definition ofthe generic function, when the generic function is passed as acharacter string. |
all.names | a |
dropPath | a |
useEnv | a |
x | typically the result of |
byclass | an optional |
... | potentially further arguments passed to and from methods;unused currently. |
methods() finds S3 and S4 methods associated with either thegeneric.function orclass argument.Methods found are those provided by all loaded namespaces viaregistration, seeUseMethod; normally, this includesall packages on the currentsearch() path..S3methods()finds only S3 methods,.S4methods() finds only S4 methods.
When invoked with thegeneric.function argument, the"byclass" attribute (see Details) isFALSE, and theprint method by default displays the signatures (full names) ofS3 and S4 methods. S3 methods are printed by pasting the genericfunction and class together, separated by a ‘.’, asgeneric.class. The S3 method name is followed by an asterisk* if the method definition is not exported from the packagenamespace in which the method is defined. S4 method signatures areprinted asgeneric,class-method; S4 allows for multipledispatch, so there may be several classes in the signaturegeneric,A,B-method.
When invoked with theclass argument,"byclass" isTRUE, and theprint method by default displays the namesof the generic functions associated with the class,generic.
The source code for all functions is available. For S3 functionsexported from the namespace, enter the method at the command line asgeneric.class. For S3 functions not exported from thenamespace, seegetAnywhere orgetS3method. For S4methods, seegetMethod.
Help is available for each method, in addition to each generic. Forinteractive help, use the documentation shortcut? with thename of the generic and tab completion,?"generic<tab>" toselect the method for which help is desired.
The S3 functions listed are those whichare named like methodsand may not actually be methods (known exceptions are discarded in thecode).
An object of class"MethodsFunction", a character vector ofmethod names with"byclass" and"info" attributes. The"byclass" attribute is alogical indicating ifthe results were obtained with argumentclassdefined. The"info" attribute is a data frame with columns:
character vector of the names of the generic.
logical(), is the method “visible” to the user?When true, it typically is exported from the namespace of the packagein which it is defined, and the package isattach()edto thesearch() path.
logical(), true when the method is an S4 method.
afactor, the location or package namewhere the method was found.
The originalmethods function was written by Martin Maechler.
Chambers, J. M. (1992)Classes and methods: object-oriented programming in S.Appendix A ofStatistical Models in Seds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
For S4,getMethod,showMethods,Introduction orMethods_Details.
methods(class = "MethodsFunction") # format and printrequire(stats)methods(summary)methods(class = "aov") # S3 class## The same, with more details and more difficult to read:print(methods(class = "aov"), byclass=FALSE)methods("[[") # uses C-internal dispatchingmethods("$")methods("$<-") # replacement functionmethods("+") # binary operatormethods("Math") # group genericrequire(graphics)methods(axis) # looks like a generic, but is notmf <- methods(format) # quite a few; ... the last few :tail(cbind(meth = format(mf)))if(require(Matrix, quietly = TRUE)) {print(methods(class = "Matrix")) # S4 classm <- methods(dim) # S3 and S4 methodsprint(m)print(attr(m, "info")) # more extensive information## --> help(showMethods) for related examples}methods(class="MethodsFunction")# format and printrequire(stats)methods(summary)methods(class="aov")# S3 class## The same, with more details and more difficult to read:print(methods(class="aov"), byclass=FALSE)methods("[[")# uses C-internal dispatchingmethods("$")methods("$<-")# replacement functionmethods("+")# binary operatormethods("Math")# group genericrequire(graphics)methods(axis)# looks like a generic, but is notmf<- methods(format)# quite a few; ... the last few :tail(cbind(meth= format(mf)))if(require(Matrix, quietly=TRUE)){print(methods(class="Matrix"))# S4 classm<- methods(dim)# S3 and S4 methodsprint(m)print(attr(m,"info"))# more extensive information## --> help(showMethods) for related examples}
Functions helping to maintain CRAN, some of them may also be usefulfor administrators of other repository networks.
mirror2html(mirrors = NULL, file = "mirrors.html", head = "mirrors-head.html", foot = "mirrors-foot.html")checkCRAN(method)mirror2html(mirrors=NULL, file="mirrors.html", head="mirrors-head.html", foot="mirrors-foot.html")checkCRAN(method)
mirrors | A data frame, by default the CRAN list of mirrors is used. |
file | Aconnection or a character string. |
head | Name of optional header file. |
foot | Name of optional footer file. |
method | Download method, see |
mirror2html creates the HTML file for the CRAN list of mirrorsand invisibly returns the HTML text.
checkCRAN performs a sanity checks on all CRAN mirrors.
Modifies a possibly nested list recursively by changing a subset ofelements at each level to match a second list.
modifyList(x, val, keep.null = FALSE)modifyList(x, val, keep.null=FALSE)
x | A named |
val | A named list with components to replace correspondingcomponents in |
keep.null | If |
A modified version ofx, with the modifications determined asfollows (here, list elements are identified by their names). Elementsinval which are missing fromx are added tox.For elements that are common to both but are not both liststhemselves, the component inx is replaced (or possiblydeleted, depending on the value ofkeep.null) by the one inval. For common elements that are in both lists,x[[name]]is replaced bymodifyList(x[[name]], val[[name]]).
Deepayan Sarkar[email protected]
foo <- list(a = 1, b = list(c = "a", d = FALSE))bar <- modifyList(foo, list(e = 2, b = list(d = TRUE)))str(foo)str(bar)foo<- list(a=1, b= list(c="a", d=FALSE))bar<- modifyList(foo, list(e=2, b= list(d=TRUE)))str(foo)str(bar)
Build and query the news data base forR or add-on packages.
news(query, package = "R", lib.loc = NULL, format = NULL, reader = NULL, db = NULL)## S3 method for class 'news_db'print(x, doBrowse = interactive(), browser = getOption("browser"), ...)news(query, package="R", lib.loc=NULL, format=NULL, reader=NULL, db=NULL)## S3 method for class 'news_db'print(x, doBrowse= interactive(), browser= getOption("browser"),...)
query | an optional expression for selecting news entries. |
package | a character string giving the name of an installedadd-on package, or |
lib.loc | a character vector of directory names of R libraries,or |
format | Not yet used. |
reader | Not yet used. |
db,x | a news db obtained from |
doBrowse | logical specifying that the news should be opened inthe browser (by |
browser | the browser to be used, see |
... | potentially further arguments passed to |
Ifpackage is"R" (default), a news db is built with thenews since the 4.0.0 release ofR, corresponding to the‘NEWS’ file in theR.home("doc") directory."R-3" or"R-2" give the news forR 3.x.yorR 2.x.y respectively. Otherwise, if the given add-on package canbe found in the given libraries, it is attempted to read its news instructured form from files ‘inst/NEWS.Rd’, ‘NEWS.md’ (sinceR version 3.6.0, needs packagescommonmark andxml2 to be available), ‘NEWS’ or ‘inst/NEWS’ (inthat order). See section ‘NEWS Formats’ for the filespecifications.
Usingquery, one can select news entries from the db. Ifmissing orNULL, the complete db is returned. Otherwise,query should be an expression involving (a subset of) thevariablesVersion,Category,Date andText, and when evaluated within the db returning a logicalvector with length the number of entries in the db. The entries forwhich evaluation gaveTRUE are selected. When evaluating,Version andDate are coerced tonumeric_version andDate objects,respectively, so that the comparison operators for these classes canbe employed.
A data frame inheriting from class"news_db", withcharacter variablesVersion,Category,Date,Text andHTML, where the last two each contain theentry texts read (in plain-text and HTML format, respectively), andthe other variables may beNA if they were missing or could notbe determined. The data frame hasattributes"package" (and"subset" if thequery lead to proper subsetting).
File ‘inst/NEWS.Rd’ should be an Rd file given the entries as Rd\itemize lists, grouped according to version using\section elements. Section titles start with a suitable prefixfollowed by a space and the versionnumber, and optionally end with a (parenthesized) ISO8601 (%Y-%m-%d, seestrptime) format date(optionally including a note), for example:
\section{Changes in version 2.0 (2020-02-02, <note>)}{ \itemize{ \item .... } }The entries can be further grouped according to categories using\subsection elements named as the categories.The ‘NEWS.Rd’ file is assumed to be UTF-8-encoded (but anincluded\encoding specification takes precedence).
File ‘NEWS.md’ should contain the news in Markdown (following theCommonMark (https://commonmark.org/) specification), with theprimary heading level giving the version number after a prefixfollowed by a space, and optionally followed by a space and aparenthesized ISO 8601 format date. Where available, secondaryheadings are taken to indicate categories. To accommodate for commonpractice, news entries are only split down to the category level.
The plain text ‘NEWS’ files in add-on packages use a variety ofdifferent formats; the default news reader should be capable toextract individual news entries from a majority of packages from thestandard repositories, which use (slight variations of) the followingformat:
Entries are grouped according to version, with version header“Changes in version” at the beginning of a line, followed bya version number, optionally followed by an ISO 8601 format date,possibly parenthesized.
Entries may be grouped according to category, with a categoryheader (different from a version header) starting at the beginningof a line.
Entries are written as itemize-type lists, using one of‘o’, ‘*’, ‘-’ or ‘+’ as item tag. Entries mustbe indented, and ideally use a common indentation for the itemtexts.
Packagetools provides an (internal) utility functionnews2Rd to convert plain text ‘NEWS’ files to Rd. For‘NEWS’ files in a format which can successfully be handled by thedefault reader, package maintainers can usetools:::news2Rd(dir, "NEWS.Rd"),possibly with additional argumentcodify = TRUE,withdir a character string specifying the path to a package'sroot directory. Upon success, the ‘NEWS.Rd’ file can further beimproved and then be moved to the ‘inst’ subdirectory of thepackage source directory.
Additional formats and readers may be supported in the future.
## Build a db of all R news entries.db <- news()## Bug fixes with PR number in 4.0.0.db4 <- news(Version == "4.0.0" & grepl("^BUG", Category) & grepl("PR#", Text), db = db)nrow(db4)## print db4 to show in an HTML browser.## News from a date range ('Matrix' is there in a regular R installation):if(length(iM <- find.package("Matrix", quiet = TRUE)) && nzchar(iM)) { dM <- news(package="Matrix") stopifnot(identical(dM, news(db=dM))) dM2014 <- news("2014-01-01" <= Date & Date <= "2014-12-31", db = dM) stopifnot(paste0("1.1-", 2:4) %in% dM2014[,"Version"])}## Which categories have been in use? % R-core maybe should standardize a bit moresort(table(db[, "Category"]), decreasing = TRUE)## Entries with version >= 4.0.0table(news(Version >= "4.0.0", db = db)$Version)## do the same for R 3.x.y, more slowlydb3 <- news(package = "R-3")sort(table(db3[, "Category"]), decreasing = TRUE)## Entries with version >= 3.6.0table(news(Version >= "3.6.0", db = db3)$Version)## Build a db of all R news entries.db<- news()## Bug fixes with PR number in 4.0.0.db4<- news(Version=="4.0.0"& grepl("^BUG", Category)& grepl("PR#", Text), db= db)nrow(db4)## print db4 to show in an HTML browser.## News from a date range ('Matrix' is there in a regular R installation):if(length(iM<- find.package("Matrix", quiet=TRUE))&& nzchar(iM)){ dM<- news(package="Matrix") stopifnot(identical(dM, news(db=dM))) dM2014<- news("2014-01-01"<= Date& Date<="2014-12-31", db= dM) stopifnot(paste0("1.1-",2:4)%in% dM2014[,"Version"])}## Which categories have been in use? % R-core maybe should standardize a bit moresort(table(db[,"Category"]), decreasing=TRUE)## Entries with version >= 4.0.0table(news(Version>="4.0.0", db= db)$Version)## do the same for R 3.x.y, more slowlydb3<- news(package="R-3")sort(table(db3[,"Category"]), decreasing=TRUE)## Entries with version >= 3.6.0table(news(Version>="3.6.0", db= db3)$Version)
Interface to the systemgethostbyname, currently availableonly on unix-alikes, i.e., not on Windows.
nsl(hostname)nsl(hostname)
hostname | the name of the host. |
This was included as a test of internet connectivity, to fail ifthe node runningR is not connected. It will also returnNULLif BSD networking is not supported, including the header file‘arpa/inet.h’.
This function is not available on Windows.
The IP address, as a character string, orNULL if the call fails.
if(.Platform$OS.type == "unix") # includes Mac print( nsl("www.r-project.org") )if(.Platform$OS.type=="unix")# includes Mac print( nsl("www.r-project.org"))
Provides an estimate of the memory that is being used to store anR object.
object.size(x)## S3 method for class 'object_size'format(x, units = "b", standard = "auto", digits = 1L, ...)## S3 method for class 'object_size'print(x, quote = FALSE, units = "b", standard = "auto", digits = 1L, ...)object.size(x)## S3 method for class 'object_size'format(x, units="b", standard="auto", digits=1L,...)## S3 method for class 'object_size'print(x, quote=FALSE, units="b", standard="auto", digits=1L,...)
x | anR object. |
quote | logical, indicating whether or not the result should beprinted with surrounding quotes. |
units | the units to be used in formatting and printing the size.Allowed values for the different
For all standards, |
standard | the byte-size unit standard to be used. A characterstring, possibly abbreviated from |
digits | the number of digits after the decimal point, passed to |
... | arguments to be passed to or from other methods. |
Exactly which parts of the memory allocation should be attributed towhich object is not clear-cut. This function merely provides a roughindication: it should be reasonably accurate for atomic vectors, butdoes not detect if elements of a list are shared, for example.(Sharing amongst elements of a character vector is taken into account,but not that between character vectors in a single object.)
The calculation is of the size of the object, and excludes the spaceneeded to store its name in the symbol table.
Associated space (e.g., the environment of a function and what thepointer in aEXTPTRSXP points to) is not included in thecalculation.
Object sizes are larger on 64-bit builds than 32-bit ones, but willvery likely be the same on different platforms with the same wordlength and pointer size.
Sizes of objects using a compact internal representation may beover-estimated.
An object of class"object_size" with a length-one double value,an estimate of the memory allocation attributable to the object in bytes.
Object sizes can be formatted using byte-size units fromR's legacystandard, theIEC standard, or theSI standard.As illustrated by below tables, the legacy andIEC standards usebinary units (multiples of 1024), whereas the SI standard usesdecimal units (multiples of 1000).
For methodsformat andprint, argumentstandardspecifies which standard to use and argumentunits specifieswhich byte-size unit to use.units = "auto" chooses the largestunits in which the result is one or more (before rounding).Byte sizes are rounded todigits decimal places.standard = "auto" chooses the standard based onunits,if possible, otherwise, the legacy standard is used.
Summary ofR's legacy andIEC units:
| object size | legacy | IEC |
| 1 | 1 bytes | 1 B |
| 1024 | 1Kb | 1KiB |
| 1024^2 | 1Mb | 1MiB |
| 1024^3 | 1Gb | 1GiB |
| 1024^4 | 1Tb | 1TiB |
| 1024^5 | 1Pb | 1PiB |
| 1024^6 | 1EiB | |
| 1024^7 | 1ZiB | |
| 1024^8 | 1YiB | |
Summary ofSI units:
| object size | SI |
| 1 | 1 B |
| 1000 | 1kB |
| 1000^2 | 1MB |
| 1000^3 | 1GB |
| 1000^4 | 1TB |
| 1000^5 | 1PB |
| 1000^6 | 1EB |
| 1000^7 | 1ZB |
| 1000^8 | 1YB |
| 1000^9 | 1RB |
| 1000^10 | 1QB |
R Core; Henrik Bengtsson for the non-legacystandards.
The wikipedia page,https://en.wikipedia.org/wiki/Binary_prefix,is extensive on the different standards, usages and their history.
Memory-limits for the design limitations on object size.
object.size(letters)object.size(ls)format(object.size(library), units = "auto")sl <- object.size(rep(letters, 1000))print(sl) ## 209288 bytesprint(sl, units = "auto") ## 204.4 Kbprint(sl, units = "auto", standard = "IEC") ## 204.4 KiBprint(sl, units = "auto", standard = "SI") ## 209.3 kB(fsl <- sapply(c("Kb", "KB", "KiB"), function(u) format(sl, units = u)))stopifnot(identical( ## assert that all three are the same : unique(substr(as.vector(fsl), 1,5)), format(round(as.vector(sl)/1024, 1))))## find the 10 largest objects in the base packagez <- sapply(ls("package:base"), function(x) object.size(get(x, envir = baseenv())))if(interactive()) {as.matrix(rev(sort(z))[1:10])} else # (more constant over time): names(rev(sort(z))[1:10])object.size(letters)object.size(ls)format(object.size(library), units="auto")sl<- object.size(rep(letters,1000))print(sl)## 209288 bytesprint(sl, units="auto")## 204.4 Kbprint(sl, units="auto", standard="IEC")## 204.4 KiBprint(sl, units="auto", standard="SI")## 209.3 kB(fsl<- sapply(c("Kb","KB","KiB"),function(u) format(sl, units= u)))stopifnot(identical(## assert that all three are the same : unique(substr(as.vector(fsl),1,5)), format(round(as.vector(sl)/1024,1))))## find the 10 largest objects in the base packagez<- sapply(ls("package:base"),function(x) object.size(get(x, envir= baseenv())))if(interactive()){as.matrix(rev(sort(z))[1:10])}else# (more constant over time): names(rev(sort(z))[1:10])
package.skeleton automates some of the setup for a new sourcepackage. It creates directories, saves functions, data, and R code files toappropriate places, and creates skeleton help files and a‘Read-and-delete-me’ file describing further steps in packaging.
package.skeleton(name = "anRpackage", list, environment = .GlobalEnv, path = ".", force = FALSE, code_files = character(), encoding = "unknown")package.skeleton(name="anRpackage", list, environment= .GlobalEnv, path=".", force=FALSE, code_files= character(), encoding="unknown")
name | character string: the package name and directory name foryour package. Must be a valid package name. |
list | character vector naming theR objects to put in thepackage. Usually, at most one of |
environment | an environment where objects are looked for. See‘Details’. |
path | path to put the package directory in. |
force | If |
code_files | a character vector with the paths to R code files tobuild the package around. See ‘Details’. |
encoding | optionally a |
The argumentslist,environment, andcode_filesprovide alternative ways to initialize the package. Ifcode_files is supplied, the files so named will be sourced toform the environment, then used to generate the package skeleton.Otherwiselist defaults to the objects inenvironment(including those whose names start with.), but can be suppliedto select a subset of the objects in that environment.
Stubs of help files are generated for functions, data objects, andS4 classes and methods, using theprompt,promptClass, andpromptMethods functions.If an object from another package is intended to be imported andre-exported without changes, thepromptImport functionshould be used afterpackage.skeletonto generate a simple help file linking to the original one.
The package sources are placed in subdirectoryname ofpath. Ifcode_files is supplied, these files arecopied; otherwise, objects will be dumped into individual sourcefiles. The file names incode_files should have suffix".R" and be in the current working directory.
The filenames created for source and documentation try to be valid forall OSes known to runR. Invalid characters are replaced by ‘_’,invalid names are preceded by ‘zz’, names are converted to lowercase (to avoid case collisions on case-insensitive file systems) andfinally the converted names are made unique bymake.unique(sep = "_"). This can be done for code andhelp files but not data files (which are looked for by name). Also,the code and help files should have names starting with an ASCIIletter or digit, and this is checked and if necessaryzprepended.
Functions with names starting with a dot are placed in file‘R/name-internal.R’.
When you are done, delete the ‘Read-and-delete-me’ file, as itshould not be distributed.
Used for its side-effects.
Read the ‘Writing R Extensions’ manual for more details.
Once you have created asource package you need to install it:see the ‘R Installation and Administration’ manual,INSTALL andinstall.packages.
prompt,promptClass, andpromptMethods.
package_native_routine_registration_skeleton for helpingin preparing packages with compiled code.
require(stats)## two functions and two "data sets" :f <- function(x, y) x+yg <- function(x, y) x-yd <- data.frame(a = 1, b = 2)e <- rnorm(1000)package.skeleton(list = c("f","g","d","e"), name = "mypkg")require(stats)## two functions and two "data sets" :f<-function(x, y) x+yg<-function(x, y) x-yd<- data.frame(a=1, b=2)e<- rnorm(1000)package.skeleton(list= c("f","g","d","e"), name="mypkg")
Parses and returns the ‘DESCRIPTION’ file of a package as a"packageDescription".
Utility functions return (transformed) parts of that.
packageDescription(pkg, lib.loc = NULL, fields = NULL, drop = TRUE, encoding = "")packageVersion(pkg, lib.loc = NULL)packageDate(pkg, lib.loc = NULL, date.fields = c("Date", "Packaged", "Date/Publication", "Built"), tryFormats = c("%Y-%m-%d", "%Y/%m/%d", "%D", "%m/%d/%y"), desc = packageDescription(pkg, lib.loc=lib.loc, fields=date.fields))asDateBuilt(built)packageDescription(pkg, lib.loc=NULL, fields=NULL, drop=TRUE, encoding="")packageVersion(pkg, lib.loc=NULL)packageDate(pkg, lib.loc=NULL, date.fields= c("Date","Packaged","Date/Publication","Built"), tryFormats= c("%Y-%m-%d","%Y/%m/%d","%D","%m/%d/%y"), desc= packageDescription(pkg, lib.loc=lib.loc, fields=date.fields))asDateBuilt(built)
pkg | a character string with the package name. |
lib.loc | a character vector of directory names ofR libraries,or |
fields | a character vector giving the tags of fields to return(if other fields occur in the file they are ignored). |
drop | If |
encoding | If there is an |
date.fields | character vector of field tags to be tried. Thefirst for which |
tryFormats | date formats to try, see |
desc | optionally, a named |
built | for |
A package will not be ‘found’ unless it has a ‘DESCRIPTION’ filewhich contains a validVersion field. Different warnings aregiven when no package directory is found and when there is a suitabledirectory but no valid ‘DESCRIPTION’ file.
Anattached environment named to look like a package(e.g.,package:utils2) will be ignored.
packageVersion() is a convenience shortcut, allowing thingslikeif (packageVersion("MASS") < "7.3") { do.things }.
ForpackageDate(), ifdesc is valid, bothpkg andlib.loc are not made use of.
If a ‘DESCRIPTION’ file for the given package is found and cansuccessfully be read,packageDescription returns an object ofclass"packageDescription", which is a named list with thevalues of the (given) fields as elements and the tags as names, unlessdrop = TRUE.
If parsing the ‘DESCRIPTION’ file was not successful, it returnsa named list ofNAs with the field tags as names iffieldsis not null, andNA otherwise.
packageVersion() returns a (length-one) object of class"package_version".
packageDate() will return a"Date" object fromas.Date() orNA.
asDateBuilt(built) returns a"Date" object or signals anerror ifbuilt is invalid.
The default behavior ofpackageDate(), notably fordate.fields, is somewhat experimental and may change.
packageDescription("stats")packageDescription("stats", fields = c("Package", "Version"))packageDescription("stats", fields = "Version")packageDescription("stats", fields = "Version", drop = FALSE)if(requireNamespace("MASS") && packageVersion("MASS") < "7.3.29") message("you need to update 'MASS'")pu <- packageDate("utils")str(pu)stopifnot(identical(pu, packageDate(desc = packageDescription("utils"))), identical(pu, packageDate("stats"))) # as "utils" and "stats" are # both 'base R' and "Built" at same timepackageDescription("stats")packageDescription("stats", fields= c("Package","Version"))packageDescription("stats", fields="Version")packageDescription("stats", fields="Version", drop=FALSE)if(requireNamespace("MASS")&& packageVersion("MASS")<"7.3.29") message("you need to update 'MASS'")pu<- packageDate("utils")str(pu)stopifnot(identical(pu, packageDate(desc= packageDescription("utils"))), identical(pu, packageDate("stats")))# as "utils" and "stats" are# both 'base R' and "Built" at same time
Many environments are associated with a package; this functionattempts to determine that package.
packageName(env = parent.frame())packageName(env= parent.frame())
env | The environment whose name we seek. |
Environmentenv would be associated with a package iftopenv(env) is the namespace environment for thatpackage. Thus whenenv is the environment associated withfunctions inside a package, or local functions defined within them,packageName will normally return the package name.
Not all environments are associated with a package: for example, the global environment, or the evaluation frames of functions definedthere.packageName will returnNULL in these cases.
A length one character vector containing the name of the package,orNULL if there is no name.
getPackageName is a more elaborate functionthat can construct a name if none is found.
packageName()packageName(environment(mean))packageName()packageName(environment(mean))
Summarize information about installed packages and packagesavailable at various repositories, and automatically upgrade outdatedpackages.
packageStatus(lib.loc = NULL, repositories = NULL, method, type = getOption("pkgType"), ...)## S3 method for class 'packageStatus'summary(object, ...)## S3 method for class 'packageStatus'update(object, lib.loc = levels(object$inst$LibPath), repositories = levels(object$avail$Repository), ...)## S3 method for class 'packageStatus'upgrade(object, ask = TRUE, ...)packageStatus(lib.loc=NULL, repositories=NULL, method, type= getOption("pkgType"),...)## S3 method for class 'packageStatus'summary(object,...)## S3 method for class 'packageStatus'update(object, lib.loc= levels(object$inst$LibPath), repositories= levels(object$avail$Repository),...)## S3 method for class 'packageStatus'upgrade(object, ask=TRUE,...)
lib.loc | a character vector describing the location ofRlibrary trees to search through, or |
repositories | a character vector of URLs describing the location ofRpackage repositories on the Internet or on the local machine.These should be full paths to the appropriate ‘contrib’sections of the repositories.The default ( |
method | download method, see |
type | type of package distribution:see |
object | an object of class |
ask | if |
... | for |
There areprint andsummary methods for the"packageStatus" objects: theprint method gives a brieftabular summary and thesummary method prints the results.
Theupdate method updates the"packageStatus" object.Theupgrade method is similar toupdate.packages:it offers to install the current versions of those packages which are notcurrently up-to-date.
An object of class"packageStatus". This is a list with twocomponents
inst | a data frame with columns as thematrix returned by |
avail | a data frame with columns as thematrix returned by |
For thesummary method the result is also of class"summary.packageStatus" with additional components
Libs | a list with one element for each library |
Repos | a list with one element for each repository |
with the elements being lists of character vectors of package name foreach status.
installed.packages,available.packages
x <- packageStatus(repositories = contrib.url(findCRANmirror("web")))print(x)summary(x)## Not run: upgrade(x)x <- update(x)print(x)## End(Not run)x<- packageStatus(repositories= contrib.url(findCRANmirror("web")))print(x)summary(x)## Not run:upgrade(x)x<- update(x)print(x)## End(Not run)
Displays a representation of the object named byx in a pagerviafile.show.
page(x, method = c("dput", "print"), ...)page(x, method= c("dput","print"),...)
x | AnR object, or a character string naming an object. |
method | The default method is to dump the objectvia |
... | additional arguments for |
Ifx is a length-one character vector, it is used as the nameof an object to look up in the environment from whichpage iscalled. All other objects are displayed directly.
A default value oftitle is passed tofile.show if oneis not supplied in....
To go to a new page when graphing, seeframe.
## Not run: ## four ways to look at the code of 'page'page(page) # as an objectpage("page") # a character stringv <- "page"; page(v) # a length-one character vectorpage(utils::page) # a call## End(Not run)## Not run: ## four ways to look at the code of 'page'page(page)# as an objectpage("page")# a character stringv<-"page"; page(v)# a length-one character vectorpage(utils::page)# a call## End(Not run)
A class and utility methods for holding information about personslike name and email address.
person(given = NULL, family = NULL, middle = NULL, email = NULL, role = NULL, comment = NULL, first = NULL, last = NULL)as.person(x)## Default S3 method:as.person(x)## S3 method for class 'person'format(x, include = c("given", "family", "email", "role", "comment"), braces = list(given = "", family = "", email = c("<", ">"), role = c("[", "]"), comment = c("(", ")")), collapse = list(given = " ", family = " ", email = ", ", role = ", ", comment = ", "), ..., style = c("text", "R", "md"))## S3 method for class 'person'toBibtex(object, escape = FALSE, ...)person(given=NULL, family=NULL, middle=NULL, email=NULL, role=NULL, comment=NULL, first=NULL, last=NULL)as.person(x)## Default S3 method:as.person(x)## S3 method for class 'person'format(x, include= c("given","family","email","role","comment"), braces= list(given="", family="", email= c("<",">"), role= c("[","]"), comment= c("(",")")), collapse= list(given=" ", family=" ", email=", ", role=", ", comment=", "),..., style= c("text","R","md"))## S3 method for class 'person'toBibtex(object, escape=FALSE,...)
given | a character vector with thegiven names,or a list thereof. |
family | a character string with thefamily name,or a list thereof. |
middle | a character string with the collapsed middle name(s).Deprecated, seeDetails. |
email | a character string (or vector) giving an e-mail address(each),or a list thereof. |
role | a character vector specifying the role(s) of the person(seeDetails),or a list thereof. |
comment | a character string (or vector) providing comments,or a list thereof. |
first | a character string giving the first name.Deprecated, seeDetails. |
last | a character string giving the last name.Deprecated, seeDetails. |
x | an object for the |
include | a character vector giving the fields to be includedwhen formatting. |
braces | a list of characters (seeDetails). |
collapse | a list of characters (seeDetails). |
... | currently not used. |
style | a character string specifying the print style, with |
object | anR object inhering from class |
escape | a logical indicating whether non-ASCII characters shouldbe translated to LaTeX escape sequences. |
Objects of class"person" can hold information about anarbitrary positive number of persons. These can be obtained by onecall toperson() with list arguments, or by first creatingobjects representing single persons and combining these viac().
Theformat() method collapses information about persons intocharacter vectors (one string for each person): the fields ininclude are selected, each collapsed to a string using therespective element ofcollapse and subsequently“embraced” using the respective element ofbraces, andfinally collapsed into one string separated by white space. Ifbraces and/orcollapse do not specify characters for allfields, the defaults shown in the usage are imputed.Ifcollapse isFALSE orNA the correspondingfield is not collapsed but only the first element is used.Theprint() method calls theformat() method and printsthe result, thetoBibtex() method creates a suitable BibTeXrepresentation.
Person objects can be subscripted by fields (using$) or byposition (using[).
as.person() is a generic function. Its default method tries toreverse the default person formatting, and can also handle formattedperson entries collapsed by comma or"and" (with appropriatewhite space).
Personal names are rather tricky, e.g.,https://en.wikipedia.org/wiki/Personal_name.
The current implementation (starting from R 2.12.0) of the"person" class uses the notions ofgiven (includingmiddle names) andfamily names, as specified bygivenandfamily respectively. Earlier versions used a scheme basedon first, middle and last names, as appropriate for most of Westernculture where the given name precedes the family name, but notuniversal, as some other cultures place it after the family name, oruse no family name. To smooth the transition to the new scheme,argumentsfirst,middle andlast are stillsupported, but their use is deprecated and they must not be given incombination with the corresponding new style arguments. For personswhich are not natural persons (e.g., institutions, companies, etc.) itis appropriate to usegiven (but notfamily) for thename, e.g.,person("R Core Team", role = "aut").
The new scheme also adds the possibility of specifyingrolesbased on a subset of the MARC Code List for Relators (https://www.loc.gov/marc/relators/relaterm.html).When giving the roles of persons in the context of authoringRpackages, the following usage is suggested.
"aut"(Author) Use for full authors who have madesubstantial contributions to the package and should show up in thepackage citation.
"com"(Compiler) Use for persons who collected code(potentially in other languages) but did not make furthersubstantial contributions to the package.
"cph"(Copyright holder) Use for all copyrightholders. This is a legal concept so should use the legal name ofan institution or corporate body. Note that authors which are‘natural persons’ are by default copyright holders and sodo not need to be given this role.
"cre"(Creator) Use for the package maintainer.
"ctb"(Contributor) Use for authors who have madesmaller contributions (such as code patches etc.) but should notshow up in the package citation.
"ctr"(Contractor) Use for authors who have beencontracted to write (parts of) the package and hence do not ownintellectual property.
"dtc"(Data contributor) Use for persons whocontributed data sets for the package.
"fnd"(Funder) Use for persons or organizations thatfurnished financial support for the development of the package.
"rev"(Reviewer) Use for persons or organizationsresponsible for reviewing (parts of) the package.
"ths"(Thesis advisor) If the package is part of athesis, use for the thesis advisor.
"trl"(Translator) If the R code is a translation fromanother language (typically S), use for the translator to R.
In the old scheme, person objects were used for single persons, and aseparate"personList" class with corresponding creatorpersonList() for collections of these. The new scheme employsa single class for information about an arbitrary positive number ofpersons, eliminating the need for thepersonList mechanism.
Thecomment field can be used for “arbitrary” additionalinformation about persons. Elements named"ORCID" will betaken to giveORCID identifiers (seehttps://orcid.org/ for moreinformation), and be displayed as the correspondingURIs by theprint() andformat() methods (seeExamplesbelow). Similarly, elements named"ROR" will be taken to giveROR identifiers (seehttps://ror.org/).
Where more than one entity is given a"cph" role, thecomment field should be used to delimit who owns the copyrightto what parts of the package.
person() andas.person() return objects of class"person".
## Create a person object directly ...p1 <- person("Karl", "Pearson", email = "[email protected]")## ... or convert a string.p2 <- as.person("Ronald Aylmer Fisher")## Combining and subsetting.p <- c(p1, p2)p[1]p[-1]## Extracting fields.p$familyp$emailp[1]$email## Specifying package authors, example from "boot":## AC is the first author [aut] who wrote the S original.## BR is the second author [aut], who translated the code to R [trl],## and maintains the package [cre].b <- c(person("Angelo", "Canty", role = "aut", comment = "S original, <http://statwww.epfl.ch/davison/BMA/library.html>"), person(c("Brian", "D."), "Ripley", role = c("aut", "trl", "cre"), comment = "R port", email = "[email protected]") )b## Formatting.format(b)format(b, include = c("family", "given", "role"), braces = list(family = c("", ","), role = c("(Role(s): ", ")")))## Conversion to BibTeX author field.paste(format(b, include = c("given", "family")), collapse = " and ")toBibtex(b)## ORCID identifiers.(p3 <- person("Achim", "Zeileis", comment = c(ORCID = "0000-0003-0918-3766")))## ROR identifiers.(p4 <- person("R Core Team", comment = c(ROR = "02zz1nj61")))## Create a person object directly ...p1<- person("Karl","Pearson", email="[email protected]")## ... or convert a string.p2<- as.person("Ronald Aylmer Fisher")## Combining and subsetting.p<- c(p1, p2)p[1]p[-1]## Extracting fields.p$familyp$emailp[1]$email## Specifying package authors, example from "boot":## AC is the first author [aut] who wrote the S original.## BR is the second author [aut], who translated the code to R [trl],## and maintains the package [cre].b<- c(person("Angelo","Canty", role="aut", comment="S original, <http://statwww.epfl.ch/davison/BMA/library.html>"), person(c("Brian","D."),"Ripley", role= c("aut","trl","cre"), comment="R port", email="[email protected]"))b## Formatting.format(b)format(b, include= c("family","given","role"), braces= list(family= c("",","), role= c("(Role(s): ",")")))## Conversion to BibTeX author field.paste(format(b, include= c("given","family")), collapse=" and ")toBibtex(b)## ORCID identifiers.(p3<- person("Achim","Zeileis", comment= c(ORCID="0000-0003-0918-3766")))## ROR identifiers.(p4<- person("R Core Team", comment= c(ROR="02zz1nj61")))
Old interface providing functionality for information aboutcollections of persons. Since R 2.14.0person objectscan be combined with the correspondingc method whichsupersedes thepersonList function.
personList(...)as.personList(x)personList(...)as.personList(x)
... | person objects (inheriting from class |
x | an object the elements of which are coercible via |
a person object (inheriting from class"person")
person for the new functionality for representing andmanipulating information about persons.
Utilities for checking whether the sources of anR add-on packagework correctly, and for building a source package from them.
R CMD check [options] pkgdirsR CMD build [options] pkgdirsR CMD check[options] pkgdirsR CMD build[options] pkgdirs
pkgdirs | a list of names of directories with sources ofRadd-on packages. For |
options | further options to control the processing, or forobtaining information about usage and version of the utility. |
R CMD checkchecksR add-on packages from their sources, performing a widevariety of diagnostic checks.
R CMD build buildsR source tarballs. The name(s) of thepackages are taken from the ‘DESCRIPTION’ files and not from thedirectory names. This works entirely on a copy of the supplied sourcedirectories.
UseR CMDfoo --help to obtain usage information onutilityfoo, notably the possibleoptions.
The defaults for some of the options toR CMD build can beset by environment variables_R_BUILD_RESAVE_DATA_ and_R_BUILD_COMPACT_VIGNETTES_: see ‘Writing R Extensions’.Many of the checks inR CMD check can be turned off or on byenvironment variables: see Chapter ‘Tools’ of the‘R Internals’ manual.
By defaultR CMD build uses the"internal" option totar to prepare the tarball. An externaltarprogram can be specified by theR_BUILD_TAR environmentvariable. This may be substantially faster for very large packages,and can be needed for packages with long path names (over 100 bytes)or very large files (over 8GB): however, the resulting tarball may notbe portable.
R CMD check by default unpacks tarballs by the internaluntar function: if needed an externaltarcommand can be specified by the environment variableR_INSTALL_TAR: please ensure that it can handle the type ofcompression used on the tarball. (This is sometimes needed fortarballs containing invalid or unsupported sections, and can be fasteron very large tarballs. SettingR_INSTALL_TAR to ‘tar.exe’has been needed to overcome permissions issues on some Windowssystems.)
Only on Windows:They make use of a temporary directory specified by the environmentvariableTMPDIR and defaulting to ‘c:/TEMP’. Do ensurethat if set forward slashes are used.
The sections on ‘Checking and building packages’ and‘Processing documentation files’ in ‘Writing R Extensions’:RShowDoc("R-exts").
R front ends like the Windows GUI handle key presses and mouse clicksthrough “events” generated by the OS. These are processedautomatically by R at intervals during computations, but in some casesit may be desirable to trigger immediate event handling. Theprocess.events function does that.
process.events()process.events()
This is a simple wrapper for the C API functionR_ProcessEvents. As such, it is possible that it will notreturn if the user has signalled to interrupt the calculation.
NULL is returned invisibly.
Duncan Murdoch
See ‘Writing R Extensions’ and the ‘R for Windows FAQ’for more discussion of theR_ProcessEvents function.
Facilitate the constructing of files documentingR objects.
prompt(object, filename = NULL, name = NULL, ...)## Default S3 method:prompt(object, filename = NULL, name = NULL, force.function = FALSE, ...)## S3 method for class 'data.frame'prompt(object, filename = NULL, name = NULL, ...)promptImport(object, filename = NULL, name = NULL, importedFrom = NULL, importPage = name, ...)prompt(object, filename=NULL, name=NULL,...)## Default S3 method:prompt(object, filename=NULL, name=NULL, force.function=FALSE,...)## S3 method for class 'data.frame'prompt(object, filename=NULL, name=NULL,...)promptImport(object, filename=NULL, name=NULL, importedFrom=NULL, importPage= name,...)
object | anR object, typically a function for the defaultmethod. Can be |
filename | usually, aconnection or a character string giving thename of the file to which the documentation shell should be written.The default corresponds to a file whose name is |
name | a character string specifying the name of the object. |
force.function | a logical. If |
... | further arguments passed to or from other methods. |
importedFrom | a character string naming the package fromwhich |
importPage | a character string naming the help pagein the package from which |
Unlessfilename isNA, a documentation shell forobject is written to the file specified byfilename, anda message about this is given. For function objects, this shellcontains the proper function and argument names. R documentationfiles thus created still need to be edited and moved into the‘man’ subdirectory of the package containing the object to bedocumented.
Iffilename isNA, a list-style representation of thedocumentation shell is created and returned. Writing the shell to afile amounts tocat(unlist(x), file = filename, sep = "\n"),wherex is the list-style representation.
Whenprompt is used infor loops or scripts, theexplicitname specification will be useful.
TheimportPage argument forpromptImport needs to give the base of the name of thehelp file of the original help page. For example,theapprox function is documented in ‘approxfun.Rd’in thestats package, so if it were imported and re-exportedit should haveimportPage = "approxfun". Objects that are imported from other packages are notnormally documented unless re-exported.
Iffilename isNA, a list-style representation of thedocumentation shell. Otherwise, the name of the file written to isreturned invisibly.
The default filename may not be a valid filename under limited filesystems (e.g., those on Windows).
Currently, callingprompt on a non-function object assumes thatthe object is in fact a data set and hence documents it as such. Thismay change in future versions ofR. UsepromptData tocreate documentation skeletons for data sets.
The documentation file produced byprompt.data.frame does nothave the same format as many of the data frame documentation files inthebase package. We are trying to settle on a preferredformat for the documentation.
Douglas Bates forprompt.data.frame
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
promptData,help and the chapter on‘Writing R documentation files’ in the ‘Writing R Extensions’manual:RShowDoc("R-exts").
For creation of many help pages (for a package),seepackage.skeleton.
To prompt the user for input, seereadline.
require(graphics)prompt(plot.default)prompt(interactive, force.function = TRUE)unlink("plot.default.Rd")unlink("interactive.Rd")prompt(women) # data.frameunlink("women.Rd")prompt(sunspots) # non-data.frame dataunlink("sunspots.Rd")## Not run: ## Create a help file for each function in the .GlobalEnv:for(f in ls()) if(is.function(get(f))) prompt(name = f)## End(Not run)require(graphics)prompt(plot.default)prompt(interactive, force.function=TRUE)unlink("plot.default.Rd")unlink("interactive.Rd")prompt(women)# data.frameunlink("women.Rd")prompt(sunspots)# non-data.frame dataunlink("sunspots.Rd")## Not run:## Create a help file for each function in the .GlobalEnv:for(fin ls())if(is.function(get(f))) prompt(name= f)## End(Not run)
Generates a shell of documentation for a data set.
promptData(object, filename = NULL, name = NULL)promptData(object, filename=NULL, name=NULL)
object | anR object to be documented as a data set. |
filename | usually, aconnection or a character string giving thename of the file to which the documentation shell should be written.The default corresponds to a file whose name is |
name | a character string specifying the name of the object. |
Unlessfilename isNA, a documentation shell forobject is written to the file specified byfilename, anda message about this is given.
Iffilename isNA, a list-style representation of thedocumentation shell is created and returned. Writing the shell to afile amounts tocat(unlist(x), file = filename, sep = "\n"),wherex is the list-style representation.
Currently, only data frames are handled explicitly by the code.
Iffilename isNA, a list-style representation of thedocumentation shell. Otherwise, the name of the file written to isreturned invisibly.
promptData(sunspots)unlink("sunspots.Rd")promptData(sunspots)unlink("sunspots.Rd")
Generates a prototype of a package overview help pageusing Rd macros that dynamically extract informationfrom package metadata when building the package.
promptPackage(package, lib.loc = NULL, filename = NULL, name = NULL, final = FALSE)promptPackage(package, lib.loc=NULL, filename=NULL, name=NULL, final=FALSE)
package | a |
lib.loc | ignored. |
filename | usually, aconnection or a character string giving thename of the file to which the documentation shell should be written.The default corresponds to a file whose name is |
name | a character string specifying the name of the help topic;defaults to |
final | a logical value indicating whether to attempt tocreate a usable version of the help topic, rather than just a shell. |
Unlessfilename isNA, a documentation shell forpackage is written to the file specified byfilename, anda message about this is given.
Iffilename isNA, a list-style representation of thedocumentation shell is created and returned. Writing the shell to afile amounts tocat(unlist(x), file = filename, sep = "\n"),wherex is the list-style representation.
Iffinal isTRUE, the generated documentation will notinclude the place-holder slots for manual editing, it will be usableas-is. In most cases a manually edited file is preferable (butfinal = TRUE is certainly less work).
Iffilename isNA, a list-style representation of thedocumentation shell. Otherwise, the name of the file written to isreturned invisibly.
filename <- tempfile()promptPackage("utils", filename = filename)file.show(filename)unlink(filename)filename<- tempfile()promptPackage("utils", filename= filename)file.show(filename)unlink(filename)
These functions provide access to documentation.Documentation on a topic with namename (typically, anRobject or a data set) can be displayed by eitherhelp("name") or?name.
?topictype?topic?topictype?topic
topic | Usually, aname or character string specifying thetopic for which help is sought. Alternatively, a function call to ask for documentation on acorresponding S4 method: see the section on S4 method documentation.The calls |
type | the special type of documentation to use for this topic;for example, type |
This is a shortcut tohelp and uses its default type of help.
Some topics need to be quoted (bybackticks) or given as acharacter string. There include those which cannot syntacticallyappear on their own such as unary and binary operators,function and control-flowreserved words (includingif,elsefor,in,repeat,while,break andnext). The otherreservedwords can be used as if they were names, for exampleTRUE,NA andInf.
Authors of formal (‘S4’) methods can provide documentationon specific methods, as well as overall documentation on the methodsof a particular function. The"?" operator allows access tothis documentation in three ways.
The expressionmethods?f will look for the overalldocumentation methods for the functionf. Currently,this means the documentation file containing the aliasf-methods.
There are two different ways to look for documentation on aparticular method. The first is to supply thetopic argumentin the form of a function call, omitting thetype argument.The effect is to look for documentation on the method that would beused if this function call were actually evaluated. See the examplesbelow. If the function is not a generic (no S4 methods are definedfor it), the help reverts to documentation on the function name.
The"?" operator can also be called withtype suppliedasmethod; in this case also, thetopic argument isa function call, but the arguments are now interpreted as specifyingthe class of the argument, not the actual expression that willappear in a real call to the function. See the examples below.
The first approach will be tedious if the actual call involvescomplicated expressions, and may be slow if the arguments take along time to evaluate. The second approach avoids theseissues, but you do have to know what the classes of the actualarguments will be when they are evaluated.
Both approaches make use of any inherited methods; the signature ofthe method to be looked up is found by usingselectMethod(see the documentation forgetMethod). A limitationis that methods in packages (as opposed to regular functions) will only be found if the package exporting them is on the search list, even if it is specified explicitly using the?package::generic() notation.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
?? for finding help pages on a vague topic.
?lapply?"for" # but quotes/backticks are needed?`+`?women # information about data set "women"package?parallel # overview help page of package 'parallel'## Not run: require(methods)## define a S4 generic function and some methodscombo <- function(x, y) c(x, y)setGeneric("combo")setMethod("combo", c("numeric", "numeric"), function(x, y) x+y)## assume we have written some documentation## for combo, and its methods ....?combo # produces the function documentationmethods?combo # looks for the overall methods documentationmethod?combo("numeric", "numeric") # documentation for the method above?combo(1:10, rnorm(10)) # ... the same method, selected according to # the arguments (one integer, the other numeric)?combo(1:10, letters) # documentation for the default method## End(Not run)?lapply?"for"# but quotes/backticks are needed?`+`?women# information about data set "women"package?parallel# overview help page of package 'parallel'## Not run:require(methods)## define a S4 generic function and some methodscombo<-function(x, y) c(x, y)setGeneric("combo")setMethod("combo", c("numeric","numeric"),function(x, y) x+y)## assume we have written some documentation## for combo, and its methods ....?combo# produces the function documentationmethods?combo# looks for the overall methods documentationmethod?combo("numeric","numeric")# documentation for the method above?combo(1:10, rnorm(10))# ... the same method, selected according to# the arguments (one integer, the other numeric)?combo(1:10, letters)# documentation for the default method## End(Not run)
This page documents a mechanism to generate relevant completionsfrom a partially completed command line. It is not intended to beuseful by itself, but rather in conjunction with other mechanisms thatuse it as a backend. The functions listed in the usage sectionprovide a simple control and query mechanism. The actual interfaceconsists of a few unexported functions described further down.
rc.settings(ops, ns, args, dots, func, ipck, S3, data, help, argdb, fuzzy, quotes, files, backtick)rc.status()rc.getOption(name)rc.options(...).DollarNames(x, pattern).AtNames(x, pattern)## Default S3 method:.DollarNames(x, pattern = "")## S3 method for class 'list'.DollarNames(x, pattern = "")## S3 method for class 'environment'.DollarNames(x, pattern = "")## Default S3 method:.AtNames(x, pattern = "")findMatches(pattern, values, fuzzy, backtick)rc.settings(ops, ns, args, dots, func, ipck, S3, data, help, argdb, fuzzy, quotes, files, backtick)rc.status()rc.getOption(name)rc.options(...).DollarNames(x, pattern).AtNames(x, pattern)## Default S3 method:.DollarNames(x, pattern="")## S3 method for class 'list'.DollarNames(x, pattern="")## S3 method for class 'environment'.DollarNames(x, pattern="")## Default S3 method:.AtNames(x, pattern="")findMatches(pattern, values, fuzzy, backtick)
ops | Logical flag. Activates completion after the |
ns | Logical flag. Controls namespace related completions. |
args | Logical flag. Enables completion of function arguments. |
dots | Logical flag. If disabled, drops |
func | Logical flag. Enables detection of functions. Ifenabled, a customizable extension ( |
S3 | Logical flag. When |
ipck | Logical flag. Enables completion of installed package namesinside |
data | Logical flag. Enables completion of data sets (includingthose already visible) inside |
help | Logical flag. Enables completion of help requests startingwith a question mark, by looking inside help index files. |
argdb | Logical flag. When |
fuzzy | Logical flag. Enables fuzzy matching, where close butnon-exact matches (e.g., with different case) are considered if noexact matches are found. This feature is experimental and thedetails can change. In |
backtick | Logical flag. If enabled, non-syntactic completionsare wrapped in backticks to make them syntactically valid. This isuseful only if the backend can handle such completions. In |
quotes | Logical flag. Enables completion inR code when insidequotes. This normally leads to filename completion, but can beotherwise depending on context (for example, when the open quote ispreceded by |
files | Logical flag. Deprecated. Use |
name,... | user-settable options. Currently valid names are
Usage is similar to that of |
x | An R object for which valid names after |
pattern | A regular expression. Only matching names arereturned. |
values | character string giving set of candidate valuesin which matches are to be found. |
There are several types of completion, some of which can be disabledusingrc.settings. The arguments ofrc.settings are alllogical flags, turning specific optional completion features on andoff. All settings are on by default exceptipck,func,andfuzzy. Turn more off if your CPU cycles are valuable; youwill still retain basic completion.
The most basic level, which can not be turned off once the completionfunctionality is activated, provides completion on names visible onthe search path, along with a few special keywords (e.g.,TRUE). This type of completion is not attempted if the partial‘word’ (a.k.a. token) being completed is empty (since therewould be too many completions). The more advanced types of completionare described below.
$ and@:When theops setting is turned on, completion after$ and@ is attempted. This requires the prefix tobe evaluated, which is attempted unless it involves an explicitfunction call (implicit function calls involving the use of[,$, etcdo not inhibit evaluation).
Valid completions after the$ and@ extractors aredetermined by the generic functions.DollarNames and.AtNames respectively. A few basic methods are provided,and more can be written for custom classes. ThefindMatchesfunction can be useful for this purpose.
When thens setting is turned on, completion insidenamespaces is attempted when a token is preceded by the::or::: operators. Additionally, the basic completionmechanism is extended to include all loaded namespaces, i.e.,foopkg:: becomes a valid completion offoo if"foopkg" is a loaded namespace.
The completion of package namespaces applies only to alreadyloaded namespaces, i.e. ifMASS is not loaded,MAS will not complete toMASS::. However, attemptedcompletioninside an apparent namespace will attempt toload the namespace if it is not already loaded,e.g. trying to complete onMASS::fr will loadMASS if it is not already loaded.
When thehelp setting is turned on, completion on helptopics is attempted when a token is preceded by?.Prefixes (such asclass,method) are supported, aswell as quoted help topics containing special characters.
When theargs setting is turned on, completion on functionarguments is attempted whenever deemed appropriate. The mechanismused will currently fail if the relevant function (at the pointwhere completion is requested) was entered on a previous prompt(which implies in particular that the current line is being typedin response to a continuation prompt, usually+). Notethat separation by newlines is fine.
The list of possible argument completions that is generated can bemisleading. There is no problem for non-generic functions (exceptthat... is listed as a completion; this is intentionalas it signals the fact that the function can accept furtherarguments). However, for generic functions, it is practicallyimpossible to give a reliable argument list without evaluatingarguments (and not even then, in some cases), which is risky (inaddition to being difficult to code, which is the real reason ithasn't even been tried), especially when that argument is itselfan inline function call. Our compromise is to consider argumentsofall currently available methods of that generic. Thishas two drawbacks. First, not all listed completions may beappropriate in the call currently being constructed. Second, forgenerics with many methods (likeprint andplot),many matches will need to be considered, which may take anoticeable amount of time. Despite these drawbacks, we believethis behaviour to be more useful than the only other practicalalternative, which is to list arguments of the generic only.
Only S3 methods are currently supported in this fashion, and thatcan be turned off using theS3 setting.
Since arguments can be unnamed inR function calls, other typesof completion are also appropriate whenever argument completionis. Since there are usually many many more visible objects thanformal arguments of any particular function, possible argumentcompletions are often buried in a bunch of other possibilities.However, recall that basic completion is suppressed for blanktokens. This can be useful to list possible arguments of afunction. For example, trying to completeseq([TAB] andseq(from = 1, [TAB]) will both list only the arguments ofseq (or any of its methods), whereas trying to completeseq(length[TAB] will list both thelength.outargument and thelength( function as possible completions.Note that no attempt is made to remove arguments already supplied,as that would incur a further speed penalty.
For a few special functions (library,data, etc), the first argument is treated specially,in the sense that normal completion is suppressed, and somefunction specific completions are enabled if so requested by thesettings. Theipck setting, which controls whetherlibrary andrequire will complete oninstalled packages, is disabled by default because thefirst call toinstalled.packages is potentially timeconsuming (e.g., when packages are installed on a remote networkfile server). Note, however, that the results of a call toinstalled.packages is cached, so subsequent callsare usually fast, so turning this option on is not particularlyonerous even in such situations.
findMatches is an utility function that is used internally todetermine matches. It can be used for writing methods for.DollarNames or.AtNames, the main benefit being that itwill take the currentfuzzy setting into account.
Ifrc.settings is called without any arguments, it returns thecurrent settings as a named logical vector. Otherwise, it returnsNULL invisibly.
rc.status returns, as a list, the contents of an internal(unexported) environment that is used to record the results of thelast completion attempt. This can be useful for debugging. For suchuse, one must resist the temptation to use completion when typing thecall torc.status itself, as that then becomes the last attemptby the time the call is executed.
The items of primary interest in the returned list are:
comps | The possible completions generated by the lastcall to |
token | The token that was (or, is to be) completed, asset by the last call to |
linebuffer | The full line, as set by the last call to |
start | The start position of the token in the linebuffer, as set by the last call to |
end | The end position of the token in the linebuffer, as set by the last call to |
fileName | Logical, indicating whether the cursor iscurrently inside quotes. |
fguess | The name of the function the cursor is currently inside. |
isFirstArg | Logical. If cursor is inside a function, is it thefirst argument? |
In addition, the componentssettings andoptions givethe current values of settings and options respectively.
rc.getOption andrc.options behave much likegetOption andoptions respectively.
findMatches returns values that match the input pattern, takingthefuzzy flag into account.
There are several unexported functions in the package. Of these,a few are special because they provide the API through which othermechanisms can make use of the facilities provided by this package(they are unexported because they are not meant to be called directlyby users). The usage of these functions are:
.assignToken(text) .assignLinebuffer(line) .assignStart(start) .assignEnd(end) .completeToken(custom = TRUE) .retrieveCompletions() .getFileComp() .guessTokenFromLine() .win32consoleCompletion(linebuffer, cursorPosition, check.repeat = TRUE, minlength = -1) .addFunctionInfo(...)
The first four functions set up a completion attempt by specifying thetoken to be completed (text), and indicating where(start andend, which should be integers) the token isplaced within the complete line typed so far (line).
Potential completions of the token are generated by.completeToken, and the completions can be retrieved as anRcharacter vector using.retrieveCompletions. It is possible forthe user to specify a replacement for this function by settingrc.options("custom.completer"); if notNULL, thisfunction is called to compute potential completions. This facility ismeant to help in situations where completing as R code is notappropriate. See source code for more details. Custom completion canbe disabled by settingcustom = FALSE when calling.completeToken.
If the cursor is inside quotes, completion may be suppressed. Thefunction.getFileComp can be used after a call to.completeToken to determine if this is the case (returnsTRUE), and alternative completions generated as deemed useful.In most cases, filename completion is a reasonable fallback.
The.guessTokenFromLine function is provided for use withbackends that do not already break a line into tokens. It requiresthe linebuffer and endpoint (cursor position) to be already set, anditself sets the token and the start position. It returns the token asa character string.
The.win32consoleCompletion is similar in spirit, but is moregeared towards the Windows GUI (or rather, any front-end that has nocompletion facilities of its own). It requires the linebufferand cursor position as arguments, and returns a list with threecomponents,addition,possible andcomps. Ifthere is an unambiguous extension at the current position,addition contains the additional text that should be insertedat the cursor. If there is more than one possibility, these areavailable either as a character vector of preformatted strings inpossible, or as a single string incomps.possible consists of lines formatted using the currentwidth option, so that printing them on the console one line ata time will be a reasonable way to list them.comps is a spaceseparated (collapsed) list of the same completions, in case thefront-end wishes to display it in some other fashion.
Theminlength argument can be used to suppress completion whenthe token is too short (which can be useful if the front-end is set upto try completion on every keypress). Ifcheck.repeat isTRUE, it is detected if the same completion is being requestedmore than once in a row, and ambiguous completions are returned onlyin that case. This is an attempt to emulate GNU Readline behaviour,where a single TAB completes up to any unambiguous part, and multiplepossibilities are reported only on two consecutiveTABs.
As the various front-end interfaces evolve, the details of thesefunctions are likely to change as well.
The function.addFunctionInfo can be used to add informationabout the permitted argument names for specific functions. Multiplenamed arguments are allowed in calls to it, where the tags are namesof functions and values are character vectors representing validarguments. When theargdb setting isTRUE, these areused as a source of valid argument names for the relevant functions.
If you are uncomfortable with unsolicited evaluation of pieces ofcode, you should setops = FALSE. Otherwise, trying tocompletefoo@ba will evaluatefoo, trying to completefoo[i, 1:10]$ba will evaluatefoo[i, 1:10], etc. Thisshould not be too bad, as explicit function calls (involvingparentheses) are not evaluated in this manner. However, thiswill affect promises and lazy loaded symbols.
Deepayan Sarkar,[email protected]
Reads a file in Data Interchange Format (DIF) and creates adata frame from it.DIF is a format for data matrices such assingle spreadsheets.
read.DIF(file, header = FALSE, dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"), row.names, col.names, as.is = !stringsAsFactors, na.strings = "NA", colClasses = NA, nrows = -1, skip = 0, check.names = TRUE, blank.lines.skip = TRUE, stringsAsFactors = FALSE, transpose = FALSE, fileEncoding = "")read.DIF(file, header=FALSE, dec=".", numerals= c("allow.loss","warn.loss","no.loss"), row.names, col.names, as.is=!stringsAsFactors, na.strings="NA", colClasses=NA, nrows=-1, skip=0, check.names=TRUE, blank.lines.skip=TRUE, stringsAsFactors=FALSE, transpose=FALSE, fileEncoding="")
file | the name of the file which the data are to be read from,or aconnection, or a complete URL. The name |
header | a logical value indicating whether the spreadsheet contains thenames of the variables as its first line. If missing, the value isdetermined from the file format: |
dec | the character used in the file for decimal points. |
numerals | string indicating how to convert numbers whose conversionto double precision would lose accuracy, see |
row.names | a vector of row names. This can be a vector givingthe actual row names, or a single number giving the column of thetable which contains the row names, or character string giving thename of the table column containing the row names. If there is a header and the first row contains one fewer field thanthe number of columns, the first column in the input is used for therow names. Otherwise if Using |
col.names | a vector of optional names for the variables.The default is to use |
as.is | controls conversion of character variables (insofar asthey are not converted to logical, numeric or complex) to factors,if not otherwise specified by Note: In releases prior toR 2.12.1, cells marked as being ofcharacter type were converted to logical, numeric or complex using Note: to suppress all conversions including those of numericcolumns, set Note that |
na.strings | a character vector of strings which are to beinterpreted as |
colClasses | character. A vector of classes to be assumed forthe columns. Recycled as necessary, or if the character vector isnamed, unspecified values are taken to be Possible values are Note that |
nrows | the maximum number of rows to read in. Negative valuesare ignored. |
skip | the number of lines of the data file to skip beforebeginning to read data. |
check.names | logical. If |
blank.lines.skip | logical: if |
stringsAsFactors | logical: should character vectors be convertedto factors? |
transpose | logical, indicating if the row and columninterpretation should be transposed. Microsoft's Excel has beenknown to produce (non-standard conforming)DIF files which wouldneed |
fileEncoding | character string: if non-empty declares theencoding used on a file (not a connection or clipboard) so thecharacter data can be re-encoded. See the ‘Encoding’ sectionof the help for |
A data frame (data.frame) containing a representation ofthe data in the file. Empty input is an error unlesscol.namesis specified, when a 0-row data frame is returned: similarly givingjust a header line ifheader = TRUE results in a 0-row data frame.
The columns referred to inas.is andcolClasses includethe column of row names (if any).
Less memory will be used ifcolClasses is specified as one ofthe six atomic vector classes.
R Core;transpose option by Christoph Buser, ETH Zurich
The DIF format specification can be found by searching onhttp://www.wotsit.org/; the optional header fields are ignored.See alsohttps://en.wikipedia.org/wiki/Data_Interchange_Format.
The term is likely to lead to confusion: Windows will have a‘Windows Data Interchange Format (DIF) data format’ as part ofits WinFX system, which may or may not be compatible.
TheR Data Import/Export manual.
scan,type.convert,read.fwf for readingfixedwidthformatted input;read.table;data.frame.
## read.DIF() may need transpose = TRUE for a file exported from Exceludir <- system.file("misc", package = "utils")dd <- read.DIF(file.path(udir, "exDIF.dif"), header = TRUE, transpose = TRUE)dc <- read.csv(file.path(udir, "exDIF.csv"), header = TRUE)stopifnot(identical(dd, dc), dim(dd) == c(4,2))## read.DIF() may need transpose = TRUE for a file exported from Exceludir<- system.file("misc", package="utils")dd<- read.DIF(file.path(udir,"exDIF.dif"), header=TRUE, transpose=TRUE)dc<- read.csv(file.path(udir,"exDIF.csv"), header=TRUE)stopifnot(identical(dd, dc), dim(dd)== c(4,2))
Read fixed-format data files using Fortran-style format specifications.
read.fortran(file, format, ..., as.is = TRUE, colClasses = NA)read.fortran(file, format,..., as.is=TRUE, colClasses=NA)
file | File orconnection to read from. |
format | Character vector or list of vectors. See‘Details’ below. |
... | Other arguments for |
as.is | Keep characters as characters? |
colClasses | Variable classes to override defaults. See |
The format for a field is of one of the following forms:rFl.d,rDl.d,rXl,rAl,rIl, wherel isthe number of columns,d is the number of decimal places, andr is the number of repeats.F andD are numericformats,A is character,I is integer, andXindicates columns to be skipped. The repeat coder and decimalplace coded are always optional. The length codel isrequired except forX formats whenr is present.
For a single-line record,format should be a charactervector. For a multiline record it should be a list with a charactervector for each line.
Skipped (X) columns are not passed toread.fwf, socolClasses,col.names, and similar arguments passed toread.fwf should not reference these columns.
A data frame
read.fortran does not use actual Fortran input routines, sothe formats are at best rough approximations to the Fortran ones.In particular, specifyingd > 0 in theF orDformat will shift the decimald places to the left, even ifit is explicitly specified in the input file.
ff <- tempfile()cat(file = ff, "123456", "987654", sep = "\n")read.fortran(ff, c("F2.1","F2.0","I2"))read.fortran(ff, c("2F1.0","2X","2A1"))unlink(ff)cat(file = ff, "123456AB", "987654CD", sep = "\n")read.fortran(ff, list(c("2F3.1","A2"), c("3I2","2X")))unlink(ff)# Note that the first number is read differently than Fortran would# read it:cat(file = ff, "12.3456", "1234567", sep = "\n")read.fortran(ff, "F7.4")unlink(ff)ff<- tempfile()cat(file= ff,"123456","987654", sep="\n")read.fortran(ff, c("F2.1","F2.0","I2"))read.fortran(ff, c("2F1.0","2X","2A1"))unlink(ff)cat(file= ff,"123456AB","987654CD", sep="\n")read.fortran(ff, list(c("2F3.1","A2"), c("3I2","2X")))unlink(ff)# Note that the first number is read differently than Fortran would# read it:cat(file= ff,"12.3456","1234567", sep="\n")read.fortran(ff,"F7.4")unlink(ff)
Read a table offixedwidthformatteddata into adata.frame.
read.fwf(file, widths, header = FALSE, sep = "\t", skip = 0, row.names, col.names, n = -1, buffersize = 2000, fileEncoding = "", ...)read.fwf(file, widths, header=FALSE, sep="\t", skip=0, row.names, col.names, n=-1, buffersize=2000, fileEncoding="",...)
file | the name of the file which the data are to be read from. Alternatively, |
widths | integer vector, giving the widths of the fixed-widthfields (of one line), or list of integer vectors giving widths formultiline records. |
header | a logical value indicating whether the file contains thenames of the variables as its first line. If present, the namesmust be delimited by |
sep | character; the separator used internally; should be acharacter that does not occur in the file (except in the header). |
skip | number of initial lines to skip; see |
row.names | see |
col.names | see |
n | the maximum number of records (lines) to be read, defaultingto no limit. |
buffersize | Maximum number of lines to read at one time |
fileEncoding | character string: if non-empty declares theencoding used on a file (not a connection) so the character data canbe re-encoded. See the ‘Encoding’ section of the help for |
... | further arguments to be passed to |
Multiline records are concatenated to a single line before processing.Fields that are of zero-width or are wholly beyond the end of the lineinfile are replaced byNA.
Negative-width fields are used to indicate columns to be skipped, e.g.,-5 to skip 5 columns. These fields are not seen byread.table and so should not be included in acol.namesorcolClasses argument (nor in the header line, if present).
Reducing thebuffersize argument may reduce memory use whenreading large files with long lines. Increasingbuffersize mayresult in faster processing when enough memory is available.
Note thatread.fwf (notread.table) reads the suppliedfile, so the latter's argumentencoding will not be useful.
Adata.frame as produced byread.tablewhich is called internally.
Brian Ripley forR version: originally inPerl by Kurt Hornik.
scan andread.table.
read.fortran for another style of fixed-format files.
ff <- tempfile()cat(file = ff, "123456", "987654", sep = "\n")read.fwf(ff, widths = c(1,2,3)) #> 1 23 456 \\ 9 87 654read.fwf(ff, widths = c(1,-2,3)) #> 1 456 \\ 9 654unlink(ff)cat(file = ff, "123", "987654", sep = "\n")read.fwf(ff, widths = c(1,0, 2,3)) #> 1 NA 23 NA \\ 9 NA 87 654unlink(ff)cat(file = ff, "123456", "987654", sep = "\n")read.fwf(ff, widths = list(c(1,0, 2,3), c(2,2,2))) #> 1 NA 23 456 98 76 54unlink(ff)ff<- tempfile()cat(file= ff,"123456","987654", sep="\n")read.fwf(ff, widths= c(1,2,3))#> 1 23 456 \\ 9 87 654read.fwf(ff, widths= c(1,-2,3))#> 1 456 \\ 9 654unlink(ff)cat(file= ff,"123","987654", sep="\n")read.fwf(ff, widths= c(1,0,2,3))#> 1 NA 23 NA \\ 9 NA 87 654unlink(ff)cat(file= ff,"123456","987654", sep="\n")read.fwf(ff, widths= list(c(1,0,2,3), c(2,2,2)))#> 1 NA 23 456 98 76 54unlink(ff)
read.socket reads a string from the specified socket,write.socket writes to the specified socket. There is verylittle error checking done by either.
read.socket(socket, maxlen = 256L, loop = FALSE)write.socket(socket, string)read.socket(socket, maxlen=256L, loop=FALSE)write.socket(socket, string)
socket | a socket object. |
maxlen | maximum length (in bytes) of string to read. |
loop | wait for ever if there is nothing to read? |
string | string to write to socket. |
read.socket returns the string read as a length-one character vector.
write.socket returns the number of bytes written.
Thomas Lumley
finger <- function(user, host = "localhost", port = 79, print = TRUE){ if (!is.character(user)) stop("user name must be a string") user <- paste(user,"\r\n") socket <- make.socket(host, port) on.exit(close.socket(socket)) write.socket(socket, user) output <- character(0) repeat{ ss <- read.socket(socket) if (ss == "") break output <- paste(output, ss) } close.socket(socket) if (print) cat(output) invisible(output)}## Not run: finger("root") ## only works if your site provides a finger daemon## End(Not run)finger<-function(user, host="localhost", port=79, print=TRUE){if(!is.character(user)) stop("user name must be a string") user<- paste(user,"\r\n") socket<- make.socket(host, port) on.exit(close.socket(socket)) write.socket(socket, user) output<- character(0)repeat{ ss<- read.socket(socket)if(ss=="")break output<- paste(output, ss)} close.socket(socket)if(print) cat(output) invisible(output)}## Not run:finger("root")## only works if your site provides a finger daemon## End(Not run)
Reads a file in table format and creates a data frame from it, withcases corresponding to lines and variables to fields in the file.
read.table(file, header = FALSE, sep = "", quote = "\"'", dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"), row.names, col.names, as.is = !stringsAsFactors, tryLogical = TRUE, na.strings = "NA", colClasses = NA, nrows = -1, skip = 0, check.names = TRUE, fill = !blank.lines.skip, strip.white = FALSE, blank.lines.skip = TRUE, comment.char = "#", allowEscapes = FALSE, flush = FALSE, stringsAsFactors = FALSE, fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)read.csv(file, header = TRUE, sep = ",", quote = "\"", dec = ".", fill = TRUE, comment.char = "", ...)read.csv2(file, header = TRUE, sep = ";", quote = "\"", dec = ",", fill = TRUE, comment.char = "", ...)read.delim(file, header = TRUE, sep = "\t", quote = "\"", dec = ".", fill = TRUE, comment.char = "", ...)read.delim2(file, header = TRUE, sep = "\t", quote = "\"", dec = ",", fill = TRUE, comment.char = "", ...)read.table(file, header=FALSE, sep="", quote="\"'", dec=".", numerals= c("allow.loss","warn.loss","no.loss"), row.names, col.names, as.is=!stringsAsFactors, tryLogical=TRUE, na.strings="NA", colClasses=NA, nrows=-1, skip=0, check.names=TRUE, fill=!blank.lines.skip, strip.white=FALSE, blank.lines.skip=TRUE, comment.char="#", allowEscapes=FALSE, flush=FALSE, stringsAsFactors=FALSE, fileEncoding="", encoding="unknown", text, skipNul=FALSE)read.csv(file, header=TRUE, sep=",", quote="\"", dec=".", fill=TRUE, comment.char="",...)read.csv2(file, header=TRUE, sep=";", quote="\"", dec=",", fill=TRUE, comment.char="",...)read.delim(file, header=TRUE, sep="\t", quote="\"", dec=".", fill=TRUE, comment.char="",...)read.delim2(file, header=TRUE, sep="\t", quote="\"", dec=",", fill=TRUE, comment.char="",...)
file | the name of the file which the data are to be read from.Each row of the table appears as one line of the file. If it doesnot contain anabsolute path, the file name isrelative to the current working directory, Alternatively,
|
header | a logical value indicating whether the file contains thenames of the variables as its first line. If missing, the value isdetermined from the file format: |
sep | the field separator character. Values on each line of thefile are separated by this character. If |
quote | the set of quoting characters. To disable quotingaltogether, use |
dec | the character used in the file for decimal points. |
numerals | string indicating how to convert numbers whose conversionto double precision would lose accuracy, see |
row.names | a vector of row names. This can be a vector givingthe actual row names, or a single number giving the column of thetable which contains the row names, or character string giving thename of the table column containing the row names. If there is a header and the first row contains one fewer field thanthe number of columns, the first column in the input is used for therow names. Otherwise if Using |
col.names | a vector of optional names for the variables.The default is to use |
as.is | controls conversion of character variables (insofar asthey are not converted to logical, numeric or complex) to factors,if not otherwise specified by Note: to suppress all conversions including those of numericcolumns, set Note that |
tryLogical | a |
na.strings | a character vector of strings which are to beinterpreted as |
colClasses | character. A vector of classes to be assumed forthe columns. If unnamed, recycled as necessary. If named, namesare matched with unspecified values being taken to be Possible values are Note that |
nrows | integer: the maximum number of rows to read in. Negativeand other invalid values are ignored. |
skip | integer: the number of lines of the data file to skip beforebeginning to read data. |
check.names | logical. If |
fill | logical. If |
strip.white | logical. Used only when |
blank.lines.skip | logical: if |
comment.char | character: a character vector of length onecontaining a single character or an empty string. Use |
allowEscapes | logical. Should C-style escapes such as‘\n’ be processed or read verbatim (the default)? Note that ifnot within quotes these could be interpreted as a delimiter (but notas a comment character). For more details see |
flush | logical: if |
stringsAsFactors | logical: should character vectors be convertedto factors? Note that this is overridden by |
fileEncoding | character string: if non-empty declares theencoding used on a file when given as a character string (not on anexisting connection) so the character data canbe re-encoded. See the ‘Encoding’ section of the help for |
encoding | encoding to be assumed for input strings. It isused to mark character strings as known to be inLatin-1 or UTF-8 (see |
text | character string: if |
skipNul | logical: shouldNULs be skipped? |
... | Further arguments to be passed to |
This function is the principal means of reading tabular data intoR.
UnlesscolClasses is specified, all columns are read ascharacter columns and then converted usingtype.convertto logical, integer, numeric, complex or (depending onas.is)factor as appropriate. Quotes are (by default) interpreted in allfields, so a column of values like"42" will result in aninteger column.
A field or line is ‘blank’ if it contains nothing (exceptwhitespace if no separator is specified) before a comment character orthe end of the field or line.
Ifrow.names is not specified and the header line has one lessentry than the number of columns, the first column is taken to be therow names. This allows data frames to be read in from the format inwhich they are printed. Ifrow.names is specified and doesnot refer to the first column, that column is discarded from such files.
The number of data columns is determined by looking at the first fivelines of input (or the whole input if it has less than five lines), orfrom the length ofcol.names if it is specified and is longer.This could conceivably be wrong iffill orblank.lines.skip are true, so specifycol.names ifnecessary (as in the ‘Examples’).
read.csv andread.csv2 are identical toread.table except for the defaults. They are intended forreading ‘comma separated value’ files (‘.csv’) or(read.csv2) the variant used in countries that use a comma asdecimal point and a semicolon as field separator. Similarly,read.delim andread.delim2 are for reading delimitedfiles, defaulting to the TAB character for the delimiter. Notice thatheader = TRUE andfill = TRUE in these variants, andthat the comment character is disabled.
The rest of the line after a comment character is skipped; quotesare not processed in comments. Complete comment lines are allowedprovidedblank.lines.skip = TRUE; however, comment lines priorto the header must have the comment character in the first non-blankcolumn.
Quoted fields with embedded newlines are supported except after acomment character. EmbeddedNULs are unsupported: skipping them (withskipNul = TRUE) may work.
A data frame (data.frame) containing a representation ofthe data in the file.
Empty input is an error unlesscol.names is specified, when a0-row data frame is returned: similarly giving just a header line ifheader = TRUE results in a 0-row data frame. Note that ineither case the columns will be logical unlesscolClasses wassupplied.
Character strings in the result (including factor levels) will have adeclared encoding ifencoding is"latin1" or"UTF-8".
See the help onwrite.csv for the various conventionsfor.csv files. The commonest form of CSV file with row namesneeds to be read withread.csv(..., row.names = 1) to use thenames in the first column of the file as row names.
These functions can use a surprising amount of memory when readinglarge files. There is extensive discussion in the ‘R DataImport/Export’ manual, supplementing the notes here.
Less memory will be used ifcolClasses is specified as one ofthe sixatomic vector classes. This can be particularly so whenreading a column that takes many distinct numeric values, as storingeach distinct value as a character string can take up to 14 times asmuch memory as storing it as an integer.
Usingnrows, even as a mild over-estimate, will help memoryusage.
Usingcomment.char = "" will be appreciably faster than theread.table default.
read.table is not the right tool for reading large matrices,especially those with many columns: it is designed to readdata frames which may have columns of very different classes.Usescan instead for matrices.
The columns referred to inas.is andcolClasses includethe column of row names (if any).
There are two approaches for reading input that is not in the localencoding. If the input is known to be UTF-8 or Latin1, use theencoding argument to declare that. If the input is in someother encoding, then it may be translated on input. ThefileEncodingargument achieves this by setting up a connection to do the re-encodinginto the current locale. Note that on Windows or other systems not runningin a UTF-8 locale, this may not be possible.
Chambers, J. M. (1992)Data for models.Chapter 3 ofStatistical Models in Seds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
The ‘R Data Import/Export’ manual.
scan,type.convert,read.fwf for readingfixedwidthformatted input;write.table;data.frame.
count.fields can be useful to determine problems withreading files which result in reports of incorrect record lengths (seethe ‘Examples’ below).
https://www.rfc-editor.org/rfc/rfc4180 for theIANA definition ofCSV files (which requires comma as separator and CRLF line endings).
## using count.fields to handle unknown maximum number of fields## when fill = TRUEtest1 <- c(1:5, "6,7", "8,9,10")tf <- tempfile()writeLines(test1, tf)read.csv(tf, fill = TRUE) # 1 columnncol <- max(count.fields(tf, sep = ","))read.csv(tf, fill = TRUE, header = FALSE, col.names = paste0("V", seq_len(ncol)))unlink(tf)## "Inline" data set, using text=## Notice that leading and trailing empty lines are auto-trimmedread.table(header = TRUE, text = "a b1 23 4")## using count.fields to handle unknown maximum number of fields## when fill = TRUEtest1<- c(1:5,"6,7","8,9,10")tf<- tempfile()writeLines(test1, tf)read.csv(tf, fill=TRUE)# 1 columnncol<- max(count.fields(tf, sep=","))read.csv(tf, fill=TRUE, header=FALSE, col.names= paste0("V", seq_len(ncol)))unlink(tf)## "Inline" data set, using text=## Notice that leading and trailing empty lines are auto-trimmedread.table(header=TRUE, text= "a b1234")
On Windows, read values of keys in the Windows Registry, andoptionally whole hives.
readRegistry(key, hive = c("HLM", "HCR", "HCU", "HU", "HCC", "HPD"), maxdepth = 1, view = c("default", "32-bit", "64-bit"))readRegistry(key, hive= c("HLM","HCR","HCU","HU","HCC","HPD"), maxdepth=1, view= c("default","32-bit","64-bit"))
key | character string, the path to the key in the Windows Registry. |
hive | The ‘hive’ containing the key. The abbreviationsare for |
maxdepth | How far to recurse into the subkeys of the key. Bydefault only the values of the key and the names of subkeys arereturned. |
view | On 64-bit Windows, the view of the Registry to be used:see ‘Details’. |
Registry access is done using the security settings of the currentRsession: this means that some Registry keys may not be accessible evenif they exist. This may result inNULL values in the objectreturned, and, possibly, empty element names.
On 64-bit Windows, this will by default read the 32-bit view of theRegistry when run from 32-bitR, and the 64-bit view when run from64-bitR: seehttps://learn.microsoft.com/en-us/windows/win32/winprog64/registry-redirector.
A named list of values and subkeys (which may themselves be namedlists). The default value (if any) precedes named values whichprecede subkeys, and both the latter sets are sorted alphabetically.
This is only available on Windows.
if(.Platform$OS.type == "windows") withAutoprint({ ## only in HLM if set in an admin-mode install. try(readRegistry("SOFTWARE\\R-core", maxdepth = 3)) gmt <- file.path("SOFTWARE", "Microsoft", "Windows NT", "CurrentVersion", "Time Zones", "GMT Standard Time", fsep = "\\") readRegistry(gmt, "HLM")}) ## Not run: ## on a 64-bit R need this to find 32-bit JAGSreadRegistry("SOFTWARE\\JAGS", maxdepth = 3, view = "32")## See if there is a 64-bit user installreadRegistry("SOFTWARE\\R-core\\R64", "HCU", maxdepth = 2)## End(Not run)if(.Platform$OS.type=="windows") withAutoprint({## only in HLM if set in an admin-mode install. try(readRegistry("SOFTWARE\\R-core", maxdepth=3)) gmt<- file.path("SOFTWARE","Microsoft","Windows NT","CurrentVersion","Time Zones","GMT Standard Time", fsep="\\") readRegistry(gmt,"HLM")})## Not run: ## on a 64-bit R need this to find 32-bit JAGSreadRegistry("SOFTWARE\\JAGS", maxdepth=3, view="32")## See if there is a 64-bit user installreadRegistry("SOFTWARE\\R-core\\R64","HCU", maxdepth=2)## End(Not run)
This function allows the user to browse directly on any of thecurrently active function calls, and is suitable as an error option.The expressionoptions(error = recover) will make thisthe error option.
recover()recover()
When called,recover prints the list of current calls, andprompts the user to select one of them. The standardRbrowser is then invoked from the correspondingenvironment; the user can type ordinaryR language expressions to beevaluated in that environment.
When finished browsing in this call, typec to return torecover from the browser. Type another frame number to browsesome more, or type0 to exitrecover.
The use ofrecover largely supersedesdump.framesas an error option, unless you really want to wait to look at theerror. Ifrecover is called in non-interactive mode, itbehaves likedump.frames. For computations involving largeamounts of data,recover has the advantage that it does notneed to copy out all the environments in order to browse in them. Ifyou do decide to quit interactive debugging, calldump.frames directly while browsing in any frame (seethe examples).
Nothing useful is returned. However, youcan invokerecover directly from a function, rather than through the erroroption shown in the examples. In this case, execution continuesafter you type0 to exitrecover.
TheRrecover function can be used in the same way as theS function of the same name; therefore, the error option shown isa compatible way to specify the error action. However, the actualfunctions are essentially unrelated and interact quite differentlywith the user. The navigating commandsup anddown donot exist in theR version; instead, exit the browser and selectanother frame.
John M. Chambers (1998).Programming with Data; Springer.
See the compatibility note above, however.
browser for details about the interactive computations;options for setting the error option;dump.frames to save the current environments for laterdebugging.
## Not run: options(error = recover) # setting the error option### Example of interaction> myFit <- lm(y ~ x, data = xy, weights = w)Error in lm.wfit(x, y, w, offset = offset, ...) : missing or negative weights not allowedEnter a frame number, or 0 to exit1:lm(y ~ x, data = xy, weights = w)2:lm.wfit(x, y, w, offset = offset, ...)Selection: 2Called from: eval(expr, envir, enclos)Browse[1]> objects() # all the objects in this frame[1] "method" "n" "ny" "offset" "tol" "w"[7] "x" "y"Browse[1]> w[1] -0.5013844 1.3112515 0.2939348 -0.8983705 -0.1538642[6] -0.9772989 0.7888790 -0.1919154 -0.3026882Browse[1]> dump.frames() # save for offline debuggingBrowse[1]> c # exit the browserEnter a frame number, or 0 to exit1:lm(y ~ x, data = xy, weights = w)2:lm.wfit(x, y, w, offset = offset, ...)Selection: 0 # exit recover>## End(Not run)## Not run:options(error= recover)# setting the error option### Example of interaction> myFit<- lm(y~ x, data= xy, weights= w)Errorin lm.wfit(x, y, w, offset= offset,...): missing or negative weights not allowedEnter a frame number, or0 to exit1:lm(y~ x, data= xy, weights= w)2:lm.wfit(x, y, w, offset= offset,...)Selection:2Called from: eval(expr, envir, enclos)Browse[1]> objects()# all the objects in this frame[1]"method""n""ny""offset""tol""w"[7]"x""y"Browse[1]> w[1]-0.50138441.31125150.2939348-0.8983705-0.1538642[6]-0.97729890.7888790-0.1919154-0.3026882Browse[1]> dump.frames()# save for offline debuggingBrowse[1]> c# exit the browserEnter a frame number, or0 to exit1:lm(y~ x, data= xy, weights= w)2:lm.wfit(x, y, w, offset= offset,...)Selection:0# exit recover>## End(Not run)
relist() is an S3 generic function with a few methods in orderto allow easy inversion ofunlist(obj) when that is usedwith an objectobj of (S3) class"relistable".
relist(flesh, skeleton)## Default S3 method:relist(flesh, skeleton = attr(flesh, "skeleton"))## S3 method for class 'factor'relist(flesh, skeleton = attr(flesh, "skeleton"))## S3 method for class 'list'relist(flesh, skeleton = attr(flesh, "skeleton"))## S3 method for class 'matrix'relist(flesh, skeleton = attr(flesh, "skeleton"))as.relistable(x)is.relistable(x)## S3 method for class 'relistable'unlist(x, recursive = TRUE, use.names = TRUE)relist(flesh, skeleton)## Default S3 method:relist(flesh, skeleton= attr(flesh,"skeleton"))## S3 method for class 'factor'relist(flesh, skeleton= attr(flesh,"skeleton"))## S3 method for class 'list'relist(flesh, skeleton= attr(flesh,"skeleton"))## S3 method for class 'matrix'relist(flesh, skeleton= attr(flesh,"skeleton"))as.relistable(x)is.relistable(x)## S3 method for class 'relistable'unlist(x, recursive=TRUE, use.names=TRUE)
flesh | a vector to be relisted |
skeleton | a list, the structure of which determines the structureof the result |
x | anR object, typically a list (or vector). |
recursive | logical. Should unlisting be applied to listcomponents of |
use.names | logical. Should names be preserved? |
Some functions need many parameters, which are most easily represented incomplex structures, e.g., nested lists. Unfortunately, manymathematical functions inR, includingoptim andnlm can only operate on functions whose domain isa vector.R hasunlist() to convert nested listobjects into a vector representation.relist(), its methods andthe functionality mentioned here provide the inverse operation to convertvectors back to the convenient structural representation.This allows structured functions (such asoptim()) to have simplemathematical interfaces.
For example, a likelihood function for a multivariate normal model needs avariance-covariance matrix and a mean vector. It would be most convenient torepresent it as a list containing a vector and a matrix. A typical parametermight look like
list(mean = c(0, 1), vcov = cbind(c(1, 1), c(1, 0))).
However,optim cannot operate on functions that takelists as input; it only likes numeric vectors. The solution isconversion. Given a functionmvdnorm(x, mean, vcov, log = FALSE)which computes the required probability density, then
ipar <- list(mean = c(0, 1), vcov = c bind(c(1, 1), c(1, 0))) initial.param <- as.relistable(ipar) ll <- function(param.vector) { param <- relist(param.vector, skeleton = ipar) -sum(mvdnorm(x, mean = param$mean, vcov = param$vcov, log = TRUE)) } optim(unlist(initial.param), ll)relist takes two parameters: skeleton and flesh. Skeleton is a sampleobject that has the rightshape but the wrong content.fleshis a vector with the right content but the wrong shape. Invoking
relist(flesh, skeleton)
will put the content of flesh on the skeleton. You don't need to specifyskeleton explicitly if the skeleton is stored as an attribute inside flesh.In particular, if flesh was created from some object obj withunlist(as.relistable(obj))then the skeleton attribute is automatically set. (Note that thisdoes not apply to the example here, asoptim is creatinga new vector to pass toll and not itspar argument.)
As long asskeleton has the right shape, it should be an inverseofunlist. These equalities hold:
relist(unlist(x), x) == x unlist(relist(y, skeleton)) == y x <- as.relistable(x) relist(unlist(x)) == x
Note however that the relisted object might not beidentical to the skeleton because of implicit coercionsperformed during the unlisting step. All elements of the relistedobjects have the same type as the unlisted object.NULL valuesare coerced to empty vectors of that type.
an object of (S3) class"relistable" (and"list").
R Core, based on a code proposal by Andrew Clausen.
ipar <- list(mean = c(0, 1), vcov = cbind(c(1, 1), c(1, 0))) initial.param <- as.relistable(ipar) ul <- unlist(initial.param) relist(ul) stopifnot(identical(relist(ul), initial.param))ipar<- list(mean= c(0,1), vcov= cbind(c(1,1), c(1,0))) initial.param<- as.relistable(ipar) ul<- unlist(initial.param) relist(ul) stopifnot(identical(relist(ul), initial.param))
Utility for removing add-on packages.
R CMD REMOVE [options] [-l lib] pkgsR CMD REMOVE[options][-l lib] pkgs
pkgs | a space-separated list with the names of the packages tobe removed. |
lib | the path name of theR library tree to remove from. Maybe absolute or relative. Also accepted in the form ‘--library=lib’. |
options | further options for help or version. |
If used asR CMD REMOVE pkgs without explicitly specifyinglib, packages are removed from the library tree rooted at thefirst directory in the library path which would be used byR run inthe current environment.
To remove from the library treelib instead of the defaultone, useR CMD REMOVE -l libpkgs.
UseR CMD REMOVE --help for more usage information.
Some binary distributions ofR haveREMOVE in a separatebundle, e.g. anR-devel RPM.
Removes installed packages/bundles and updates index informationas necessary.
remove.packages(pkgs, lib)remove.packages(pkgs, lib)
pkgs | a character vector with the names of the packages to be removed. |
lib | a character vector giving the library directories to remove thepackages from. If missing, defaults to the first element in |
On Unix-alikes,REMOVE for a command line version;
install.packages for installing packages.
Whenoptions("keep.source") isTRUE, a copy of theoriginal source code to a function is stored with it. Similarly,parse() may keep formatted source for an expression.Such source reference attributes are removed from the object byremoveSource().
removeSource(fn)removeSource(fn)
fn | a |
This removes the"srcref" and related attributes, viarecursive cleaning ofbody(fn) in the case of a functionor the recursive language parts, otherwise.
A copy of thefn object with the source removed.
is.language about language objects.
srcref for a description of source reference records,deparse for a description of how functions are deparsed.
## to make this act independently of the global 'options()' setting:op <- options(keep.source = TRUE)fn <- function(x) { x + 1 # A comment, kept as part of the source}fnnames(attributes(fn)) # "srcref" (only)names(attributes(body(fn))) # "srcref" "srcfile" "wholeSrcref"f2 <- removeSource(fn)f2stopifnot(length(attributes(fn)) > 0, is.null(attributes(f2)), is.null(attributes(body(f2))))## Source attribute of parse()d expressions,## have {"srcref", "srcfile", "wholeSrcref"} :E <- parse(text ="a <- x^y # power") ; names(attributes(E ))E. <- removeSource(E) ; names(attributes(E.))stopifnot(length(attributes(E )) > 0, is.null(attributes(E.)))options(op) # reset to previous state## to make this act independently of the global 'options()' setting:op<- options(keep.source=TRUE)fn<-function(x){ x+1# A comment, kept as part of the source}fnnames(attributes(fn))# "srcref" (only)names(attributes(body(fn)))# "srcref" "srcfile" "wholeSrcref"f2<- removeSource(fn)f2stopifnot(length(attributes(fn))>0, is.null(attributes(f2)), is.null(attributes(body(f2))))## Source attribute of parse()d expressions,## have {"srcref", "srcfile", "wholeSrcref"} :E<- parse(text="a <- x^y # power"); names(attributes(E))E.<- removeSource(E); names(attributes(E.))stopifnot(length(attributes(E))>0, is.null(attributes(E.)))options(op)# reset to previous state
Returns the location of theR home directory, which is the root ofthe installedR tree.
R RHOMER RHOME
Simple manipulation of (a small set of) integer numbers as roman numerals.
as.roman(x).romansr1 + r2r1 <= r2max(r1)sum(r2)as.roman(x).romansr1+ r2r1<= r2max(r1)sum(r2)
x | a numeric or character vector of arabic or roman numerals. |
r1,r2 | a roman number vector, i.e., of |
as.roman creates objects of class"roman" which areinternally represented as integers, and have suitable methods forprinting, formatting, subsetting, coercion, etc, seemethods(class = "roman").
Arithmetic ("Arith"), Comparison ("Compare")and ("Logic"), i.e., all"Ops" groupoperations work as for regular numbers viaR's integer functionality.
Only numbers between 1 and 4999 are “well” representable as romannumbers, and hence others result inas.roman(NA).
.romans is the basic dictionary, a namedcharacter vector.
Wikipedia contributors (2024). Roman numerals.Wikipedia, The Free Encyclopedia.https://en.wikipedia.org/w/index.php?title=Roman_numerals&oldid=1188781837.Accessed February 22, 2024.
## First five roman 'numbers'.(y <- as.roman(1 : 5))## Middle one.y[3]## Current year as a roman number.(y <- as.roman(format(Sys.Date(), "%Y")))## Today, and 10, 20, 30, and 100 years ago ...y - 10*c(0:3,10)## mixture of arabic and roman numbers :as.roman(c(NA, 1:3, "", strrep("I", 1:7))) # + NA with a warning for the last.cc <- c(NA, 1:3, strrep("I", 0:6)) # "IIIIII" is historical (Wikipedia)(rc <- as.roman(cc)) # two NAs: 0 is not "roman"(ic <- as.integer(rc)) # works automatically [without an explicit method]rNA <- as.roman(NA)## simple consistency checks -- arithmetic when result is in {1,2,..,4999} :stopifnot(identical(rc, as.roman(rc)), # as.roman(.) is "idempotent" identical(rc + rc + (3*rc), rc*5), identical(ic, c(NA, 1:3, NA, 1:6)), identical(as.integer(5*rc), 5L*ic), identical(as.numeric(rc), as.numeric(ic)), identical(rc[1], rNA), identical(as.roman(0), rNA), identical(as.roman(NA_character_), rNA), identical(as.list(rc), as.list(ic)))## Non-Arithmetic 'Ops' :stopifnot(exprs = { # Comparisons : identical(ic <= (ii <- c(0:4,1:6)), rc <= ii) identical(ic == ii, rc == as.roman(ii)) # Logic [integers |-> logical] : identical(rc & TRUE , ic & TRUE) identical(rc & FALSE, ic & FALSE) identical(rc | FALSE, ic | FALSE) identical(rc | NA , ic | NA)})## 'Summary' group functions (and comparison):(rc. <- rc[!is.na(rc)])stopifnot(exprs = { identical(min(rc), as.roman(NA)) identical(min(rc, na.rm=TRUE), as.roman(min(ic, na.rm=TRUE))) identical(range(rc.), as.roman(range(as.integer(rc.)))) identical(sum (rc, na.rm=TRUE), as.roman("XXVII")) identical(format(prod(rc, na.rm=TRUE)), "MMMMCCCXX") format(prod(rc.)) == "MMMMCCCXX" identical(format(prod(rc[-11], na.rm=TRUE)), "DCCXX")})## First five roman 'numbers'.(y<- as.roman(1:5))## Middle one.y[3]## Current year as a roman number.(y<- as.roman(format(Sys.Date(),"%Y")))## Today, and 10, 20, 30, and 100 years ago ...y-10*c(0:3,10)## mixture of arabic and roman numbers :as.roman(c(NA,1:3,"", strrep("I",1:7)))# + NA with a warning for the last.cc<- c(NA,1:3, strrep("I",0:6))# "IIIIII" is historical (Wikipedia)(rc<- as.roman(cc))# two NAs: 0 is not "roman"(ic<- as.integer(rc))# works automatically [without an explicit method]rNA<- as.roman(NA)## simple consistency checks -- arithmetic when result is in {1,2,..,4999} :stopifnot(identical(rc, as.roman(rc)),# as.roman(.) is "idempotent" identical(rc+ rc+(3*rc), rc*5), identical(ic, c(NA,1:3,NA,1:6)), identical(as.integer(5*rc),5L*ic), identical(as.numeric(rc), as.numeric(ic)), identical(rc[1], rNA), identical(as.roman(0), rNA), identical(as.roman(NA_character_), rNA), identical(as.list(rc), as.list(ic)))## Non-Arithmetic 'Ops' :stopifnot(exprs={# Comparisons : identical(ic<=(ii<- c(0:4,1:6)), rc<= ii) identical(ic== ii, rc== as.roman(ii))# Logic [integers |-> logical] : identical(rc&TRUE, ic&TRUE) identical(rc&FALSE, ic&FALSE) identical(rc|FALSE, ic|FALSE) identical(rc|NA, ic|NA)})## 'Summary' group functions (and comparison):(rc.<- rc[!is.na(rc)])stopifnot(exprs={ identical(min(rc), as.roman(NA)) identical(min(rc, na.rm=TRUE), as.roman(min(ic, na.rm=TRUE))) identical(range(rc.), as.roman(range(as.integer(rc.)))) identical(sum(rc, na.rm=TRUE), as.roman("XXVII")) identical(format(prod(rc, na.rm=TRUE)),"MMMMCCCXX") format(prod(rc.))=="MMMMCCCXX" identical(format(prod(rc[-11], na.rm=TRUE)),"DCCXX")})
Enable or disable profiling of the execution ofR expressions.
Rprof(filename = "Rprof.out", append = FALSE, interval = 0.02, memory.profiling = FALSE, gc.profiling = FALSE, line.profiling = FALSE, filter.callframes = FALSE, numfiles = 100L, bufsize = 10000L, event = c("default", "cpu", "elapsed"))Rprof(filename="Rprof.out", append=FALSE, interval=0.02, memory.profiling=FALSE, gc.profiling=FALSE, line.profiling=FALSE, filter.callframes=FALSE, numfiles=100L, bufsize=10000L, event= c("default","cpu","elapsed"))
filename | The file to be used for recording the profiling results.Set to |
append | logical: should the file be over-written or appended to? |
interval | real: distance (time interval) between samples in seconds. |
memory.profiling | logical: write memory use information to the file? |
gc.profiling | logical: record whetherGC is running? |
line.profiling | logical: write line locations to the file? |
filter.callframes | logical: filter out intervening call framesof the call tree. See the filtering out call frames section. |
numfiles,bufsize | integers: line profiling memory allocation |
event | character: profiling event, character vector of length one, |
Enabling profiling automatically disables any existing profiling toanother or the same file.
Profiling works by writing out the call stack everyintervalseconds (units of the profiling event), to the file specified. Either thesummaryRprof function or the wrapper scriptR CMD Rprof can be used to process the output file to produce a summary of theusage; useR CMD Rprof --help for usage information.
Exactly what is measured is subtle and depends on the profiling event.
With"elapsed" (the default and only supported event on Windows): itis time that theR process is running and executing anR command. It isnot however just CPU time, for ifreadline() is waiting for input,that counts as well. It is also known as ‘elapsed’ time.
With"cpu" (the default on Unix and typically the preferred eventfor identifying performance bottlenecks), it is CPU time of theR process,so for example excludes time whenR is waiting for input or for processesrun bysystem to return. It may go slower than"elapsed" when the process is often waiting for I/O to finish, but itmay go faster with actively computing concurrent threads (say viaOpenMP)on a multi-core system.
Note that the (timing) interval cannot be too small. With"cpu",the time spent in each profiling step is currently added to the interval. With all profiling events, the computation in each profiling step causesperturbation to the observed system and biases the results. What isfeasible is machine-dependent. On Linux, R requires the interval to be atleast 10ms, on all other platforms at least 1ms. Shorter intervals willbe rounded up with a warning.
The"default" profiling event is"elapsed" on Windows and"cpu" on Unix.
Support for"elapsed" event on Unix is new and consideredexperimental. To reduce the risk of missing a sample, R tries to use the(real-time) FIFO scheduling policy with the maximum scheduling priorityfor an internal thread which initiates collection of each sample. Ifsetting that priority fails, it tries to use the maximum schedulingpriority of the current scheduling policy, falling back to the currentscheduling parameters. On Linux, regular users are typically not allowedto use the real-time scheduling priorities. This can be usually allowedvia PAM (e.g. ‘/etc/security/limits.conf’), see the OS documentationfor details. The priorities only matter when profiling a system underhigh load.
Functions will only be recorded in the profile log if they put acontext on the call stack (seesys.calls). Someprimitive functions do not do so: specifically those which areoftype"special" (see the ‘R Internals’ manualfor more details).
Individual statements will be recorded in the profile log ifline.profiling isTRUE, and if the code being executedwas parsed with source references. Seeparse for adiscussion of source references. By default the statement locationsare not shown insummaryRprof, but see that help pagefor options to enable the display.
Lazy evaluation makes the call stack more complex because interveningcall frames are created between the time arguments are applied to afunction, and the time they are effectively evaluated. When the callstack is represented as a tree, these intervening frames appear assibling nodes. For instance, evaluatingtry(EXPR) produces thefollowing call tree, at the timeEXPR gets evaluated:
1. +-base::try(EXPR)2. | \-base::tryCatch(...)3. | \-base:::tryCatchList(expr, classes, parentenv, handlers)4. | \-base:::tryCatchOne(expr, names, parentenv, handlers[[1L]])5. | \-base:::doTryCatch(return(expr), name, parentenv, handler)6. \-EXPR
Lines 2 to 5 are intervening call frames, the last of which finallytriggered evaluation ofEXPR. Settingfilter.callframestoTRUE simplifies the profiler output by removing all siblingnodes of intervening frames.
The same kind of call frame filtering is applied witheval()frames. When you calleval(), two frames are pushed on thestack to ensure a continuity between frames. Say we have thesedefinitions:
calling <- function() evaluator(quote(called()), environment())evaluator <- function(expr, env) eval(expr, env)called <- function() EXPR()
calling() callscalled() in its own environment, viaeval(). The latter is called indirectly throughevaluator(). The net effect of this code is identical to justcallingcalled() directly, without the intermediaries. However,the full call stack looks like this:
1. calling()2. \-evaluator(quote(called()), environment())3. \-base::eval(expr, env)4. \-base::eval(expr, env)5. \-called()6. \-EXPR()
When call frame filtering is turned on, the true calling environmentofcalled() is looked up, and the filtered call stack looks likethis:
1. calling()5. \-called()6. \-EXPR()
If the calling environment is not on the stack, the function called byeval() becomes a root node. Say we have:
calling <- function() evaluator(quote(called()), new.env())
With call frame filtering we then get the following filtered callstack:
5. called()6. \-EXPR()
Profiling is not available on all platforms. By default,support for profiling is compiled in if possible – configureR with--disable-R-profiling to change this.
AsR CPU profiling uses the same mechanisms as C profiling, the twocannot be used together, so do not useRprof(event = "cpu")(the default) in an executable built for C-level profiling (such asusing the GCC option-p or-pg).
filename can be a UTF-8-encoded filepath that cannot be translated tothe current locale.
The profiler interrupts R asynchronously, and it cannot allocatememory to store results as it runs. This affects line profiling,which needs to store an unknown number of file pathnames. Thenumfiles andbufsize arguments control the size ofpre-allocated buffers to hold these results: the former counts themaximum number of paths, the latter counts the numbers of bytes inthem. If the profiler runs out of space it will skip recording theline information for new files, and issue a warning whenRprof(NULL) is called to finish profiling.
The chapter on “Tidying and profiling R code” in‘Writing R Extensions’:RShowDoc("R-exts").
summaryRprof to analyse the output file.
tracemem,Rprofmem for other ways to trackmemory use.
## Not run: Rprof()## some code to be profiledRprof(NULL)## some code NOT to be profiledRprof(append = TRUE)## some code to be profiledRprof(NULL)## ...## Now post-process the output as described in Details## End(Not run)## Not run: Rprof()## some code to be profiledRprof(NULL)## some code NOT to be profiledRprof(append=TRUE)## some code to be profiledRprof(NULL)## ...## Now post-process the output as described in Details## End(Not run)
Enable or disable reporting of memory allocation in R.
Rprofmem(filename = "Rprofmem.out", append = FALSE, threshold = 0)Rprofmem(filename="Rprofmem.out", append=FALSE, threshold=0)
filename | The file to be used for recording the memoryallocations. Set to |
append | logical: should the file be over-written or appended to? |
threshold | numeric: allocations on R's "large vector" heaplarger than this number of bytes will be reported. |
Enabling profiling automatically disables any existing profiling toanother or the same file.
Profiling writes the call stack to the specified file every timemalloc is called to allocate a large vector object or toallocate a page of memory for small objects. The size of a page ofmemory and the size above whichmalloc is used for vectors arecompile-time constants, by default 2000 and 128 bytes respectively.
The profiler tracks allocations, some of which will be to previouslyused memory and will not increase the total memory use of R.
None
The memory profiler slows down R even when not in use, and so is acompile-time option.(It is enabled in a standard Windows build ofR.)
The memory profiler can be used at the same time as otherR and C profilers.
The R sampling profiler,Rprof also collectsmemory information.
tracemem traces duplications of specific objects.
The chapter on ‘Tidying and profiling R code’in the ‘Writing R Extensions’ manual.
## Not run: ## not supported unless R is compiled to support it.Rprofmem("Rprofmem.out", threshold = 1000)example(glm)Rprofmem(NULL)noquote(readLines("Rprofmem.out", n = 5))## End(Not run)## Not run:## not supported unless R is compiled to support it.Rprofmem("Rprofmem.out", threshold=1000)example(glm)Rprofmem(NULL)noquote(readLines("Rprofmem.out", n=5))## End(Not run)
This is an alternative front end for use in ‘#!’ scripts andother scripting applications.
Rscript [options] file [args]Rscript [options] -e expr [-e expr2 ...] [args]Rscript[options] file[args]Rscript[options]-e expr[-e expr2...][args]
options | a list of options, all beginning with ‘--’. Thesecan be any of the options of the standardR front-end, and also thosedescribed in the details. |
expr,expr2 | R expression(s), properly quoted. |
file | the name of a file containingR commands. ‘-’indicates ‘stdin’. |
args | arguments to be passed to the script in |
Rscript --help gives details of usage, andRscript --version gives the version ofRscript.
Other invocations invoke theR front-end with selected options. Thisfront-end is convenient for writing ‘#!’ scripts since it is anexecutable and takesfile directly as an argument. Options--no-echo --no-restore are always supplied: these imply--no-save. Arguments that contain spaces cannot be specifieddirectly on the ‘#!’ line, because spaces and tabs are interpreted asdelimiters and there is no way to protect them from this interpretation onthe ‘#!’ line. (The standard Windows command line has no conceptof ‘#!’ scripts, but Cygwin shells do.)
Either one or more-e options orfile shouldbe supplied. When using-e options be aware of the quotingrules in the shell used: see the examples.
The prescribed order of arguments is important: e.g.--verbosespecified after-e will be part ofargs and passed to theexpression; the same will happen to-e specified afterfile.
Additional options accepted as part ofoptions (beforefileor-e) are
gives details of whatRscript isdoing.
wherelist is acomma-separated list of package names orNULL. Sets theenvironment variableR_DEFAULT_PACKAGES which determines thepackages loaded on startup.
Spaces are allowed inexpr andfile (but will needto be protected from the shell in use, if any, for example byenclosing the argument in quotes).
If--default-packages is not used, thenRscriptchecks the environment variableR_SCRIPT_DEFAULT_PACKAGES. Ifthis is set, then it takes precedence overR_DEFAULT_PACKAGES.
Normally the version ofR is determined at installation, but this canbe overridden by setting the environment variableRHOME.
stdin() refers to the input file, andfile("stdin") to thestdin file stream of theprocess.
Rscript is only supported on systems with theexecvsystem call.
## Not run: Rscript -e 'date()' -e 'format(Sys.time(), "%a %b %d %X %Y")'# Get the same initial packages in the same order as default R:Rscript --default-packages=methods,datasets,utils,grDevices,graphics,stats -e 'sessionInfo()'## example #! script for a Unix-alike## (arguments given on the #! line end up as [options] to Rscript, while## arguments passed to the #! script end up as [args], so available to## commandArgs())#! /path/to/Rscript --vanilla --default-packages=utilsargs <- commandArgs(TRUE)res <- try(install.packages(args))if(inherits(res, "try-error")) q(status=1) else q()## End(Not run)## Not run:Rscript-e'date()'-e'format(Sys.time(), "%a %b %d %X %Y")'# Get the same initial packages in the same order as default R:Rscript--default-packages=methods,datasets,utils,grDevices,graphics,stats-e'sessionInfo()'## example #! script for a Unix-alike## (arguments given on the #! line end up as [options] to Rscript, while## arguments passed to the #! script end up as [args], so available to## commandArgs())#! /path/to/Rscript --vanilla --default-packages=utilsargs<- commandArgs(TRUE)res<- try(install.packages(args))if(inherits(res,"try-error")) q(status=1)else q()## End(Not run)
Utility function to find and displayR documentation.
RShowDoc(what, type = c("pdf", "html", "txt"), package)RShowDoc(what, type= c("pdf","html","txt"), package)
what | a character string: see ‘Details’. |
type | an optional character string giving the preferred format.Can be abbreviated. |
package | an optional character string specifying the name of apackage within which to look for documentation. |
what can specify one of several different sources of documentation,including theR manuals (R-admin,R-data,R-exts,R-intro,R-ints,R-lang),NEWS,COPYING (the GPL licence), any of the licenses in‘share/licenses’,FAQ (also available asR-FAQ), and the files in ‘R_HOME/doc’.
Only on Windows, theR for Windows FAQ is specified byrw-FAQ.
Ifpackage is supplied, documentation is looked for in the‘doc’ and top-level directories of an installed package of that name.
Ifwhat is missing a brief usage message is printed.
The documentation types are tried in turn starting with the firstspecified intype (or"pdf" if none is specified).
A invisible character string given the path to the file found.
For displaying regular help files,help (or?) andhelp.start.
Fortype = "txt",file.show is used.vignettes are nicely viewed viaRShowDoc(*, package= . ).
RShowDoc("R-lang")RShowDoc("FAQ", type = "html")RShowDoc("frame", package = "grid")RShowDoc("changes.txt", package = "grid")RShowDoc("NEWS", package = "MASS")RShowDoc("R-lang")RShowDoc("FAQ", type="html")RShowDoc("frame", package="grid")RShowDoc("changes.txt", package="grid")RShowDoc("NEWS", package="MASS")
Search for key words or phrases in various documentation, such asR manuals, help pages of base and CRAN packages, vignettes, task views and others, using the search engineathttps://search.r-project.org and view them in a web browser.
RSiteSearch(string, restrict = c("functions", "descriptions", "news", "Rfunctions", "Rmanuals", "READMEs", "views", "vignettes"), format, sortby = c("score", "date:late", "date:early", "subject", "subject:descending", "size", "size:descending"), matchesPerPage = 20, words = c("all", "any"))RSiteSearch(string, restrict= c("functions","descriptions","news","Rfunctions","Rmanuals","READMEs","views","vignettes"), format, sortby= c("score","date:late","date:early","subject","subject:descending","size","size:descending"), matchesPerPage=20, words= c("all","any"))
string | A character string specifying word(s) or phrase(s) tosearch. If the words are to be searched as one entity, enclose themeither in (escaped) quotes or in braces. |
restrict | A character vector, typically of length greater than one.Values can be abbreviated.Possible areas to search in: |
format | deprecated. |
sortby | character string (can be abbreviated) indicating how tosort the search results: |
matchesPerPage | How many items to show per page. |
words | Show results matching |
This function is designed to work with the search site athttps://search.r-project.org.
Unique partial matches will work for all arguments. Each newbrowser window will stay open unless you close it.
(Invisibly) the complete URL passed to the browser,including the query string.
Andy Liaw and Jonathan Baron and Gennadiy Starostin
help.search,help.start for local searches.
browseURL for how the help file is displayed.
# need Internet connection## for phrase searching you may use (escaped) double quotes or bracketsRSiteSearch("{logistic regression} \"glm object\"")RSiteSearch('"logistic regression"')## Search in vignettes and help files of R base packages## store the query string:fullquery <- RSiteSearch("lattice", restrict = c("vignettes","Rfunctions"))fullquery # a string of 112 characters# need Internet connection## for phrase searching you may use (escaped) double quotes or bracketsRSiteSearch("{logistic regression} \"glm object\"")RSiteSearch('"logistic regression"')## Search in vignettes and help files of R base packages## store the query string:fullquery<- RSiteSearch("lattice", restrict= c("vignettes","Rfunctions"))fullquery# a string of 112 characters
rtags provides etags-like indexing capabilities for R code,using R's own parser.
rtags(path = ".", pattern = "\\.[RrSs]$", recursive = FALSE, src = list.files(path = path, pattern = pattern, full.names = TRUE, recursive = recursive), keep.re = NULL, ofile = "", append = FALSE, verbose = getOption("verbose"), type = c("etags", "ctags"))rtags(path=".", pattern="\\.[RrSs]$", recursive=FALSE, src= list.files(path= path, pattern= pattern, full.names=TRUE, recursive= recursive), keep.re=NULL, ofile="", append=FALSE, verbose= getOption("verbose"), type= c("etags","ctags"))
path,pattern,recursive | Arguments passed on to |
src | A vector of file names to be indexed. |
keep.re | A regular expression further restricting |
ofile | Passed on to |
append | Logical, indicating whether the output should overwritean existing file, or append to it. |
verbose | Logical. If |
type | Character string specifying whether emacs style( |
Many text editors allow definitions of functions and other languageobjects to be quickly and easily located in source files through atagging utility. This functionality requires the relevant sourcefiles to be preprocessed, producing an index (or tag) file containingthe names and their corresponding locations. There are multiple tagfile formats, the most popular being the vi-style ctags format and theand emacs-style etags format. Tag files in these formats are usuallygenerated by thectags andetags utilities respectively.Unfortunately, these programs do not recognize R code syntax. They doallow tagging of arbitrary language files through regular expressions,but this too is insufficient.
Thertags function is intended to be a tagging utility for Rcode. It parses R code files (using R's parser) and produces tags inboth etags and ctags formats. The support for vi-style ctags isrudimentary, and was adapted from a patch by Neal Fultz; seePR#17214.
It may be more convenient to use the command-line wrapper scriptR CMD rtags.
Deepayan Sarkar
https://en.wikipedia.org/wiki/Ctags,https://www.gnu.org/software/emacs/manual/html_node/emacs/Tags-Tables.html
## Not run: rtags("/path/to/src/repository", pattern = "[.]*\\.[RrSs]$", keep.re = "/R/", verbose = TRUE, ofile = "TAGS", append = FALSE, recursive = TRUE)## End(Not run)## Not run:rtags("/path/to/src/repository", pattern="[.]*\\.[RrSs]$", keep.re="/R/", verbose=TRUE, ofile="TAGS", append=FALSE, recursive=TRUE)## End(Not run)
StangleA driver forStangle that extractsR code chunks.Notably allRtangleSetup() arguments may be used as argumentsin theStangle() call.
Rtangle()RtangleSetup(file, syntax, output = NULL, annotate = TRUE, split = FALSE, quiet = FALSE, drop.evalFALSE = FALSE, ...)Rtangle()RtangleSetup(file, syntax, output=NULL, annotate=TRUE, split=FALSE, quiet=FALSE, drop.evalFALSE=FALSE,...)
file | name of Sweave source file. See the description of thecorresponding argument of |
syntax | an object of class |
output | name of output file used unless |
annotate | a logical or |
split | split output into a file for each code chunk? |
quiet | logical to suppress all progress messages. |
drop.evalFALSE | logical; When false, as by default, all chunks withoption |
... | additional named arguments setting defaults for furtheroptions listed in ‘Supported Options’. |
Unlesssplit = TRUE, the default name of the output file isbasename(file) with an extension corresponding to the Sweavesyntax (e.g., ‘Rnw’, ‘Stex’) replaced by ‘R’. Filenames"stdout" and"stderr" are interpreted as theoutput and message connection respectively.
If splitting is selected (including by the options in the file), eachchunk is written to a separate file with extension the name of the‘engine’ (default ‘.R’).
Note that this driver does more than simply extract the code chunks verbatim,because chunks may re-use earlier chunks.
annotate)By defaultannotate = TRUE, the annotation is of one of the forms
###################################################### code chunk number 3: viewport######################################################################################################### code chunk number 18: grid.Rnw:647-648######################################################################################################### code chunk number 19: trellisdata (eval = FALSE)###################################################
using either the chunk label (if present, i.e., when specified in thesource) or the file name and line numbers.
annotate may be a function with formal arguments(options, chunk, output), e.g. to produce less dominant chunkannotations; seeRtangle()$runcode how it is called instead ofthe default.
Rtangle supports the following options for code chunks (thevalues in parentheses show the default values):
character string ("R"). Only chunks withengine equal to"R" or"S" are processed.
logical (TRUE). Ifkeep.source == TRUE the original source is copied to thefile. Otherwise, deparsed source is output.
logical (TRUE). IfFALSE, the code chunkis copied across but commented out.
Used ifsplit = TRUE. Seeprefix.string.
a character string, default is the name of thesource file (without extension). Used ifsplit = TRUE asthe prefix for the filename if the chunk has no label, or if ithas a label andprefix = TRUE. Note that this is used aspart of filenames, so needs to be portable.
logical (FALSE). Should the output beannotated with comments showing the line number of the first code lineof the chunk?
Friedrich Leisch and R-core.
‘Sweave User Manual’, a vignette intheutils package.
nmRnw <- "example-1.Rnw"exfile <- system.file("Sweave", nmRnw, package = "utils")## Create R source fileStangle(exfile)nmR <- sub("Rnw$", "R", nmRnw) # the (default) R output file nameif(interactive()) file.show("example-1.R")## Smaller R source file with custom annotation:my.Ann <- function(options, chunk, output) { cat("### chunk #", options$chunknr, ": ", if(!is.null(ol <- options$label)) ol else .RtangleCodeLabel(chunk), if(!options$eval) " (eval = FALSE)", "\n", file = output, sep = "")}Stangle(exfile, annotate = my.Ann)if(interactive()) file.show("example-1.R")Stangle(exfile, annotate = my.Ann, drop.evalFALSE=TRUE)if(interactive()) file.show("example-1.R")nmRnw<-"example-1.Rnw"exfile<- system.file("Sweave", nmRnw, package="utils")## Create R source fileStangle(exfile)nmR<- sub("Rnw$","R", nmRnw)# the (default) R output file nameif(interactive()) file.show("example-1.R")## Smaller R source file with custom annotation:my.Ann<-function(options, chunk, output){ cat("### chunk #", options$chunknr,": ",if(!is.null(ol<- options$label)) olelse .RtangleCodeLabel(chunk),if(!options$eval)" (eval = FALSE)","\n", file= output, sep="")}Stangle(exfile, annotate= my.Ann)if(interactive()) file.show("example-1.R")Stangle(exfile, annotate= my.Ann, drop.evalFALSE=TRUE)if(interactive()) file.show("example-1.R")
A driver forSweave that translatesR code chunks inLaTeX files by “running them”, i.e.,parse() andeval() each.
RweaveLatex()RweaveLatexSetup(file, syntax, output = NULL, quiet = FALSE, debug = FALSE, stylepath, ...)RweaveLatex()RweaveLatexSetup(file, syntax, output=NULL, quiet=FALSE, debug=FALSE, stylepath,...)
file | Name of Sweave source file. See the description of thecorresponding argument of |
syntax | An object of class |
output | Name of output file. The default is to remove extension‘.nw’, ‘.Rnw’ or ‘.Snw’ and to addextension ‘.tex’. Any directory paths in |
quiet | If |
debug | If |
stylepath | See ‘Details’. |
... | named values for the options listed in ‘SupportedOptions’. |
The LaTeX file generated needs to contain the line‘\usepackage{Sweave}’, and if this is not present in theSweave source file (possibly in a comment), it is inserted by theRweaveLatex driver as last line before the ‘\begin{document}’statement. Ifstylepath = TRUE, a hard-codedpath to the file ‘Sweave.sty’ in theR installation is set inplace ofSweave. The hard-coded path makes the LaTeX file lessportable, but avoids the problem of installing the current version of‘Sweave.sty’ to some place in your TeX input path. However, TeXmay not be able to process the hard-coded path if it contains spaces(as it often will under Windows) or TeX special characters.
The default forstylepath is now taken from the environmentvariableSWEAVE_STYLEPATH_DEFAULT, or isFALSE it that isunset or empty. If set, it should be exactlyTRUE orFALSE: any other values are taken asFALSE.
The simplest way for frequent Sweave users to ensure that‘Sweave.sty’ is in the TeX input path is to add‘R_HOME/share/texmf’ as a ‘texmf tree’ (‘rootdirectory’ in the parlance of the ‘MiKTeX settings’ utility).
By default, ‘Sweave.sty’ loads the ‘graphicx’ LaTeX packageand sets the width of all included graphics to:
‘\setkeys{Gin}{width=0.8\textwidth}’.
This setting (defined in the ‘graphicx’ package)affects the width size option passed to the‘\includegraphics{}’ directive for each plot file and in turnimpacts the scaling of your plot files as they will appear in yourfinal document.
Thus, for example, you may setwidth=3 in your figure chunk andthe generated graphics files will be set to 3 inches inwidth. However, the width of your graphic in your final document willbe set to ‘0.8\textwidth’ and the height dimension will bescaled accordingly. Fonts and symbols will be similarly scaled in thefinal document.
You can adjust the default value by including the‘\setkeys{Gin}{width=...}’ directive in your ‘.Rnw’ fileafter the ‘\begin{document}’ directive and changing thewidth option value as you prefer, using standard LaTeXmeasurement values.
If you wish to override this default behavior entirely, you can add a‘\usepackage[nogin]{Sweave}’ directive in your preamble. In thiscase, no size/scaling options will be passed to the‘\includegraphics{}’ directive and theheight andwidth options will determine both the runtime generated graphicfile sizes and the size of the graphics in your final document.
‘Sweave.sty’ also supports the ‘[nofontenc]’ option, whichskips the default inclusion of ‘\usepackage[T1]{fontenc}’for pdfTeX processing.
It also supports the ‘[inconsolata]’ option, to render monospacedtext ininconsolata, the font used by default forR helppages.
The use of fancy quotes (seesQuote) can cause problemswhen settingR output in non-UTF-8 locales (note that pdfTeX assumesUTF-8 by default since 2018). Either setoptions(useFancyQuotes = FALSE) or arrange that LaTeXis aware of the encoding used and ensure that typewriter fontscontaining directional quotes are used.
Some LaTeX graphics drivers do not include ‘.png’ or ‘.jpg’in the list of known extensions. To enable them, add something like‘\DeclareGraphicsExtensions{.png,.pdf,.jpg}’ to the preamble ofyour document or check the behavior of your graphics driver. Whenbothpdf andpng areTRUE both files will beproduced bySweave, and their order in the‘DeclareGraphicsExtensions’ list determines which will be used bypdflatex.
RweaveLatex supports the following options for code chunks (thevalues in parentheses show the default values). Character stringvalues should be quoted when passed fromSweave through... but not when used in the header of a code chunk.
character string ("R"). Only chunks withengine equal to"R" or"S" are processed.
logical (TRUE). IncludeR code in the outputfile?
logical (TRUE). When echoing, ifTRUE the original source is copied to the file. Otherwise,deparsed source is echoed.
logical (TRUE). IfFALSE, the code chunkis not evaluated, and hence no text nor graphical outputproduced.
character string ("verbatim"). If"verbatim", the output ofR commands is included in theverbatim-like ‘Soutput’ environment. If"tex", theoutput is taken to be already proper LaTeX markup and included asis. If"hide" then all output is completely suppressed(but the code executed during the weave). Values can be abbreviated.
logical (FALSE). IfTRUE, this forcesauto-printing of all expressions.
logical (TRUE). IfTRUE, visibility ofvalues emulates an interactiveR session: values of assignmentsare not printed, values of single objects are printed. IfFALSE, output comes only from explicitprintor similar statements.
logical (FALSE). IfTRUE, text outputis written to separate files for each code chunk.
character string ("true"). If"true", blank lines at the beginning and end of output areremoved. If"all", then all blank lines are removed fromthe output. If"false" then blank lines are retained.
A ‘blank line’ is one that is empty or includes onlywhitespace (spaces and tabs).
Note that blank lines in a code chunk will usually produce aprompt string rather than a blank line on output.
logical (TRUE). IfTRUE generatedfilenames of figures and output all have the common prefix givenby theprefix.string option: otherwise only unlabelledchunks use the prefix.
a character string, default is the name of thesource file (without extension). Note that this is used as partof filenames, so needs to be portable.
logical (TRUE), indicating whether inputstatements for text output (ifsplit = TRUE) and‘\includegraphics’ statements for figures should beauto-generated. Useinclude = FALSE if the output shouldappear in a different place than the code chunk (by placing theinput line manually).
logical (FALSE), indicating whether the codechunk produces graphical output. Note that only one figure percode chunk can be processed this way. The labels for figurechunks are used as part of the file names, so should preferably bealphanumeric.
logical (FALSE), indicating whether EPS figuresshould be generated. Ignored iffig = FALSE.
logical (TRUE), indicating whether PDF figuresshould be generated. Ignored iffig = FALSE.
passed topdf to set the version, encoding and compression (ornot). Defaults taken frompdf.options().
logical (FALSE), indicating whether PNG figuresshould be generated. Ignored iffig = FALSE.Only available in.
logical (FALSE), indicating whether JPEG figuresshould be generated. Ignored iffig = FALSE.Only available in.
character (NULL): see section‘Custom Graphics Devices’. Ignored iffig = FALSE.Only available in.
numeric (6), width of figures in inches. See‘Details’.
numeric (6), height of figures in inches. See‘Details’.
numeric (300), resolution in pixels per inch:used for PNG and JPEG graphics. Note that the default is a fairlyhigh value, appropriate for high-quality plots. Something like100 is a better choice for package vignettes.
logical (FALSE). Write a concordancefile to link the input line numbers to the output line numbers.
logical (FALSE).By default each figure chunk is run once, then re-run for eachselected type of graphics. That will open a default graphicsdevice for the first figure chunk and use that device for the firstevaluation of all subsequent chunks. If this option is true, thefigure chunk is run only for each selected type of graphics, forwhich a new graphics device is opened and then closed.
In addition, users can specify further options, either in the headerof an individual code section or in a ‘\SweaveOpts{}’ line inthe document. For unknown options, their type is set at first use.
If optiongrdevice is supplied for a code chunk with bothfig andeval true, the following call is made
get(options$grdevice, envir = .GlobalEnv)(name=, width=, height=, options)
which should open a graphics device. The chunk's code is thenevaluated anddev.off is called. Normally a function ofthe name given will have been defined earlier in the Sweave document, e.g.
<<results=hide>>=my.Swd <- function(name, width, height, ...) grDevices::png(filename = paste(name, "png", sep = "."), width = width, height = height, res = 100, units = "in", type = "quartz", bg = "transparent")@
Alternatively forR >= 3.4.0, if the function exists in a package(rather than the.GlobalEnv) it can be used by settinggrdevice = "pkg::my.Swd" (or with ‘:::’ instead of‘::’ if the function is not exported).
Currently only one custom device can be used for each chunk, butdifferent devices can be used for different chunks.
A replacement fordev.off can be provided as a functionwith suffix.off, e.g.my.Swd.off() orpkg::my.Swd.off(), respectively.
Before each code chunk is evaluated, zero or more hook functions canbe executed. IfgetOption("SweaveHooks") is set, it is takento be a named list of hook functions. For eachlogical option of acode chunk (echo,print, ...) a hook can bespecified, which is executed if and only if the respective option isTRUE. Hooks must be named elements of the list returned bygetOption("SweaveHooks") and be functions taking no arguments.E.g., if option"SweaveHooks" is defined aslist(fig = foo), andfoo is a function, then it would beexecuted before the code in each figure chunk. This is especiallyuseful to set defaults for the graphical parameters in a series offigure chunks.
Note that the user is free to define new Sweave logical options andassociate arbitrary hooks with them. E.g., one could define a hookfunction for a new option calledclean that removes all objectsin the workspace. Then all code chunks specified withclean = TRUE would start operating on an empty workspace.
Friedrich Leisch and R-core
‘Sweave User Manual’, a vignette intheutils package.
The file ‘Rconsole’ configures the R GUI (Rgui) consoleunder MS Windows andloadRconsole(*) loads a new configuration.
The file ‘Rdevga’ configures the graphics deviceswindows,win.graph,win.metafile andwin.print, as well as the bitmap devicesbmp,jpeg,png andtiff (which use fortype = "windows" usewindows internally).
loadRconsole(file)loadRconsole(file)
file | The file from which to load a new ‘Rconsole’ configuration.By default a file dialog is used to select a file. |
There are system copies of these files in‘R_HOME\etc’. Users can have personal copies ofthe files: these are looked for in the location given by theenvironment variableR_USER. The system files are read only if acorresponding personal file is not found.
If the environment variableR_USER is not set, theR systemsets it toHOME if that is set (stripping any trailing slash),otherwise to the Windows ‘personal’ directory,otherwise to{HOMEDRIVE}{HOMEPATH} ifHOMEDRIVE andHOMEDRIVE are both setotherwise to the working directory. This is as described in the file‘rw-FAQ’.
Each of the files contains details in its comments of how to set thevalues.
At the time of writing ‘Rdevga’ configured the mapping of fontnumbers to fonts, and ‘Rconsole’ configured the appearance(single or multiple document interface, toolbar, status bar on MDI),size, font and colours of the GUI console, and whether resizing theconsole setsoptions("width").
The file ‘Rconsole’ also configures the internal pager. Thisshares the font and colours of the console, but can be sizedseparately.
‘Rconsole’ can also set the initial positions of the console andthe graphics device, as well as the size and position of the MDIworkspace in MDI mode.
loadRconsole is called for its side effect of loading newdefaults. It returns no useful value.
Users of these languages will need to select a suitable font for theconsole (perhapsMS Mincho) and for the graphics device(although the defaultArial has many East Asian characters).It is essential that the font selected for the console hasdouble-width East Asian characters – many monospaced fonts do not.
TheGUI preferences item on theEdit menu brings up andialog box which can be used to edit the console settings, and to save themto a file.
This is only available on Windows.
Guido Masarotto and R-core members
if(.Platform$OS.type == "windows") withAutoprint({ ruser <- Sys.getenv("R_USER") cat("\n\nLocation for personal configuration files is\n R_USER = ", ruser, "\n\n", sep = "") ## see if there are personal configuration files file.exists(file.path(ruser, c("Rconsole", "Rdevga"))) ## show the configuration files used showConfig <- function(file) { ruser <- Sys.getenv("R_USER") path <- file.path(ruser, file) if(!file.exists(path)) path <- file.path(R.home(), "etc", file) file.show(path, header = path) } showConfig("Rconsole")})if(.Platform$OS.type=="windows") withAutoprint({ ruser<- Sys.getenv("R_USER") cat("\n\nLocation for personal configuration files is\n R_USER = ", ruser,"\n\n", sep="")## see if there are personal configuration files file.exists(file.path(ruser, c("Rconsole","Rdevga")))## show the configuration files used showConfig<-function(file){ ruser<- Sys.getenv("R_USER") path<- file.path(ruser, file)if(!file.exists(path)) path<- file.path(R.home(),"etc", file) file.show(path, header= path)} showConfig("Rconsole")})
Load or save or display the commands history.
loadhistory(file = ".Rhistory")savehistory(file = ".Rhistory")history(max.show = 25, reverse = FALSE, pattern, ...)timestamp(stamp = date(), prefix = "##------ ", suffix = " ------##", quiet = FALSE)loadhistory(file=".Rhistory")savehistory(file=".Rhistory")history(max.show=25, reverse=FALSE, pattern,...)timestamp(stamp= date(), prefix="##------ ", suffix=" ------##", quiet=FALSE)
file | The name of the file in which to save the history, orfrom which to load it. The path is relative to the currentworking directory. |
max.show | The maximum number of lines to show. |
reverse | logical. If true, the lines are shown in reverseorder. Note: this is not useful when there are continuation lines. |
pattern | A character string to be matched against the lines ofthe history. When supplied, onlyunique matching lines are shown. |
... | Arguments to be passed to |
stamp | A value or vector of values to be written into the history. |
prefix | A prefix to apply to each line. |
suffix | A suffix to apply to each line. |
quiet | If |
There are several history mechanisms available for the differentRconsoles, which work in similar but not identical ways. Notably,there are different implementations for Unix and Windows.
The functions described here work inRgui and interactiveRterm but not in batch use ofRterm nor inembedded/DCOM versions.
The functions described here work under thereadline command-line interface but may not otherwise (forexample, in batch use or in an embedded application). Note thatRcan be built withoutreadline.
R.app, the console on macOS, has a separate and largelyincompatible history mechanism, which by default uses a file‘.Rapp.history’ and saves up to 250 entries. These functions arenot currently implemented there.
The (readline on Unix-alikes) history mechanismis controlled by two environment variables:R_HISTSIZE controlsthe number of lines that are saved (default 512), andR_HISTFILE(default ‘.Rhistory’) sets the filename used for theloading/saving of history if requested at the beginning/end of asession (but not the default forloadhistory/savehistory). There is no limit on thenumber of lines of history retained during a session, so settingR_HISTSIZE to a large value has no penalty unless a large fileis actually generated.
These environment variables are read at the time of saving, so can bealtered within a session by the use ofSys.setenv.
On Unix-alikes:Note thatreadline history library saves files with permission0600, that is with read/write permission for the user and noteven read permission for any other account.
Thetimestamp function writes a timestamp (or other message)into the history and echos it to the console. On platforms that do notsupport a history mechanism only the console message is printed.
If you want to save the history at the end of (almost) everyinteractive session (even those in which you do not save theworkspace), you can put a call tosavehistory() in.Last. See the examples.
## Not run: ## Save the history in the home directory: note that it is not## (by default) read from there but from the current directory.Last <- function() if(interactive()) try(savehistory("~/.Rhistory"))## End(Not run)## Not run:## Save the history in the home directory: note that it is not## (by default) read from there but from the current directory.Last<-function()if(interactive()) try(savehistory("~/.Rhistory"))## End(Not run)
Select item(s) from a character vector.
select.list(choices, preselect = NULL, multiple = FALSE, title = NULL, graphics = getOption("menu.graphics"))select.list(choices, preselect=NULL, multiple=FALSE, title=NULL, graphics= getOption("menu.graphics"))
choices | a character vector of items. |
preselect | a character vector, or |
multiple | logical: can more than one item be selected? |
title | optional character string for window title, or |
graphics | logical: should a graphical widget be used? |
The normal default isgraphics = TRUE.
this brings up a modal dialog box with a (scrollable) listof items, which can be selected by the mouse. Ifmultiple istrue, further items can be selected or deselected by holding thecontrol key down whilst selecting, and shift-clicking can be used toselect ranges.
Normal termination is via the ‘OK’ button or by hitting Enter ordouble-clicking an item. Selection can be aborted via the‘Cancel’ button or pressing Escape.
this brings up a modal dialog boxwith a (scrollable) list of items, which can be selected by the mouse.
it will use a Tcl/Tk listboxwidget if possible.
Ifgraphics is FALSE or no graphical widget is available itdisplays a text list from which the user can choose by number(s). Themultiple = FALSE case usesmenu. Preselection isonly supported formultiple = TRUE, where it is indicated by a"+" preceding the item.
It is an error to useselect.list in a non-interactive session.
A character vector of selected items. Ifmultiple is false andno item was selected (orCancel was used),"" isreturned. Ifmultiple is true and no item was selected (orCancel was used) then a character vector of length 0 is returned.
menu,tk_select.list for a graphicalversion using Tcl/Tk.
## Not run: select.list(sort(.packages(all.available = TRUE)))## End(Not run)## Not run:select.list(sort(.packages(all.available=TRUE)))## End(Not run)
Get and report version information aboutR, the OS and attached orloaded packages.
Theprint() andtoLatex() methods (for a"sessionInfo" object) show the locale and timezone information bydefault, whenlocale ortzone are true.Thesystem.codepage is only shown when it is not empty, i.e., onlyon Windows,and if it differs fromcode.page, see below orl10n_info().
sessionInfo(package = NULL)## S3 method for class 'sessionInfo'print(x, locale = TRUE, tzone = locale, RNG = !identical(x$RNGkind, .RNGdefaults), ...)## S3 method for class 'sessionInfo'toLatex(object, locale = TRUE, tzone = locale, RNG = !identical(object$RNGkind, .RNGdefaults), ...)osVersionsessionInfo(package=NULL)## S3 method for class 'sessionInfo'print(x, locale=TRUE, tzone= locale, RNG=!identical(x$RNGkind, .RNGdefaults),...)## S3 method for class 'sessionInfo'toLatex(object, locale=TRUE, tzone= locale, RNG=!identical(object$RNGkind, .RNGdefaults),...)osVersion
package | a character vector naming installed packages, or |
x | an object of class |
object | an object of class |
locale | show locale, by default |
tzone | show time zone information? |
RNG | show information on |
... | currently not used. |
sessionInfo() returns an object of class"sessionInfo"which hasprint andtoLatex methods. This is a list with components
R.version | a list, the result of calling |
platform | a character string describing the platformR wasbuilt under. Where sub-architectures are in use this is of the form‘platform/sub-arch’: 32-bit builds have |
running | a character string (or possibly |
RNGkind | a character vector, the result of calling |
matprod | a character string, the result of calling |
BLAS | a character string, the result of calling |
LAPACK | a character string, the result of calling |
LA_version | a character string, the result of calling |
locale | a character string, the result of calling |
tzone | a character string, the result of calling |
tzcode_type | a character string indicating source(system/internal) of the date-time conversion and printing functions. |
basePkgs | a character vector of base packages which are attached. |
otherPkgs | (not always present): a named list of the results ofcalling |
loadedOnly | (not always present): a named list of the results ofcalling |
osVersionosVersion is a character string (or possiblyNULL onbizarre platforms) describing the OS and version which it is runningunder (as distinct from built under). This attempts to name a Linuxdistribution and give the OS name on an Apple Mac.
It is the same assessionInfo()$runningand created when loading theutils package.
Windows may report unexpected versions: see the help forwin.version.
How OSes identify themselves and their versions can be arcane: wherepossibleosVersion (and hencesessionInfo()$running) usesa human-readable form.
WhereR was compiled under macOS 10.x (as theCRAN Inteldistributions were prior toR 4.3.0) but running under ‘Big Sur’ or later, macOS reports itself as ‘10.16’ (whichR recognizes as ‘Big Sur ...’) and not ‘11’, ‘12’, ....
The information on ‘loaded’ packages and namespaces is thecurrent version installed at the location the package wasloaded from: it can be wrong if another process has been changingpackages during the session.
sI <- sessionInfo()sI# The same, showing the RNGkind, but not the locale : print(sI, RNG = TRUE, locale = FALSE)toLatex(sI, locale = FALSE) # shortest; possibly desirable at end of reportsI<- sessionInfo()sI# The same, showing the RNGkind, but not the locale : print(sI, RNG=TRUE, locale=FALSE)toLatex(sI, locale=FALSE)# shortest; possibly desirable at end of report
Interact with the user to choose the package repositories to be used.
setRepositories(graphics = getOption("menu.graphics"), ind = NULL, addURLs = character(), name = NULL)setRepositories(graphics= getOption("menu.graphics"), ind=NULL, addURLs= character(), name=NULL)
graphics | Logical. If true, use a graphical list: on Windows ormacOS GUI use a list box, and on a Unix-alike iftcltk and an Xserver are available, use Tk widget. Otherwise use a text |
ind |
|
name |
|
addURLs | A character vector of additional URLs: it is oftenhelpful to use a named vector. |
The default list of known repositories is stored in the file‘R_HOME/etc/repositories’.That file can be edited for a site, or a user can have a personal copyin the file pointed to by the environment variableR_REPOSITORIES, or if this is unset,NULL or does not exist,in ‘HOME/.R/repositories’, which will take precedence.
A Bioconductor mirror can be selected by settingoptions("BioC_mirror"), e.g. viachooseBioCmirror — the default value is‘"https://bioconductor.org"’.This version of R chooses Bioconductor version 3.21 by default, but that can be changed via the environment variableR_BIOC_VERSION.
The items that are preselected are those that are currently inoptions("repos") plus those marked as default in thelist of known repositories.
The list of repositories offered depends on the setting of option"pkgType" as some repositories only offer a subset of types(e.g., only source packages or not macOS binary packages).Further, for binary packages some repositories (notably R-Forge) onlyoffer packages for the current or recent versions ofR.(Type"both" is equivalent to"source".)
Repository ‘CRAN’ is treated specially: the value is taken fromthe current setting ofgetOption("repos") if this has anelement"CRAN": this ensures mirror selection is sticky.
This function requires theR session to be interactive unlessind orname is supplied. The latter overrides the formerif both are supplied and values are not case-sensitive. If any of thesupplied names does not match, an error is raised.
This function is invoked mainly for its side effect of updatingoptions("repos"). It returns (invisibly) the previousrepos options setting (as alist with componentrepos) orNULL if no changes were applied.
This doesnot set the list of repositories at startup: to doso setoptions(repos =) in a start up file (see help topicStartup) or via a customized ‘repositories’ file.
chooseCRANmirror,chooseBioCmirror,install.packages.
## Not run: setRepositories(addURLs = c(CRANxtras = "https://www.stats.ox.ac.uk/pub/RWin"))## End(Not run)oldrepos <- setRepositories(name = c("CRAN", "R-Forge"))getOption("repos")options(oldrepos) # restore## Not run:setRepositories(addURLs= c(CRANxtras="https://www.stats.ox.ac.uk/pub/RWin"))## End(Not run)oldrepos<- setRepositories(name= c("CRAN","R-Forge"))getOption("repos")options(oldrepos)# restore
Set or get the title of theR (i.e.RGui) window whichwill appear in the task bar, or set the status bar (if in use).
setWindowTitle(suffix, title = paste(getIdentification(), suffix))getWindowTitle()getIdentification()setStatusBar(text)setWindowTitle(suffix, title= paste(getIdentification(), suffix))getWindowTitle()getIdentification()setStatusBar(text)
suffix | a character string to form part of the title |
title | a character string forming the complete new title |
text | a character string of up to 255 characters, to bedisplayed in the status bar. |
setWindowTitle appendssuffix to the normal windowidentification (RGui,R Console orRterm). Usesuffix = "" to reset the title.
getWindowTitle gets the current title.
This sets the title of the frame in MDI mode, the title of the consoleforRGui --sdi, and the title of the window from which it waslaunched forRterm.It has no effect in embedded uses ofR.
getIdentification returns the normal window identification.
setStatusBar sets the text in the status bar of an MDIframe: if this is not currently shown it is selected and shown.
The first three functions return a length 1 character vector.
setWindowTitle returns the previous window title (invisibly).
getWindowTitle andgetIdentification return the currentwindow title and the normal window identification, respectively.
These functions are only available on Windows and only make sense whenusing theRgui. E.g., inRterm (and hence inESS)the title is not visible (but can be set and gotten), and in a version ofRStudio it has been"", invariably.
if(.Platform$OS.type == "windows") withAutoprint({## show the current working directory in the title, saving the old oneoldtitle <- setWindowTitle(getwd())Sys.sleep(0.5)## reset the titlesetWindowTitle("")Sys.sleep(0.5)## restore the original titlesetWindowTitle(title = oldtitle)})if(.Platform$OS.type=="windows") withAutoprint({## show the current working directory in the title, saving the old oneoldtitle<- setWindowTitle(getwd())Sys.sleep(0.5)## reset the titlesetWindowTitle("")Sys.sleep(0.5)## restore the original titlesetWindowTitle(title= oldtitle)})
Compile the given source files and then link all specified objectfiles into a shared object aka DLL which can be loaded intoR usingdyn.load orlibrary.dynam.
R CMD SHLIB [options] [-o dllname] filesR CMD SHLIB[options][-o dllname] files
files | a list specifying the object files to be included in theshared object/DLL. You can also include the name of source files (forwhich the object files are automagically made from their sources)and library linking commands. |
dllname | the full name of the shared object/DLL to be built,including the extension (typically ‘.so’ on Unix systems, and‘.dll’ on Windows). If not given, the basename of the object/DLLis taken from the basename of the first file. |
options | Further options to control the processing. Use |
R CMD SHLIB is the mechanism used byINSTALL tocompile source code in packages. It will generate suitablecompilation commands for C, C++, Objective C(++) and Fortran sources: Fortran90/95 sources can also be used but it may not be possible to mix thesewith other languages (on most platforms it is possible to mix with C,but mixing with C++ rarely works).
Please consult section ‘Creating shared objects’ in the manual‘Writing R Extensions’ for how to customize it (for example toaddcpp flags and to add libraries to the link step) and fordetails of some of its quirks.
Items infiles with extensions ‘.c’, ‘.cpp’,‘.cc’, ‘.C’, ‘.f’, ‘.f90’, ‘.f95’, ‘.m’(Objective-C), ‘.M’ and ‘.mm’ (Objective-C++) are regarded as sourcefiles, and those with extension ‘.o’ as object files. All otheritems are passed to the linker.
Objective C(++) support is optional whenR was configured: their mainusage is on macOS.
Note that the appropriate run-time libraries will be used when linkingif C++, Fortran or Objective C(++) sources are supplied, but not forcompiled object files from these languages.
Option-n (also known as--dry-run) will show thecommands that would be run without actually executing them.
Some binary distributions ofR haveSHLIB in a separatebundle, e.g., anR-devel RPM.
COMPILE,dyn.load,library.dynam.
The ‘R Installation and Administration’ and‘Writing R Extensions’ manuals, including the section on‘Customizing package compilation’ in the former.
## Not run: # To link against a library not on the system library paths:R CMD SHLIB -o mylib.so a.f b.f -L/opt/acml3.5.0/gnu64/lib -lacml## End(Not run)## Not run:# To link against a library not on the system library paths:R CMD SHLIB-o mylib.so a.f b.f-L/opt/acml3.5.0/gnu64/lib-lacml## End(Not run)
Convert file paths to the short form. This is an interface to theWindows API callGetShortPathNameW.
shortPathName(path)shortPathName(path)
path | character vector of file paths. |
For most file systems, the short form is the ‘DOS’ form with8+3 path components and no spaces, and this used to be guaranteed.But some file systems on recent versions of Windows do not have shortpath names when the long-name path will be returned instead.
A character vector. The path separator will be ‘\’. If afile path does not exist, the supplied path will be returned with slashesreplaced by backslashes.
This is only available on Windows.
if(.Platform$OS.type == "windows") withAutoprint({ cat(shortPathName(c(R.home(), tempdir())), sep = "\n")})if(.Platform$OS.type=="windows") withAutoprint({ cat(shortPathName(c(R.home(), tempdir())), sep="\n")})
These functions extract information from source references.
getSrcFilename(x, full.names = FALSE, unique = TRUE)getSrcDirectory(x, unique = TRUE)getSrcref(x)getSrcLocation(x, which = c("line", "column", "byte", "parse"), first = TRUE)getSrcFilename(x, full.names=FALSE, unique=TRUE)getSrcDirectory(x, unique=TRUE)getSrcref(x)getSrcLocation(x, which= c("line","column","byte","parse"), first=TRUE)
x | An object (typically a function) containing source references. |
full.names | Whether to include the full path in the filename result. |
unique | Whether to list only unique filenames/directories. |
which | Which part of a source reference to extract. Can be abbreviated. |
first | Whether to show the first (or last) location of the object. |
Each statement of a function will have its own source reference if the"keep.source" option isTRUE. These functions retrieveall of them.
The components are as follows:
The line number where the object starts or ends.
The column number where the object starts or ends.Horizontal tabs are converted to spaces.
As for"column", but counting bytes, which maydiffer in case of multibyte characters (and horizontal tabs).
As for"line", but this ignores#line directives.
getSrcFilename andgetSrcDirectory return character vectorsholding the filename/directory.
getSrcref returns a list of"srcref" objects orNULL if there are none.
getSrcLocation returns an integer vector of the requested typeof locations.
fn <- function(x) { x + 1 # A comment, kept as part of the source}# Show the temporary file directory# where the example was savedgetSrcDirectory(fn)getSrcLocation(fn, "line")fn<-function(x){ x+1# A comment, kept as part of the source}# Show the temporary file directory# where the example was savedgetSrcDirectory(fn)getSrcLocation(fn,"line")
Stacking vectors concatenates multiple vectors into a single vectoralong with a factor indicating where each observation originated.Unstacking reverses this operation.
stack(x, ...)## Default S3 method:stack(x, drop=FALSE, ...)## S3 method for class 'data.frame'stack(x, select, drop=FALSE, ...)unstack(x, ...)## Default S3 method:unstack(x, form, ...)## S3 method for class 'data.frame'unstack(x, form, ...)stack(x,...)## Default S3 method:stack(x, drop=FALSE,...)## S3 method for class 'data.frame'stack(x, select, drop=FALSE,...)unstack(x,...)## Default S3 method:unstack(x, form,...)## S3 method for class 'data.frame'unstack(x, form,...)
x | a list or data frame to be stacked or unstacked. |
select | an expression, indicating which variable(s) to select from adata frame. |
form | a two-sided formula whose left side evaluates to thevector to be unstacked and whose right side evaluates to theindicator of the groups to create. Defaults to |
drop | Whether to drop the unused levels from the “ind”column of the return value. |
... | further arguments passed to or from other methods. |
Thestack function is used to transform data available asseparate columns in a data frame or list into a single column that canbe used in an analysis of variance model or other linear model. Theunstack function reverses this operation.
Note thatstack applies tovectors (as determined byis.vector): non-vector columns (e.g., factors) will beignored with a warning. Where vectors of different types are selectedthey are concatenated byunlist whose help page explainshow the type of the result is chosen.
These functions are generic: the supplied methods handle data framesand objects coercible to lists byas.list.
unstack produces a list of columns according to the formulaform. If all the columns have the same length, the resultinglist is coerced to a data frame.
stack produces a data frame with two columns:
values | the result of concatenating the selected vectors in |
ind | a factor indicating from which vector in |
Douglas Bates
require(stats)formula(PlantGrowth) # check the default formulapg <- unstack(PlantGrowth) # unstack according to this formulapgstack(pg) # now put it back togetherstack(pg, select = -ctrl) # omitting one vectorrequire(stats)formula(PlantGrowth)# check the default formulapg<- unstack(PlantGrowth)# unstack according to this formulapgstack(pg)# now put it back togetherstack(pg, select=-ctrl)# omitting one vector
Compactly display the internalstructure of anR object, adiagnostic function and an alternative tosummary(and to some extent,dput). Ideally, only one line foreach ‘basic’ structure is displayed. It is especially well suitedto compactly display the (abbreviated) contents of (possibly nested)lists. The idea is to give reasonable output foranyRobject. It callsargs for (non-primitive) function objects.
strOptions() is a convenience function for settingoptions(str = .), see the examples.
str(object, ...)## S3 method for class 'data.frame'str(object, ...)## Default S3 method:str(object, max.level = NA, vec.len = strO$vec.len, digits.d = strO$digits.d, nchar.max = 128, give.attr = TRUE, drop.deparse.attr = strO$drop.deparse.attr, give.head = TRUE, give.length = give.head, width = getOption("width"), nest.lev = 0, indent.str = paste(rep.int(" ", max(0, nest.lev + 1)), collapse = ".."), comp.str = "$ ", no.list = FALSE, envir = baseenv(), strict.width = strO$strict.width, formatNum = strO$formatNum, list.len = strO$list.len, deparse.lines = strO$deparse.lines, ...)strOptions(strict.width = "no", digits.d = 3, vec.len = 4, list.len = 99, deparse.lines = NULL, drop.deparse.attr = TRUE, formatNum = function(x, ...) format(x, trim = TRUE, drop0trailing = TRUE, ...))str(object,...)## S3 method for class 'data.frame'str(object,...)## Default S3 method:str(object, max.level=NA, vec.len= strO$vec.len, digits.d= strO$digits.d, nchar.max=128, give.attr=TRUE, drop.deparse.attr= strO$drop.deparse.attr, give.head=TRUE, give.length= give.head, width= getOption("width"), nest.lev=0, indent.str= paste(rep.int(" ", max(0, nest.lev+1)), collapse=".."), comp.str="$ ", no.list=FALSE, envir= baseenv(), strict.width= strO$strict.width, formatNum= strO$formatNum, list.len= strO$list.len, deparse.lines= strO$deparse.lines,...)strOptions(strict.width="no", digits.d=3, vec.len=4, list.len=99, deparse.lines=NULL, drop.deparse.attr=TRUE, formatNum=function(x,...) format(x, trim=TRUE, drop0trailing=TRUE,...))
object | anyR object about which you want to have someinformation. |
max.level | maximal level of nesting which is applied fordisplaying nested structures, e.g., a list containing sub lists.Default NA: Display all nesting levels. |
vec.len | numeric (>= 0) indicating how many ‘first few’ elementsare displayed of each vector. The number is multiplied by differentfactors (from .5 to 3) depending on the kind of vector. Defaults tothe |
digits.d | number of digits for numerical components (as for |
nchar.max | maximal number of characters to show for |
give.attr | logical; if |
drop.deparse.attr | logical; if |
give.length | logical; if |
give.head | logical; if |
width | the page width to be used. The default is the currentlyactive |
nest.lev | current nesting level in the recursive calls to |
indent.str | the indentation string to use. |
comp.str | string to be used for separating list components. |
no.list | logical; if true, no ‘list of ...’ nor theclass are printed. |
envir | the environment to be used forpromise (see |
strict.width | string indicating if the |
formatNum | a function such as |
list.len | numeric; maximum number of list elements to displaywithin a level. |
deparse.lines | numeric or |
... | potential further arguments (required for Method/Genericreasons). |
str does not return anything, for efficiency reasons.The obvious side effect is output to the terminal.
See the extensive annotated ‘Examples’ below.
The default method tries to “work always”, but needs to make someassumptions for the case whenobject has aclass butno ownstr() method which isthe typical case: There itrelies on"[" and"[[" subsetting methods tobe compatible withlength(). When this is not the case, orwhenis.list(object) isTRUE, butlength(object)differs fromlength(unclass(object)) it treats it as“irregular” and reports the contents ofunclass(object)as “hidden list”.
Martin Maechler[email protected] since 1990.
ls.str forlisting objects with their structure;summary,args.
require(stats); require(grDevices); require(graphics)## The following examples show some of 'str' capabilitiesstr(1:12)str(ls)str(args) #- more useful than args(args) !str(freeny)str(str)str(.Machine, digits.d = 20) # extra digits for identification of binary numbersstr( lsfit(1:9, 1:9))str( lsfit(1:9, 1:9), max.level = 1)str( lsfit(1:9, 1:9), width = 60, strict.width = "cut")str( lsfit(1:9, 1:9), width = 60, strict.width = "wrap")op <- options(); str(op) # save first; # otherwise internal options() is used.need.dev <- !exists(".Device") || is.null(.Device) || .Device == "null device"{ if(need.dev) pdf() str(par()) if(need.dev) graphics.off()}ch <- letters[1:12]; is.na(ch) <- 3:5str(ch) # character NA'sstr(list(a = "A", L = as.list(1:100)), list.len = 9)## ------------## " .. [list output truncated] "## Long strings, 'nchar.max'; 'strict.width' :nchar(longch <- paste(rep(letters,100), collapse = ""))str(longch)str(longch, nchar.max = 52)str(longch, strict.width = "wrap")## Multibyte characters in strings:## Truncation behavior (<-> correct width measurement) for "long" non-ASCII:idx <- c(65313:65338, 65345:65350)fwch <- intToUtf8(idx) # full width character string: each has width 2ch <- strtrim(paste(LETTERS, collapse="._"), 64)(ncc <- c(c.ch = nchar(ch), w.ch = nchar(ch, "w"), c.fw = nchar(fwch), w.fw = nchar(fwch, "w")))stopifnot(unname(ncc) == c(64,64, 32, 64))## nchar.max: 1st line needs an increase of 2 in order to see 1 (in UTF-8!):invisible(lapply(60:66, function(N) str(fwch, nchar.max = N)))invisible(lapply(60:66, function(N) str( ch , nchar.max = N))) # "1 is 1" here## Settings for narrow transcript :op <- options(width = 60, str = strOptions(strict.width = "wrap"))str(lsfit(1:9,1:9))str(options())## reset to previous:options(op)str(quote( { A+B; list(C, D) } ))## S4 classes :require(stats4)x <- 0:10; y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)ll <- function(ymax = 15, xh = 6) -sum(dpois(y, lambda=ymax/(1+x/xh), log=TRUE))fit <- mle(ll)str(fit)require(stats); require(grDevices); require(graphics)## The following examples show some of 'str' capabilitiesstr(1:12)str(ls)str(args)#- more useful than args(args) !str(freeny)str(str)str(.Machine, digits.d=20)# extra digits for identification of binary numbersstr( lsfit(1:9,1:9))str( lsfit(1:9,1:9), max.level=1)str( lsfit(1:9,1:9), width=60, strict.width="cut")str( lsfit(1:9,1:9), width=60, strict.width="wrap")op<- options(); str(op)# save first;# otherwise internal options() is used.need.dev<-!exists(".Device")|| is.null(.Device)|| .Device=="null device"{if(need.dev) pdf() str(par())if(need.dev) graphics.off()}ch<- letters[1:12]; is.na(ch)<-3:5str(ch)# character NA'sstr(list(a="A", L= as.list(1:100)), list.len=9)## ------------## " .. [list output truncated] "## Long strings, 'nchar.max'; 'strict.width' :nchar(longch<- paste(rep(letters,100), collapse=""))str(longch)str(longch, nchar.max=52)str(longch, strict.width="wrap")## Multibyte characters in strings:## Truncation behavior (<-> correct width measurement) for "long" non-ASCII:idx<- c(65313:65338,65345:65350)fwch<- intToUtf8(idx)# full width character string: each has width 2ch<- strtrim(paste(LETTERS, collapse="._"),64)(ncc<- c(c.ch= nchar(ch), w.ch= nchar(ch,"w"), c.fw= nchar(fwch), w.fw= nchar(fwch,"w")))stopifnot(unname(ncc)== c(64,64,32,64))## nchar.max: 1st line needs an increase of 2 in order to see 1 (in UTF-8!):invisible(lapply(60:66,function(N) str(fwch, nchar.max= N)))invisible(lapply(60:66,function(N) str( ch, nchar.max= N)))# "1 is 1" here## Settings for narrow transcript :op<- options(width=60, str= strOptions(strict.width="wrap"))str(lsfit(1:9,1:9))str(options())## reset to previous:options(op)str(quote({ A+B; list(C, D)}))## S4 classes :require(stats4)x<-0:10; y<- c(26,17,13,12,20,5,9,8,5,4,8)ll<-function(ymax=15, xh=6)-sum(dpois(y, lambda=ymax/(1+x/xh), log=TRUE))fit<- mle(ll)str(fit)
Given a character vector and a regular expression containing captureexpressions,strcapture will extract the captured tokens into atabular data structure, such as a data.frame, the type and structure ofwhich is specified by a prototype object. The assumption is that thesame number of tokens are captured from every input string.
strcapture(pattern, x, proto, perl = FALSE, useBytes = FALSE)strcapture(pattern, x, proto, perl=FALSE, useBytes=FALSE)
pattern | The regular expression with the capture expressions. |
x | A character vector in which to capture the tokens. |
proto | A |
perl,useBytes | Arguments passed to |
Theproto argument is typically adata.frame, with acolumn corresponding to each capture expression, in order. Thecaptured character vector is coerced to the type of the column, andthe column names are carried over to the return value. Any data in theprototype are ignored. See the examples.
A tabular data structure of the same type asproto, sotypically adata.frame, containing a column for each captureexpression. The column types and names are inherited fromproto. Cases inx that do not matchpattern haveNA in every column.
regexec andregmatches for relatedlow-level utilities.
x <- "chr1:1-1000"pattern <- "(.*?):([[:digit:]]+)-([[:digit:]]+)"proto <- data.frame(chr=character(), start=integer(), end=integer())strcapture(pattern, x, proto)x<-"chr1:1-1000"pattern<-"(.*?):([[:digit:]]+)-([[:digit:]]+)"proto<- data.frame(chr=character(), start=integer(), end=integer())strcapture(pattern, x, proto)
Summarise the output of theRprof function to show theamount of time used by differentR functions.
summaryRprof(filename = "Rprof.out", chunksize = 5000, memory = c("none", "both", "tseries", "stats"), lines = c("hide", "show", "both"), index = 2, diff = TRUE, exclude = NULL, basenames = 1)summaryRprof(filename="Rprof.out", chunksize=5000, memory= c("none","both","tseries","stats"), lines= c("hide","show","both"), index=2, diff=TRUE, exclude=NULL, basenames=1)
filename | Name of a file produced by |
chunksize | Number of lines to read at a time. |
memory | Summaries for memory information. See ‘Memory profiling’ below. Can be abbreviated. |
lines | Summaries for line information. See ‘Line profiling’ below. Can be abbreviated. |
index | How to summarize the stack trace for memoryinformation. See ‘Details’ below. |
diff | If |
exclude | Functions to exclude when summarizing the stack tracefor memory summaries. |
basenames | Number of components of the path to filenames to display. |
This function provides the analysis code forRprof filesused byR CMD Rprof.
As the profiling output file could be larger than available memory, itis read in blocks ofchunksize lines. Increasingchunksizewill make the function run faster if sufficient memory is available.
Ifmemory = "none" andlines = "hide", a list with components
by.self | A data frame of timings sorted by ‘self’ time. |
by.total | A data frame of timings sorted by ‘total’ time. |
sample.interval | The sampling interval. |
sampling.time | Total time of profiling run. |
The first two components have columns ‘self.time’,‘self.pct’, ‘total.time’ and ‘total.pct’, the times inseconds and percentages of the total time spent executing code in thatfunction and code in that function or called from that function,respectively.
Iflines = "show", an additional component is added to the list:
by.line | A data frame of timings sorted by source location. |
Ifmemory = "both" the same list but with memory consumption in Mbin addition to the timings.
Ifmemory = "tseries" a data frame giving memory statistics overtime. Memory usage is in bytes.
Ifmemory = "stats" aby object giving memory statisticsby function. Memory usage is in bytes.
If no events were recorded, a zero-row data frame is returned.
Options other thanmemory = "none" apply only to files producedbyRprof(memory.profiling = TRUE).
When called withmemory.profiling = TRUE, the profiler writesinformation on three aspects of memory use: vector memory in smallblocks on the R heap, vector memory in large blocks (frommalloc), memory in nodes on the R heap. It also records the number ofcalls to the internal functionduplicate in the timeinterval.duplicate is called by C code when arguments need to becopied. Note that the profiler does not track which function actuallyallocated the memory.
Withmemory = "both" the change in total memory (truncated at zero)is reported in addition to timing data.
Withmemory = "tseries" ormemory = "stats" theindexargument specifies how to summarize the stack trace. A positive numberspecifies that many calls from the bottom of the stack; a negativenumber specifies the number of calls from the top of the stack. Withmemory = "tseries" the index is used to construct labels and may bea vector to give multiple sets of labels. Withmemory = "stats" theindex must be a single number and specifies how to aggregate the data tothe maximum and average of the memory statistics. With bothmemory = "tseries" andmemory = "stats" the argumentdiff = TRUE asks for summaries of the increase in memory use overthe sampling interval anddiff = FALSE asks for the memory use atthe end of the interval.
If the code being run has source reference information retained (viakeep.source = TRUE insource orKeepSource = TRUE in a package ‘DESCRIPTION’ file orsome other way), then information about the origin of lines isrecorded during profiling. By default this is not displayed, butthelines parameter can enable the display.
Iflines = "show", line locations will be used in preferenceto the usual function name information, and the resultswill be displayed ordered by location in addition to the other orderings.
Iflines = "both", line locations will be mixed with functionnames in a combined display.
The chapter on ‘Tidying and profiling R code’ in‘Writing R Extensions’:RShowDoc("R-exts").
tracemem traces copying of an object via the C functionduplicate.
Rprofmem is a non-sampling memory-use profiler.
https://developer.r-project.org/memory-profiling.html
## Not run: ## Rprof() is not available on all platformsRprof(tmp <- tempfile())example(glm)Rprof()summaryRprof(tmp)unlink(tmp)## End(Not run)## Not run:## Rprof() is not available on all platformsRprof(tmp<- tempfile())example(glm)Rprof()summaryRprof(tmp)unlink(tmp)## End(Not run)
Sweave provides a flexible framework for mixing text and R/S codefor automatic report generation. The basic idea is to replace thecode with its output, such that the final document only contains thetext and the output of the statistical analysis: however, the sourcecode can also be included.
Sweave(file, driver = RweaveLatex(), syntax = getOption("SweaveSyntax"), encoding = "", ...)Stangle(file, driver = Rtangle(), syntax = getOption("SweaveSyntax"), encoding = "", ...)Sweave(file, driver= RweaveLatex(), syntax= getOption("SweaveSyntax"), encoding="",...)Stangle(file, driver= Rtangle(), syntax= getOption("SweaveSyntax"), encoding="",...)
file | Path to Sweave source file. Note that this can besupplied without the extension, but the function will only proceedif there is exactly one Sweave file in the directory whosebasename matches |
driver | the actual workhorse, (a function returning) a named |
syntax |
|
encoding | The default encoding to assume for |
... | further arguments passed to the driver's setup function.See |
An Sweave source file contains both text in a markup language (likeLaTeX) andR (or S) code. The code gets replaced by its output (text orgraphs) in the final markup file. This allows a report to be re-generatedif the input data change and documents the code to reproduce theanalysis in the same file that also produces the report.
Sweave combines the documentation and code chunks (ortheir output) into a single document.Stangle extracts onlythe code from the Sweave file creating anR source file that can berun usingsource. (Code inside\Sexpr{}statements is ignored byStangle.)
Stangle is just a wrapper toSweave specifying adifferent default driver. Alternative drivers can be used and areprovided by various contributed packages.
Environment variableSWEAVE_OPTIONS can be used to override theinitial options set by the driver: it should be a comma-separated setofkey=value items, as would be used in a ‘\SweaveOpts’statement in a document.
If theencoding is unspecified (the default),non-ASCII source files must contain a line of the form
\usepackage[foo]{inputenc}(where ‘foo’ is typically ‘latin1’, ‘latin2’, ‘utf8’ or‘cp1252’ or ‘cp1250’) or a comment line
%\SweaveUTF8
to declare UTF-8 input (the default encoding assumed by pdfTeX since 2018),or they will give an error.Re-encoding can be turned off completely with argumentencoding = "bytes".
Sweave allows a flexible syntax framework for markingdocumentation and text chunks. The default is a noweb-style syntax, asalternative a LaTeX-style syntax can be used. (See the user manual forfurther details.)
Ifsyntax = NULL (the default) then the available syntaxobjects are consulted in turn, and selected if theirextensioncomponent matches (as a regexp) the file name. ObjectsSweaveSyntaxNoweb (withextension = "[.][rsRS]nw$") andSweaveSyntaxLatex (withextension = "[.][rsRS]tex$") aresupplied, but users or packages can supply others with names matchingthe patternSweaveSyntax.*.
Friedrich Leisch and R-core.
Friedrich Leisch (2002)Dynamic generation of statistical reports using literate data analysis.In W. Härdle and B. Rönz, editors,Compstat 2002 - Proceedings in Computational Statistics,pages 575–580. Physika Verlag, Heidelberg, Germany, ISBN 3-7908-1517-9.
‘Sweave User Manual’, a vignette intheutils package.
RweaveLatex,Rtangle.Alternative Sweave drivers are in, for example, packagesweaver (Bioconductor),R2HTML, andascii.
tools::buildVignette to process source filesusing Sweave or alternative vignette processing engines.
testfile <- system.file("Sweave", "Sweave-test-1.Rnw", package = "utils")## enforce par(ask = FALSE)options(device.ask.default = FALSE)## create a LaTeX file - in the current working directory, getwd():Sweave(testfile)## This can be compiled to PDF by## tools::texi2pdf("Sweave-test-1.tex")## or outside R by#### R CMD texi2pdf Sweave-test-1.tex## on Unix-alikes which sets the appropriate TEXINPUTS path.#### On Windows,## Rcmd texify --pdf Sweave-test-1.tex## if MiKTeX is available.## create an R source file from the code chunksStangle(testfile)## which can be sourced, e.g.source("Sweave-test-1.R")testfile<- system.file("Sweave","Sweave-test-1.Rnw", package="utils")## enforce par(ask = FALSE)options(device.ask.default=FALSE)## create a LaTeX file - in the current working directory, getwd():Sweave(testfile)## This can be compiled to PDF by## tools::texi2pdf("Sweave-test-1.tex")## or outside R by#### R CMD texi2pdf Sweave-test-1.tex## on Unix-alikes which sets the appropriate TEXINPUTS path.#### On Windows,## Rcmd texify --pdf Sweave-test-1.tex## if MiKTeX is available.## create an R source file from the code chunksStangle(testfile)## which can be sourced, e.g.source("Sweave-test-1.R")
This function converts the syntax of files inSweaveformat to another Sweave syntax definition.
SweaveSyntConv(file, syntax, output = NULL)SweaveSyntConv(file, syntax, output=NULL)
file | Name of Sweave source file. |
syntax | An object of class |
output | Name of output file, default is to remove the extensionfrom the input file and to add the default extension of the targetsyntax. Any directory names in |
Friedrich Leisch
‘Sweave User Manual’, a vignette intheutils package.
testfile <- system.file("Sweave", "Sweave-test-1.Rnw", package = "utils")## convert the file to latex syntaxSweaveSyntConv(testfile, SweaveSyntaxLatex)## and run it through SweaveSweave("Sweave-test-1.Stex")testfile<- system.file("Sweave","Sweave-test-1.Rnw", package="utils")## convert the file to latex syntaxSweaveSyntConv(testfile, SweaveSyntaxLatex)## and run it through SweaveSweave("Sweave-test-1.Stex")
Create a tar archive.
tar(tarfile, files = NULL, compression = c("none", "gzip", "bzip2", "xz", "zstd"), compression_level = 6, tar = Sys.getenv("tar"), extra_flags = "")tar(tarfile, files=NULL, compression= c("none","gzip","bzip2","xz","zstd"), compression_level=6, tar= Sys.getenv("tar"), extra_flags="")
tarfile | The pathname of the tar file: tilde expansion (see |
files | A character vector of filepaths to be archived:the default is to archive all files under the current directory. |
compression | character string giving the type of compression tobe used (default none). Can be abbreviated. |
compression_level | integer: the level of compression. Only usedfor the internal method: see the help for |
tar | character string: the path to the command to be used. Ifthe command itself contains spaces it needs to be quoted (e.g., by |
extra_flags | Any extra flags for an external |
This is either a wrapper for atar command or uses aninternal implementation inR. The latter is used iftarfileis a connection or if the argumenttar is"internal" or"" (the ‘factory-fresh’ default). Note that whereasUnix-alike versions ofR set the environment variableTAR, itsvalue is not the default for this function.
Argumentextra_flags is passed to an externaltar andso is platform-dependent. Possibly useful values include-h(follow symbolic links, also-L on some platforms),‘--acls’,--exclude-backups,--exclude-vcs (andsimilar) and on Windows--force-local (so drives can beincluded in filepaths: this used to be the default forR on Windows).
A convenient and robust way to set options for GNUtar is viaenvironment variableTAR_OPTIONS. Appending--force-localtoTAR does not work with GNUtar due to restrictions onhow some options can be mixed. Thetar available on Windows 10(libarchive'sbsdtar) supports drive letters by default. Itdoes not support the--force-local, but ignoresTAR_OPTIONS.
For GNUtar,--format=ustar forces a more portable format. (The default isset at compilation and will be shown at the end of the output fromtar --help: for version 1.35 ‘out-of-the-box’ it is--format=gnu, but the manual says the intention is to changeto--format=posix which is the same aspax –it was never part of the POSIX standard fortar and shouldnot be used. However, the intention has been stated now for severalyears without changing the default.)For libarchive'sbsdtar,--format=ustar is moreportable than the default.
One issue which can cause an external command to fail is a commandline too long for the system shell: this is workedaround if the external command is detected to be GNUtar orlibarchivetar (akabsdtar).
Note thatfiles = '.' will usually not work with an externaltar as that would expand the list of files aftertarfile is created. (It does work with the default internalmethod.)
The return code fromsystem or0 for the internalversion, invisibly.
The ‘tar’ format no longer has an agreed standard!‘Unix Standard Tar’ was part of POSIX 1003.1:1998 but has beenremoved in favour ofpax, and in any case many commonimplementations diverged from the former standard.
ManyR platforms use a version of GNUtar, but thebehaviour seems to be changed with each version. macOS >= 10.6,FreeBSD and Windows 10 usebsdtar from the libarchiveproject (but for macOS often a quite-old version), and commercialUnixes will have their own versions.bsdtar is available formany other platforms: macOS up to at least 10.9 (but not recently) hadGNUtar asgnutar and other platforms,e.g. Solaris, have it asgtar: on a Unix-alikeconfigure will trygnutar andgtarbeforetar.
Known problems arise from
The handling of file paths of more than 100 bytes. These wereunsupported in early versions oftar, and supported in oneway by POSIXtar and in another by GNUtar andyet another by the POSIXpax command whichrecenttar programs often support. The internalimplementation warns on paths of more than 100 bytes,uses the ‘ustar’ way from the 1998 POSIXstandard which supports up to 256 bytes (depending on the path: inparticular the final component is limited to 100 bytes) if possible,otherwise the GNU way (which is widely supported, including byuntar).
Most formats do not record the encoding of file paths.
(File) links.tar was developed on an OS that usedhard links, and physical files that were referred to more than oncein the list of files to be included were included only once, theremaining instances being added as links. Later a means to includesymbolic links was added. The internal implementation supportssymbolic links (on OSes that support them), only. Of course, thequestion arises as to how links should be unpacked on OSes that donot support them: for regular files file copies can be used.
Names of links in the ‘ustar’ format are restricted to 100bytes. There is an GNU extension for arbitrarily long link names,butbsdtar ignores it. The internal method uses theGNU extension, with a warning.
Header fields, in particular the padding to be used whenfields are not full or not used. POSIX did define the correctbehaviour but commonly used implementations did (and still do)not comply.
File sizes. The ‘ustar’ format is restricted to 8GBper (uncompressed) file.
For portability, avoid file paths of more than 100 bytes and all links(especially hard links and symbolic links to directories).
The internal implementation writes only the blocks of 512 bytesrequired (including trailing blocks ofNULs), unlike GNUtarwhich by default pads with ‘nul’ to a multiple of 20 blocks(10KB). Implementations which pad differ on whether the block paddingshould occur before or after compression (or both): padding wasdesigned for improved performance on physical tape drives.
The ‘ustar’ format records file modification times to aresolution of 1 second: on file systems with higher resolution it isconventional to discard fractional seconds.
When an externaltar command is used, compressing the tararchive requires thattar supports the-z,-j,-J or--zstdflag, and may require theappropriate command (gzip,bzip2xz orzstd) to be available. For GNUtar, furthercompression programs can be specified bye.g.extra_flags = "-I lz4" or"--lzip" or"--lzop" in argumentextra_flags. Some versions ofbsdtar accept options such as--lz4,--lzop and--lrzip or an external compressorvia--use-compress-program lz4: these could besupplied inextra_flags.
NetBSD prior to 8.0 used flag--xz rather than-J,so this should be usedviaextra_flags = "--xz" ratherthancompression = "xz". The commands from OpenBSD and theHeirloom Toolchest are not documented to supportxz norzstd.
Thetar program in recent macOS (e.g. 15.2) doessupportzstd compression.via anexternal command, but Apple does not supply one.
Thetar programs in commercial Unixen such as AIX andSolaris do not support compression.
GNUtar added support in version 1.22 forxzcompression and in version 1.31 forzstd compression.bsdtar added support forxz in 2019 and forzstd in 2020.
Neither the internal or the known externaltar commandssupport parallel compression — but this function can be used to writean uncompressed tarball which can then be compressed in parallel, forexample withzstd -T0.
For users of macOS. Apple's file systems have a legacy concept of‘resource forks’ dating from classic Mac OS and rarely usednowadays. Apple's version oftar stores these as separatefiles in the tarball with names prefixed by ‘._’, and unpackssuch files into resource forks (if possible): other ways of unpacking(includinguntar inR) unpack them as separate files.
When argumenttar is set to the commandtar on macOS,environment variableCOPYFILE_DISABLE=1 is set, which for thesystem version oftar prevents these separate files beingincluded in the tarball.
https://en.wikipedia.org/wiki/Tar_(file_format),https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06for the way the POSIX utilitypax handlestar formats.
https://github.com/libarchive/libarchive/wiki/FormatTar.
These methods convertR objects to character vectors withBibTeX or LaTeX markup.
toBibtex(object, ...)toLatex(object, ...)## S3 method for class 'Bibtex'print(x, prefix = "", ...)## S3 method for class 'Latex'print(x, prefix = "", ...)toBibtex(object,...)toLatex(object,...)## S3 method for class 'Bibtex'print(x, prefix="",...)## S3 method for class 'Latex'print(x, prefix="",...)
object | object of a class for which a |
x | object of class |
prefix | a character string which is printed at the beginning ofeach line, mostly used to insert whitespace for indentation. |
... | in the print methods, passed to |
Objects of class"Bibtex" or"Latex" are simplycharacter vectors where each element holds one line of thecorresponding BibTeX or LaTeX file.
citEntry andsessionInfo for examples
Text progress bar in theR console.
txtProgressBar(min = 0, max = 1, initial = 0, char = "=", width = NA, title, label, style = 1, file = "")getTxtProgressBar(pb)setTxtProgressBar(pb, value, title = NULL, label = NULL)## S3 method for class 'txtProgressBar'close(con, ...)txtProgressBar(min=0, max=1, initial=0, char="=", width=NA, title, label, style=1, file="")getTxtProgressBar(pb)setTxtProgressBar(pb, value, title=NULL, label=NULL)## S3 method for class 'txtProgressBar'close(con,...)
min,max | (finite) numeric values for the extremes of theprogress bar. Must have |
initial,value | initial or new value for the progress bar. See‘Details’ for what happens with invalid values. |
char | the character (or character string) to form the progressbar. Must have non-zero display width. |
width | the width of the progress bar, as a multiple of the widthof |
style | the ‘style’ of the bar – see ‘Details’. |
file | an open connection object or |
pb,con | an object of class |
title,label | ignored, for compatibility with other progress bars. |
... | for consistency with the generic. |
txtProgressBar will display a progress bar on theR console(or a connection) via a text representation.
setTxtProgessBar will update the value. Missing(NA) and out-of-range values ofvalue will be(silently) ignored. (Such values ofinitial cause the progressbar not to be displayed until a valid value is set.)
The progress bar should beclosed when finished with: thisoutputs the final newline character.
style = 1 andstyle = 2 just shows a line ofchar. They differ in thatstyle = 2 redraws the lineeach time, which is useful if other code might be writing to theRconsole.style = 3 marks the end of the range by ‘|’ andgives a percentage to the right of the bar.
FortxtProgressBar an object of class"txtProgressBar".
ForgetTxtProgressBar andsetTxtProgressBar, alength-one numeric vector giving the previous value (invisibly forsetTxtProgressBar).
Usingstyle 2 or 3 or reducing the value withstyle = 1uses ‘\r’ to return to the left margin – the interpretation ofcarriage return is up to the terminal or console in whichR isrunning, and this is liable to produce ugly output on a connectionother than a terminal, including whenstdout() isredirected to a file.
winProgressBar (Windows only),tkProgressBar (Unix-alike platforms).
# slowtestit <- function(x = sort(runif(20)), ...){ pb <- txtProgressBar(...) for(i in c(0, x, 1)) {Sys.sleep(0.5); setTxtProgressBar(pb, i)} Sys.sleep(1) close(pb)}testit()testit(runif(10))testit(style = 3)testit(char=' \u27a4')# slowtestit<-function(x= sort(runif(20)),...){ pb<- txtProgressBar(...)for(iin c(0, x,1)){Sys.sleep(0.5); setTxtProgressBar(pb, i)} Sys.sleep(1) close(pb)}testit()testit(runif(10))testit(style=3)testit(char=' \u27a4')
Convert a data object to logical, integer, numeric, complex, characteror factor as appropriate.
type.convert(x, ...)## Default S3 method:type.convert(x, na.strings = "NA", as.is, dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"), tryLogical = TRUE, ...)## S3 method for class 'data.frame'type.convert(x, ...)## S3 method for class 'list'type.convert(x, ...)type.convert(x,...)## Default S3 method:type.convert(x, na.strings="NA", as.is, dec=".", numerals= c("allow.loss","warn.loss","no.loss"), tryLogical=TRUE,...)## S3 method for class 'data.frame'type.convert(x,...)## S3 method for class 'list'type.convert(x,...)
x | a vector, matrix, array, data frame, or list. |
na.strings | a vector of strings which are to be interpreted as |
as.is | whether to store strings as plain |
dec | the character to be assumed for decimal points. |
numerals | string indicating how to convert numbers whoseconversion to double precision would lose accuracy, typically when |
tryLogical | a |
... | arguments to be passed to or from methods. |
This helper function is used byread.table. When thedata objectx is a data frame or list, the function is calledrecursively for each column or list element.
Given a vector, the function attempts to convert it to logical,integer, numeric or complex, and when additionallyas.is = FALSE(no longer the default!), converts a character vector tofactor. The first type that can accept all the non-missingvalues is chosen.
Vectors which are entirely missing values are converted to logical,sinceNA is primarily logical.
IftryLogical is true as by default, vectors containing justF,T,FALSE,TRUEand values fromna.strings are converted to logical. This may be surprisingin a context where you have manycharacter columns with e.g.,1-letter strings and you happen to get one with only"F". In suchcasestryLogical = FALSE is recommended.Vectors containing optional whitespace followed by decimal constantsrepresentable asR integers or values fromna.strings areconverted to integer. Other vectors containing optional whitespacefollowed by other decimal or hexadecimal constants (seeNumericConstants), orNaN,Inf orinfinity(ignoring case) or values fromna.strings are converted tonumeric. Where converting inputs to numeric or complex would resultin loss of accuracy they can optionally be returned as strings or (foras.is = FALSE) factors.
Since this is a helper function, the caller should always pass anappropriate value ofas.is.
An object likex but using another storage mode whenappropriate.
R Core, with a contribution by Arni Magnusson
read.table,class,storage.mode.
## Numeric to integerclass(rivers)x <- type.convert(rivers, as.is = TRUE)class(x)## Convert many columnsauto <- type.convert(mtcars, as.is = TRUE)str(mtcars)str(auto)## Convert matrixphones <- type.convert(WorldPhones, as.is = TRUE)storage.mode(WorldPhones)storage.mode(phones)## Factor or characterchr <- c("A", "B", "B", "A")ch2 <- c("F", "F", "NA", "F")(fac <- factor(chr))type.convert(chr, as.is = FALSE) # -> factortype.convert(fac, as.is = FALSE) # -> factortype.convert(chr, as.is = TRUE) # -> charactertype.convert(fac, as.is = TRUE) # -> charactertype.convert(ch2, as.is = TRUE) #-> logicaltype.convert(ch2, as.is = TRUE, tryLogical=FALSE) #-> character## Numeric to integerclass(rivers)x<- type.convert(rivers, as.is=TRUE)class(x)## Convert many columnsauto<- type.convert(mtcars, as.is=TRUE)str(mtcars)str(auto)## Convert matrixphones<- type.convert(WorldPhones, as.is=TRUE)storage.mode(WorldPhones)storage.mode(phones)## Factor or characterchr<- c("A","B","B","A")ch2<- c("F","F","NA","F")(fac<- factor(chr))type.convert(chr, as.is=FALSE)# -> factortype.convert(fac, as.is=FALSE)# -> factortype.convert(chr, as.is=TRUE)# -> charactertype.convert(fac, as.is=TRUE)# -> charactertype.convert(ch2, as.is=TRUE)#-> logicaltype.convert(ch2, as.is=TRUE, tryLogical=FALSE)#-> character
Extract files from or list the contents of a tar archive.
untar(tarfile, files = NULL, list = FALSE, exdir = ".", compressed = NA, extras = NULL, verbose = FALSE, restore_times = TRUE, support_old_tars = Sys.getenv("R_SUPPORT_OLD_TARS", FALSE), tar = Sys.getenv("TAR"))untar(tarfile, files=NULL, list=FALSE, exdir=".", compressed=NA, extras=NULL, verbose=FALSE, restore_times=TRUE, support_old_tars= Sys.getenv("R_SUPPORT_OLD_TARS",FALSE), tar= Sys.getenv("TAR"))
tarfile | The pathname of the tar file: tilde expansion (see |
files | A character vector of recorded filepaths to be extracted:the default is to extract all files. |
list | If |
exdir | The directory to extract files to (the equivalent of |
compressed | (Deprecated in favour of auto-detection, used onlyfor an external The external command may ignore the selected compression type anddetect a type automagically. |
extras |
|
verbose | logical: if true echo the command used for an external |
restore_times | logical. If true (default) restore filemodification times. If false, the equivalent of the-mflag. Times in tarballs are supposed to be in UTC, but tarballshave been submitted to CRAN with times in the future or far past:this argument allows such times to be discarded. Note that file times in a tarball are stored with a resolution of 1second, and can only be restored to the resolution supported by thefile system (which on a FAT system is 2 seconds). |
support_old_tars | logical. If false (the default), the external If true, theR code calls an appropriate decompressor and pipesthe output to |
tar | character string: the path to the command to be used or |
This is either a wrapper for atar command or for aninternal implementation written inR. The latter is used iftarfile is a connection or if the argumenttar is"internal" or"" (except on Windows, whentar.exe is tried first).
Unless otherwise stated three types of compression of the tar file aresupported:gzip,bzip2 andxz.
What options are supported will depend on thetarimplementation used: the"internal" one is intended to providesupport for most in a platform-independent way.
Modern GNUtar versions supportcompressed archives and since 1.15 are able to detect the type ofcompression automatically: version 1.22 added support forxz compression and version 1.31 forzstdcompression.
On a Unix-alike,configure will set environment variableTAR, preferring GNU tar if found.
bsdtar:macOS 10.6 and later (and FreeBSD and someother OSes) have atar from the libarchive projectwhich detects known-to-it forms of compression automagically.However, this may rely on an external command being available: macOShas a tar which knows aboutzstd compression, but relieson azstd command which it does not supply.
This added support forxz in 2019 and forzstdin 2020 (if the appropriate library or external program isavailable).
It is undocumented if NetBSD'star candetect compression automagically: for versions before 8 the flagforxz compression was--xz not-J.Sosupport_old_tars = TRUE is recommended (or usebsdtar if installed).
OpenBSD'star does not detect compressionautomagically. It has no support forxz beyond reportingthat the file isxz-compressed. Sosupport_old_tars= TRUE is recommended.
Thistar does automagicallydetectgzip andbzip2 compression (undocumented)but had no support forxz norzstd compression.
Environment variableR_GZIPCMD gives thecommand to decompressgzip files, andR_BZIPCMD forbzip2 files. (On Unix-alikesthese are set at installation if found.) An external program calledxz orzstd is used if available: if notdecompression is expected to fail.
Argumentscompressed,extras andverbose are onlyused when an externaltar is used.
Some externaltar commands will detect some oflrzip,lzma,lz4 andlzopcompression in addition togzip,bzip2,xz andzstd. (For some externaltarcommands, compressed tarfiles can only be read if the appropriateutility program is available.) For GNUtar, further(de)compression programs can be specified by e.g.extras = "-I lz4". Forbsdtar this could beextras = "--use-compress-program lz4". Most commands will detect (thenowadays rarely seen) ‘.tar.Z’ archives compressed bycompress.
The internal implementation restores symbolic links as links on aUnix-alike, and as file copies on Windows (which works only forexisting files, not for directories), and hard links as links. If thelinking operation fails (as it may on a FAT file system), a file copyis tried. Since it usesgzfile to read a file it canhandle files compressed by any of the methods that function canhandle: at leastcompress,gzip,bzip2,xz andzstd compression, and some types oflzma compression. It does not guard against restoringabsolute file paths, as sometar implementations do. Itwill create the parent directories for directories or files in thearchive if necessary. It handles the USTAR/POSIX, GNU andpax ways of handling file paths of more than 100 bytes, andthe GNU way of handling link targets of more than 100 bytes.
You may see warnings from the internal implementation suchas
unsupported entry type 'x'
This often indicates an invalid archive: entry types"A-Z" areallowed as extensions, but other types are reserved. The only thingyou can do with such an archive is to find atar program thathandles it, and look carefully at the resulting files. There may alsobe the warning
using pax extended headers
This indicates that additional information may have been discarded,such asACLs, encodings ....
The former standards only supported ASCII filenames (indeed, onlyalphanumeric plus period, underscore and hyphen).untar makesno attempt to map filenames to those acceptable on the current system,and treats the filenames in the archive as applicable without anyre-encoding in the current locale.
The internal implementation does not special-case ‘resourceforks’ in macOS: that system'star command does. This maylead to unexpected files with names with prefix ‘._’.
Iflist = TRUE, a character vector of (relative or absolute)paths of files contained in the tar archive.
Otherwise the return code fromsystem with an externaltar or0L, invisibly.
Extract files from or list a zip archive.
unzip(zipfile, files = NULL, list = FALSE, overwrite = TRUE, junkpaths = FALSE, exdir = ".", unzip = "internal", setTimes = FALSE)unzip(zipfile, files=NULL, list=FALSE, overwrite=TRUE, junkpaths=FALSE, exdir=".", unzip="internal", setTimes=FALSE)
zipfile | The pathname of the zip file: tilde expansion (see |
files | A character vector of recorded filepaths to be extracted:the default is to extract all files. |
list | If |
overwrite | If |
junkpaths | If |
exdir | The directory to extract files to (the equivalent of |
unzip | The method to be used. An alternative is to use |
setTimes | logical. For the internal method only, should thefile times be set based on the times in the zip file? (NB: thisapplies to included files, not to directories.) |
Iflist = TRUE, a data frame with columnsName(character)Length (the size of the uncompressed file, numeric)andDate (of class"POSIXct").
Otherwise for the"internal" method, a character vector of thefilepaths extracted to, invisibly.
The default internal method is a minimal implementation, principallydesigned for Windows' users to be able to unpack Windows binarypackages without external software. It does not (for example) supportUnicode filenames as introduced inzip 3.0: for that useunzip = "unzip" withunzip 6.00 or later. It doeshave some support forbzip2 compression and > 2GB zip files(but not >= 4GB files pre-compression contained in a zip file: likemany builds ofunzip it may truncate these, inR's casewith a warning if possible).
Ifunzip specifies a program, the format of the dates listedwithlist = TRUE is unknown (on Windows it can even depend onthe current locale) and the return values could beNA orexpressed in the wrong time zone or misinterpreted (the latter beingfar less likely as fromunzip 6.00).
File times in zip files are stored in the style of MS-DOS, as local timesto an accuracy of 2 seconds. This is not very useful whentransferring zip files between machines (even across continents), sowe chose not to restore them by default.
The internal C code useszlib and is in particular based on thecontributed ‘minizip’ application in thezlib sources(fromhttps://zlib.net/) by Gilles Vollant.
unz to read a single component from a zip file.
zip for packing, i.e., the “inverse” ofunzip();furtheruntar andtar, the correspondingpair for (un)packing tar archives (“tarballs”) such asRsource packages.
old.packages indicates packages which have a (suitable) laterversion on the repositories whereasupdate.packages offers todownload and install such packages.
new.packages looks for (suitable) packages on the repositoriesthat are not already installed, and optionally offers them forinstallation.
update.packages(lib.loc = NULL, repos = getOption("repos"), contriburl = contrib.url(repos, type), method, instlib = NULL, ask = TRUE, available = NULL, oldPkgs = NULL, ..., checkBuilt = FALSE, type = getOption("pkgType"))old.packages(lib.loc = NULL, repos = getOption("repos"), contriburl = contrib.url(repos, type), instPkgs = installed.packages(lib.loc = lib.loc, ...), method, available = NULL, checkBuilt = FALSE, ..., type = getOption("pkgType"))new.packages(lib.loc = NULL, repos = getOption("repos"), contriburl = contrib.url(repos, type), instPkgs = installed.packages(lib.loc = lib.loc, ...), method, available = NULL, ask = FALSE, ..., type = getOption("pkgType"))update.packages(lib.loc=NULL, repos= getOption("repos"), contriburl= contrib.url(repos, type), method, instlib=NULL, ask=TRUE, available=NULL, oldPkgs=NULL,..., checkBuilt=FALSE, type= getOption("pkgType"))old.packages(lib.loc=NULL, repos= getOption("repos"), contriburl= contrib.url(repos, type), instPkgs= installed.packages(lib.loc= lib.loc,...), method, available=NULL, checkBuilt=FALSE,..., type= getOption("pkgType"))new.packages(lib.loc=NULL, repos= getOption("repos"), contriburl= contrib.url(repos, type), instPkgs= installed.packages(lib.loc= lib.loc,...), method, available=NULL, ask=FALSE,..., type= getOption("pkgType"))
lib.loc | character vector describing the location of Rlibrary trees to search through (and update packages therein), or |
repos | character vector, the base URL(s) of the repositoriesto use, e.g., the URL of a CRAN mirror such as |
contriburl | URL(s) of the contrib sections of therepositories. Use this argument if your repository isincomplete. Overrides argument |
method | Download method, see |
instlib | character string giving the library directory where toinstall the packages. |
ask | logical indicating whether to ask the user to selectpackages before they are downloaded and installed, or the characterstring |
available | an object as returned by |
checkBuilt | If |
oldPkgs | if specified as non-NULL, |
instPkgs | by default all installed packages, |
... | Arguments such as |
type | character, indicating the type of package to download andinstall. See |
old.packages compares the information fromavailable.packages with that frominstPkgs (computed byinstalled.packages by default) and reports installedpackages that have newer versions on the repositories or, ifcheckBuilt = TRUE, that were built under an earlier minorversion ofR (for example built under 3.3.x when runningR 3.4.0).(For binary package types there is no check that the version on therepository was built under the current minor version ofR,but it is advertised as being suitable for this version.)
new.packages does the same comparison but reports uninstalledpackages that are available at the repositories. Ifask != FALSE it asks which packages should be installed in thefirst element oflib.loc.
The main function of the set isupdate.packages. First a listof all packages found inlib.loc is created and compared withthose available at the repositories. Ifask = TRUE (thedefault) packages with a newer version are reported and for each onethe user can specify if it should be updated. If so the packages aredownloaded from the repositories and installed in the respectivelibrary path (orinstlib if specified).
For how the list of suitable available packages is determined seeavailable.packages.available = NULL make a calltoavailable.packages(contriburl = contriburl, method = method)and hence by default filters onR version, OS type and removesduplicates.
update.packages returnsNULL invisibly.
Forold.packages,NULL or a matrix with one row perpackage, row names the package names and column names"Package","LibPath","Installed" (the version),"Built" (the version built under),"ReposVer" and"Repository".
Fornew.packages a character vector of package names,after any selectedviaask have been installed.
Take care when usingdependencies (passed toinstall.packages) withupdate.packages,for it is unclear where new dependencies should be installed. Thecurrent implementation will only allow it if all the packages to beupdated are in a single library, when that library will be used.
install.packages,available.packages,download.packages,installed.packages,contrib.url.
The options listed forinstall.packages underoptions.
Seedownload.file for how to handle proxies andother options to monitor file transfers.
INSTALL,REMOVE,remove.packages,library,.packages,read.dcf
The ‘R Installation and Administration’ manual for how toset up a repository.
Upgrade objects.
upgrade(object, ...)upgrade(object,...)
object | anR object. |
... | further arguments passed to or from other methods. |
This is a generic function, with a method for"packageStatus" objects.
Extension offile.show to display text files from a remoteserver.
url.show(url, title = url, file = tempfile(), delete.file = TRUE, method, ...)url.show(url, title= url, file= tempfile(), delete.file=TRUE, method,...)
url | The URL to read from. |
title | Title for the browser. |
file | File to copy to. |
delete.file | Delete the file afterwards? |
method | File transfer method: see |
... | Arguments to pass to |
Since this is for text files, it will convert to CRLF line endings onWindows.
## Not run: url.show("https://www.stats.ox.ac.uk/pub/datasets/csb/ch3a.txt")## Not run: url.show("https://www.stats.ox.ac.uk/pub/datasets/csb/ch3a.txt")
Functions to percent-encode or decode characters in URLs.
URLencode(URL, reserved = FALSE, repeated = FALSE)URLdecode(URL)URLencode(URL, reserved=FALSE, repeated=FALSE)URLdecode(URL)
URL | a character vector. |
reserved | logical: should ‘reserved’ characters beencoded? See ‘Details’. |
repeated | logical: should apparently already-encoded URLs beencoded again? |
Characters in a URL other than the English alphanumeric characters and‘- _ . ~’ should be encoded as%plus a two-digit hexadecimal representation, and any single-bytecharacter can be so encoded. (Multi-byte characters are encodedbyte-by-byte.) The standard refers to this as ‘percent-encoding’.
In addition, ‘! $ & ' ( ) * + , ; = : / ? @ # [ ]’ are reservedcharacters, and should be encoded unless used in their reserved sense,which is scheme specific. The default inURLencode is to leavethem alone, which is appropriate for ‘file://’ URLs, but probablynot for ‘http://’ ones.
An ‘apparently already-encoded URL’ is one containing%xx for two hexadecimal digits.
A character vector.
Internet STD 66 (formerly RFC 3986),https://www.rfc-editor.org/info/std66
(y <- URLencode("a url with spaces and / and @"))URLdecode(y)(y <- URLencode("a url with spaces and / and @", reserved = TRUE))URLdecode(y)URLdecode(z <- "ab%20cd")c(URLencode(z), URLencode(z, repeated = TRUE)) # first is usually wanted## both functions support character vectors of length > 1y <- URLdecode(URLencode(c("url with space", "another one")))(y<- URLencode("a url with spaces and / and @"))URLdecode(y)(y<- URLencode("a url with spaces and / and @", reserved=TRUE))URLdecode(y)URLdecode(z<-"ab%20cd")c(URLencode(z), URLencode(z, repeated=TRUE))# first is usually wanted## both functions support character vectors of length > 1y<- URLdecode(URLencode(c("url with space","another one")))
(Currently none)
These functions are provided for compatibility with older versions ofR only, and may be defunct as soon as of the next release.
Invoke a spreadsheet-style data viewer on a matrix-likeR object.
View(x, title)View(x, title)
x | anR object which can be coerced to a data frame withnon-zero numbers of rows and columns. |
title | title for viewer window. Defaults to name of |
Objectx is coerced (if possible) to a data frame, thencolumns are converted to character usingformat.data.frame.The object is then viewed in a spreadsheet-like data viewer, aread-only version ofdata.entry.
If there are row names on the data frame that are not1:nrow,they are displayed in a separate first column calledrow.names.
Objects with zero columns or zero rows are not accepted.
the array of cells can be navigated by thecursor keys and Home, End, Page Up and Page Down (where supported byX11) as well as Enter and Tab.
the array of cells can be navigatedviathe scrollbars and by the cursor keys, Home, End, Page Up and Page Down.
On Windows, the initial size of the data viewer window is takenfrom the default dimensions of a pager (seeRconsole),but adjusted downwards to show a whole number of rows and columns.
InvisibleNULL. The functions puts up a window and returnsimmediately: the window can be closed via its controls or menus.
View a specified package vignette, or list the available ones;display it rendered in a viewer, and get or edit itsR source file.
vignette(topic, package = NULL, lib.loc = NULL, all = TRUE)## S3 method for class 'vignette'print(x, ...)## S3 method for class 'vignette'edit(name, ...)vignette(topic, package=NULL, lib.loc=NULL, all=TRUE)## S3 method for class 'vignette'print(x,...)## S3 method for class 'vignette'edit(name,...)
topic | a character string giving the (base) name of the vignetteto view. If omitted, all vignettes from all installed packages arelisted. |
package | a character vector with the names of packages tosearch through, or |
lib.loc | a character vector of directory names ofR libraries,or |
all | logical; if |
x,name | object of class |
... | ignored by the |
Functionvignette returns an object of the same class, theprint method opens a viewer for it.
On Unix-alikes,the program specified by thepdfviewer option is used forviewing PDF versions of vignettes.
If several vignettes have PDF/HTML versions with base name identicaltotopic, the first one found is used.
If no topics are given, all available vignettes are listed. Thecorresponding information is returned in an object of class"packageIQR".
browseVignettes for an HTML-based vignette browser;RShowDoc("basename", package = "pkgname") displays a“rendered” vignette (pdf or html).
## List vignettes from all *attached* packagesvignette(all = FALSE)## List vignettes from all *installed* packages (can take a long time!):vignette(all = TRUE)## The grid intro vignette -- open it## Not run: vignette("grid") # calling print()## The same (conditional on existence of the vignettte).## Note that 'package = *' is much faster in the case of many installed packages:if(!is.null(v1 <- vignette("grid", package="grid"))) {## Not run: v1 # calling print(.) str(v1) ## Now let us have a closer look at the code ## Not run: edit(v1) # e.g., to send lines ...}# if( has vignette "installed")## A package can have more than one vignette (package grid has several):vignette(package = "grid")if(interactive()) { ## vignette("rotated") ## The same, but without searching for it: vignette("rotated", package = "grid")}## List vignettes from all *attached* packagesvignette(all=FALSE)## List vignettes from all *installed* packages (can take a long time!):vignette(all=TRUE)## The grid intro vignette -- open it## Not run: vignette("grid") # calling print()## The same (conditional on existence of the vignettte).## Note that 'package = *' is much faster in the case of many installed packages:if(!is.null(v1<- vignette("grid", package="grid"))){## Not run: v1 # calling print(.) str(v1)## Now let us have a closer look at the code## Not run: edit(v1) # e.g., to send lines ...}# if( has vignette "installed")## A package can have more than one vignette (package grid has several):vignette(package="grid")if(interactive()){## vignette("rotated")## The same, but without searching for it: vignette("rotated", package="grid")}
Collect errors (class"error", typically fromtryCatch)from a listx into a “summary warning”, by defaultproduce awarning and keep that message as"warningMsg" attribute.
warnErrList(x, warn = TRUE, errValue = NULL)warnErrList(x, warn=TRUE, errValue=NULL)
x | a |
warn | logical indicating if |
errValue | the value with which errors should be replaced. |
alist of the same length and names as thexargument, with the error components replaced byerrValue,NULL by default, and summarized in the"warningMsg" attribute.
ThewarnErrList() utility has been used inlmList() andnlsList() inrecommended packagenlme forever.
## Regression for each Chick:ChWtgrps <- split(ChickWeight, ChickWeight[,"Chick"])sapply(ChWtgrps, nrow)# typically 12 obs.nlis1 <- lapply(ChWtgrps, function(DAT) tryCatch(error = identity, lm(weight ~ (Time + I(Time^2)) * Diet, data = DAT)))nl1 <- warnErrList(nlis1) #-> warning :## 50 times the same error (as Diet has only one level in each group)stopifnot(sapply(nl1, is.null)) ## all errors --> all replaced by NULLnlis2 <- lapply(ChWtgrps, function(DAT) tryCatch(error = identity, lm(weight ~ Time + I(Time^2), data = DAT)))nl2 <- warnErrList(nlis2)stopifnot(identical(nl2, nlis2)) # because there was *no* error at allnlis3 <- lapply(ChWtgrps, function(DAT) tryCatch(error = identity, lm(weight ~ poly(Time, 3), data = DAT)))nl3 <- warnErrList(nlis3) # 1 error caught:stopifnot(inherits(nlis3[[1]], "error") , identical(nl3[-1], nlis3[-1]) , is.null(nl3[[1]]))## With different error messagesif(requireNamespace("nlme")) { # almost always, as it is recommended data(Soybean, package="nlme") attr(Soybean, "formula") #-> weight ~ Time | Plot => split by "Plot": L <- lapply(split(Soybean, Soybean[,"Plot"]), function(DD) tryCatch(error = identity, nls(weight ~ SSlogis(Time, Asym, xmid, scal), data = DD))) Lw <- warnErrList(L)} # if <nlme>## Regression for each Chick:ChWtgrps<- split(ChickWeight, ChickWeight[,"Chick"])sapply(ChWtgrps, nrow)# typically 12 obs.nlis1<- lapply(ChWtgrps,function(DAT) tryCatch(error= identity, lm(weight~(Time+ I(Time^2))* Diet, data= DAT)))nl1<- warnErrList(nlis1)#-> warning :## 50 times the same error (as Diet has only one level in each group)stopifnot(sapply(nl1, is.null))## all errors --> all replaced by NULLnlis2<- lapply(ChWtgrps,function(DAT) tryCatch(error= identity, lm(weight~ Time+ I(Time^2), data= DAT)))nl2<- warnErrList(nlis2)stopifnot(identical(nl2, nlis2))# because there was *no* error at allnlis3<- lapply(ChWtgrps,function(DAT) tryCatch(error= identity, lm(weight~ poly(Time,3), data= DAT)))nl3<- warnErrList(nlis3)# 1 error caught:stopifnot(inherits(nlis3[[1]],"error"), identical(nl3[-1], nlis3[-1]), is.null(nl3[[1]]))## With different error messagesif(requireNamespace("nlme")){# almost always, as it is recommended data(Soybean, package="nlme") attr(Soybean,"formula")#-> weight ~ Time | Plot => split by "Plot": L<- lapply(split(Soybean, Soybean[,"Plot"]),function(DD) tryCatch(error= identity, nls(weight~ SSlogis(Time, Asym, xmid, scal), data= DD))) Lw<- warnErrList(L)}# if <nlme>
On MS Windows only, put up a dialog box to communicate with the user.There are various types, either for the user to select from a set ofbuttons or to edit a string.
winDialog(type = c("ok", "okcancel", "yesno", "yesnocancel"), message)winDialogString(message, default)winDialog(type= c("ok","okcancel","yesno","yesnocancel"), message)winDialogString(message, default)
type | character. The type of dialog box. It will have thebuttons implied by its name. |
message | character. The information field of the dialogbox. Limited to 255 chars (by Windows, checked by R). |
default | character. The default string. |
ForwinDialog a character string giving the name of the buttonpressed (in capitals) orNULL (invisibly) if the user had nochoice.
ForwinDialogString a string giving the contents of the textbox whenOk was pressed, orNULL ifCancel was pressed.
The standard keyboard accelerators work with these dialog boxes:where appropriateReturn accepts the default action,Esc cancels and the underlined initial letter (Y orN) can be used.
These functions are only available on Windows.
winMenuAddfile.choose to select a file
packagewindlgs in the package source distribution for ways toprogram dialogs in C in theGraphApp toolkit.
## Not run: winDialog("yesno", "Is it OK to delete file blah")## Not run: winDialog("yesno", "Is it OK to delete file blah")
Get the self-reported Microsoft Windows version number.
win.version()win.version()
win.version is an auxiliary function forsessionInfo andbug.report.
A character string describing the version of Windows reported to be in use.
This function is only available on Microsoft Windows.
The result is based on the WindowsGetVersionEx API function. Itis not known how to detect a version of Windows before it is released, andhence the textual information returned byR may identify an older versionthan installed. The build number is more reliable. When runningR incompatibility mode, the reported version including the build number is thecompatibility version, not the installed version.
if(.Platform$OS.type == "windows") print(win.version())if(.Platform$OS.type=="windows") print(win.version())
Enables users to add, delete and program menus for theRgui in MS Windows.
winMenuAdd(menuname)winMenuAddItem(menuname, itemname, action)winMenuDel(menuname)winMenuDelItem(menuname, itemname)winMenuNames()winMenuItems(menuname)winMenuAdd(menuname)winMenuAddItem(menuname, itemname, action)winMenuDel(menuname)winMenuDelItem(menuname, itemname)winMenuNames()winMenuItems(menuname)
menuname | a character string naming a menu. |
itemname | a character string naming a menu item on an existing menu. |
action | a character string describing the action when that menuis selected, or |
User menus are added to the right of existing menus, and items areadded at the bottom of the menu.
By default the action character string is treated asR input, beingechoed on the command line and parsed and executed as usual.
If themenuname parameter ofwinMenuAddItem does notalready exist, it will be created automatically.
Normally new submenus and menu items are added to the main consolemenu. They may be added elsewhere using the following special names:
$ConsoleMainThe console menu (the default)
$ConsolePopupThe console popup menu
$Graph<n>MainThe menu for graphics window<n>
$Graph<n>PopupThe popup menu for graphics window<n>
Specifying an existing item inwinMenuAddItem enables theaction to be changed.
Submenus can be specified by separating the elements inmenuname by slashes: as a consequence menu names may notcontain slashes.
If theaction is specified as"none" no action is taken:this can be useful to reserve items for future expansion.
The functionwinMenuNames can be used to find out what menushave been created by the user and returns a vector of the existingmenu names.
ThewinMenuItems function will take the name of a menu andreturn the items that exist in that menu. The return value is a namedvector where the names correspond to the names of the items and thevalues of the vector are the corresponding actions.
ThewinMenuDel function will delete a menu and all of its itemsand submenus.winMenuDelItem just deletes one menu item.
The total path to an item (menu string plus item string) cannot exceed1000 bytes, and the menu string cannot exceed 500 bytes.
NULL, invisibly. If an error occurs, an informative errormessage will be given.
These functions are only available on Windows and only when usingtheRgui, hence not inESS norRStudio.
## Not run: winMenuAdd("Testit")winMenuAddItem("Testit", "one", "aaaa")winMenuAddItem("Testit", "two", "bbbb")winMenuAdd("Testit/extras")winMenuAddItem("Testit", "-", "")winMenuAddItem("Testit", "two", "disable")winMenuAddItem("Testit", "three", "cccc")winMenuAddItem("Testit/extras", "one more", "ddd")winMenuAddItem("Testit/extras", "and another", "eee")winMenuAdd("$ConsolePopup/Testit")winMenuAddItem("$ConsolePopup/Testit", "six", "fff")winMenuNames()winMenuItems("Testit")## End(Not run)## Not run:winMenuAdd("Testit")winMenuAddItem("Testit","one","aaaa")winMenuAddItem("Testit","two","bbbb")winMenuAdd("Testit/extras")winMenuAddItem("Testit","-","")winMenuAddItem("Testit","two","disable")winMenuAddItem("Testit","three","cccc")winMenuAddItem("Testit/extras","one more","ddd")winMenuAddItem("Testit/extras","and another","eee")winMenuAdd("$ConsolePopup/Testit")winMenuAddItem("$ConsolePopup/Testit","six","fff")winMenuNames()winMenuItems("Testit")## End(Not run)
Put up a Windows progress bar widget, update and access it.
winProgressBar(title = "R progress bar", label = "", min = 0, max = 1, initial = 0, width = 300)getWinProgressBar(pb)setWinProgressBar(pb, value, title = NULL, label = NULL)## S3 method for class 'winProgressBar'close(con, ...)winProgressBar(title="R progress bar", label="", min=0, max=1, initial=0, width=300)getWinProgressBar(pb)setWinProgressBar(pb, value, title=NULL, label=NULL)## S3 method for class 'winProgressBar'close(con,...)
title,label | character strings, giving the window title and thelabel on the dialog box respectively. |
min,max | (finite) numeric values for the extremes of theprogress bar. |
initial,value | initial or new value for the progress bar. |
width | the width of the progress bar in pixels: the dialog boxwill be 40 pixels wider (plus frame). |
pb,con | an object of class |
... | for consistency with the generic. |
winProgressBar will display a progress bar centred on thescreen. Space will be allocated for the label only if it is non-empty.
setWinProgessBar will update the value and for non-NULLvalues, the title and label (provided there was one when the widgetwas created). Missing (NA) and out-of-range values ofvalue will be (silently) ignored.
The progress bar should beclosed when finished with, but itwill be garbage-collected once noR object refers to it.
ForwinProgressBar an object of class"winProgressBar".
ForgetWinProgressBar andsetWinProgressBar, alength-one numeric vector giving the previous value (invisibly forsetWinProgressBar).
These functions are only available on Windows.
On all platforms,txtProgressBar,tkProgressBar
write.table prints its required argumentx (afterconverting it to a data frame if it is not one nor a matrix) toa file orconnection.
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", eol = "\n", na = "NA", dec = ".", row.names = TRUE, col.names = TRUE, qmethod = c("escape", "double"), fileEncoding = "")write.csv(...)write.csv2(...)write.table(x, file="", append=FALSE, quote=TRUE, sep=" ", eol="\n", na="NA", dec=".", row.names=TRUE, col.names=TRUE, qmethod= c("escape","double"), fileEncoding="")write.csv(...)write.csv2(...)
x | the object to be written, preferably a matrix or data frame.If not, it is attempted to coerce |
file | either a character string naming a file or aconnectionopen for writing. |
append | logical. Only relevant if |
quote | a logical value ( |
sep | the field separator string. Values within each row of |
eol | the character(s) to print at the end of each line (row).For example, |
na | the string to use for missing values in the data. |
dec | the string to use for decimal points in numeric or complexcolumns: must be a single character. |
row.names | either a logical value indicating whether the rownames of |
col.names | either a logical value indicating whether the columnnames of |
qmethod | a character string specifying how to deal with embeddeddouble quote characters when quoting strings. Must be one of |
fileEncoding | character string: if non-empty declares theencoding to be used on a file (not a connection) so the character data canbe re-encoded as they are written. See |
... | arguments to |
If the table has no columns the rownames will be written only ifrow.names = TRUE, andvice versa.
Real and complex numbers are written to the maximal possible precision.
If a data frame has matrix-like columns these will be converted tomultiple columns in the result (viaas.matrix)and so a charactercol.names or a numericquote shouldrefer to the columns in the result, not the input. Such matrix-likecolumns are unquoted by default.
Any columns in a data frame which are lists or have a class(e.g., dates) will be converted by the appropriateas.charactermethod: such columns are unquoted by default. On the other hand,any class information for a matrix is discarded and non-atomic(e.g., list) matrices are coerced to character.
Only columns which have been converted to character will be quoted ifspecified byquote.
Thedec argument only applies to columns that are not subjectto conversion to character because they have a class or are part of amatrix-like column (or matrix), in particular to columns protected byI(). Useoptions("OutDec") to controlsuch conversions.
In almost all cases the conversion of numeric quantities is governedby the option"scipen" (seeoptions), but withthe internal equivalent ofdigits = 15. For finer control, useformat to make a character matrix/data frame, and callwrite.table on that.
These functions check for a user interrupt every 1000 lines of output.
Iffile is a non-open connection, an attempt is made to open itand then close it after use.
To write a Unix-style file on Windows, use a binary connectione.g.file = file("filename", "wb").
By default,write.table does not output a column name for a column of row names. Ifcol.names = NA androw.names = TRUE a blank column nameis added, which is the convention used for CSV files to be read byspreadsheets. Note that such CSV files can be read inR by
read.csv(file = "<filename>", row.names = 1)
write.csv andwrite.csv2 provide convenience wrappersfor writing CSV files. They setsep anddec (seebelow),qmethod = "double", andcol.names toNAifrow.names = TRUE (the default) and toTRUE otherwise.
write.csv uses"." for the decimal point and a comma forthe separator.
write.csv2 uses a comma for the decimal point and a semicolon forthe separator, the Excel convention for CSV files in some WesternEuropean locales.
These wrappers are deliberately inflexible: they are designed toensure that the correct conventions are used to write a valid file.Attempts to changeappend,col.names,sep,dec orqmethod are ignored, with a warning.
CSV files do not record an encoding, and this causes problems if theyare not ASCII for many other applications. Windows Excel 2007/10 willopen files (e.g., by the file association mechanism) correctly if theyare ASCII or UTF-16 (usefileEncoding = "UTF-16LE") or perhapsin the current Windows codepage (e.g.,"CP1252"), but the‘Text Import Wizard’ (from the ‘Data’ tab) allows farmore choice of encodings. Excel:mac 2004/8 canimport only‘Macintosh’ (which seems to mean Mac Roman), ‘Windows’(perhaps Latin-1) and ‘PC-8’ files. OpenOffice 3.x asks forthe character set when opening the file.
There is anIETF RFC4180 (https://www.rfc-editor.org/rfc/rfc4180)for CSV files, which mandates comma as the separator and CRLF lineendings.write.csv writes compliant files on Windows: useeol = "\r\n" on other platforms.
write.table can be slow for data frames with large numbers(hundreds or more) of columns: this is inevitable as each column couldbe of a different class and so must be handled separately. If theyare all of the same class, consider using a matrix instead.
The ‘R Data Import/Export’ manual.
write.matrix in packageMASS.
x <- data.frame(a = "a \" quote", b = pi)tf <- tempfile(fileext = ".csv")## To write a CSV file for input to Excel one might usewrite.table(x, file = tf, sep = ",", col.names = NA, qmethod = "double")file.show(tf)## and to read this file back into R one needsread.table(tf, header = TRUE, sep = ",", row.names = 1)## NB: you do need to specify a separator if qmethod = "double".### Alternativelywrite.csv(x, file = tf)read.csv(tf, row.names = 1)## or without row nameswrite.csv(x, file = tf, row.names = FALSE)read.csv(tf)## Not run: ## To write a file in Mac Roman for simple use in Mac Excel 2004/8write.csv(x, file = "foo.csv", fileEncoding = "macroman")## or for Windows Excel 2007/10write.csv(x, file = "foo.csv", fileEncoding = "UTF-16LE")## End(Not run)x<- data.frame(a="a \" quote", b= pi)tf<- tempfile(fileext=".csv")## To write a CSV file for input to Excel one might usewrite.table(x, file= tf, sep=",", col.names=NA, qmethod="double")file.show(tf)## and to read this file back into R one needsread.table(tf, header=TRUE, sep=",", row.names=1)## NB: you do need to specify a separator if qmethod = "double".### Alternativelywrite.csv(x, file= tf)read.csv(tf, row.names=1)## or without row nameswrite.csv(x, file= tf, row.names=FALSE)read.csv(tf)## Not run:## To write a file in Mac Roman for simple use in Mac Excel 2004/8write.csv(x, file="foo.csv", fileEncoding="macroman")## or for Windows Excel 2007/10write.csv(x, file="foo.csv", fileEncoding="UTF-16LE")## End(Not run)
A wrapper for an externalzip command to create zip archives.
zip(zipfile, files, flags = "-r9X", extras = "", zip = Sys.getenv("R_ZIPCMD", "zip"))zip(zipfile, files, flags="-r9X", extras="", zip= Sys.getenv("R_ZIPCMD","zip"))
zipfile | The pathname of the zip file: tilde expansion (see |
files | A character vector of recorded filepaths to be included. |
flags | A character string of flags to be passed to the command:see ‘Details’. |
extras | An optional character vector: see ‘Details’. |
zip | A character string specifying the external command to be used. |
On a Unix-alike, the default forzip will use thevalue ofR_ZIPCMD, whose default is set in ‘etc/Renviron’to thezip command found during configuration. On Windows, thedefault relies on azip program (for example that fromRtools) being in the path.
The default forflags is that appropriate for zipping up adirectory tree in a portable way: see the system-specific help for thezip command for other possibilities.
Argumentextras can be used to specify-x or-ifollowed by a list of filepaths to exclude or include. Sinceextras will be treated as if passed tosystem, ifthe filepaths contain spaces they must be quoted e.g. byshQuote.
The status value returned by the external command, invisibly.
unzip,unz; further,tar anduntar for (un)packing tar archives.