Movatterモバイル変換


[0]ホーム

URL:


Package 'utils'

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

Help Index


The R Utils Package

Description

R utility functions

Details

This package contains a collection of utility functions.

For a completelist, uselibrary(help = "utils").

Author(s)

R Core Team and contributors worldwide

Maintainer: R Core Team[email protected]


Approximate String Distances

Description

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.

Usage

adist(x, y=NULL, costs=NULL, counts=FALSE, fixed=TRUE,      partial=!fixed, ignore.case=FALSE, useBytes=FALSE)

Arguments

x

a character vector.Long vectors are not supported.

y

a character vector, orNULL (default) indicatingtakingx asy.

costs

a numeric vector or list with names partially matching‘⁠insertions⁠’, ‘⁠deletions⁠’ and ‘⁠substitutions⁠’ givingthe respective costs for computing the Levenshtein distance, orNULL (default) indicating using unit cost for all threepossible transformations.

counts

a logical indicating whether to optionally return thetransformation counts (numbers of insertions, deletions andsubstitutions) as the"counts" attribute of the returnvalue.

fixed

a logical. IfTRUE (default), thexelements are used as string literals. Otherwise, they are taken asregular expressions andpartial = TRUE is implied(corresponding to the approximate string distance used byagrep withfixed = FALSE).

partial

a logical indicating whether the transformedxelements must exactly match the completey elements, or onlysubstrings of these. The latter corresponds to the approximatestring distance used byagrep (by default).

ignore.case

a logical. IfTRUE, case is ignored forcomputing the distances.

useBytes

a logical. IfTRUE distance computations aredone byte-by-byte rather than character-by-character.

Details

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 complexityO(mn)O(mn), wheremm andnn are thelengths ofs andt, respectively. Additionally computingthe transformation sequence and counts isO(max(m,n))O(\max(m, n)).

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.

Value

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 offsets1-1 in case of no match).

See Also

agrep for approximate string matching (fuzzy matching)using the generalized Levenshtein distance.

Examples

## 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)

Alert the User

Description

Gives an audible or visual signal to the user.

Usage

alarm()

Details

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).

Value

No useful value is returned.

Examples

alarm()

Find Objects by (Partial) Name

Description

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.

Usage

apropos(what, where=FALSE, ignore.case=TRUE,        dot_internals=FALSE, mode="any")find(what, mode="any", numeric=FALSE, simple.words=TRUE)

Arguments

what

a character string. Forfind withsimple.words = TRUE, the name of an object; otherwisearegular expression to match object names against.

where,numeric

logical indicating whether positions in thesearch list should also be returned.

ignore.case

logical indicating if the search should becase-insensitive,TRUE by default.

dot_internals

logical indicating if the search result should showbase internal objects,FALSE by default.

mode

a character string; if not"any", only objects whosemode equalsmode are searched.

simple.words

logical; ifTRUE, thewhat argument isonly searched as a whole word.

Details

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.

Value

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.

Author(s)

Originally, Kurt Hornik and Martin Maechler (May 1997).

See Also

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.

Examples

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,}$")))

Approximate String Match Positions

Description

Determine positions of approximate string matches.

Usage

aregexec(pattern, text, max.distance=0.1, costs=NULL,         ignore.case=FALSE, fixed=FALSE, useBytes=FALSE)

Arguments

pattern

a non-empty character string or a character stringcontaining a regular expression (forfixed = FALSE) to bematched.Coerced byas.character to a string if possible.

text

character vector where matches are sought.Coerced byas.character to a character vector ifpossible.

max.distance

maximum distance allowed for a match.Seeagrep.

costs

cost of transformations.Seeagrep.

ignore.case

a logical. IfTRUE, case is ignored forcomputing the distances.

fixed

IfTRUE, the pattern is matched literally (as is).Otherwise (default), it is matched as a regular expression.

useBytes

a logical. IfTRUE comparisons arebyte-by-byte rather than character-by-character.

Details

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".

Value

A list of the same length astext, each element of which iseither1-1 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 (or1-1 for no match).

See Also

regmatches for extracting the matched substrings.

Examples

## 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)

Rearrange Windows on MS Windows

Description

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.

Usage

arrangeWindows(action, windows, preserve=TRUE, outer=FALSE)

Arguments

action

a character string, the action to perform on thewindows. The choices arec("vertical", "horizontal", "cascade", "minimize", "restore")with default"vertical"; see the ‘Details’ for the interpretation.Abbreviations may be used.

windows

alist of window handles, by defaultproduced bygetWindowsHandles().

preserve

IfTRUE, when tiling preserve the outerboundary of the collection of windows; otherwise make them as largeas will fit.

outer

This argument is only used in MDI mode. IfTRUE,tile the windows on the system desktop. Otherwise, tile them withinthe MDI frame.

Details

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.

Value

This function is called for the side effect of arranging the windows.The list of window handles is returned invisibly.

Note

This is only available on Windows.

Author(s)

Duncan Murdoch

See Also

getWindowsHandles

Examples

## 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)

Ask a Yes/No Question

Description

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.

Usage

askYesNo(msg, default=TRUE,          prompts= getOption("askYesNo", gettext(c("Yes","No","Cancel"))),...)

Arguments

msg

The prompt message for the user.

default

The default response.

prompts

Any of: a character vector containing 3 prompts corresponding toreturn values ofTRUE,FALSE, orNA, ora single character value containing the prompts separated by/ characters, or a function to call.

...

Additional parameters, ignored by the default function.

Details

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.

Value

TRUE for yes,FALSE for no, andNA for cancel.

See Also

readline for more general user input.

Examples

if(interactive())    askYesNo("Do you want to use askYesNo?")

Spell Check Interface

Description

Spell check given files via Aspell, Hunspell or Ispell.

Usage

aspell(files, filter, control= list(), encoding="unknown",       program=NULL, dictionaries= character())

Arguments

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 formalsifile andencoding), or a character string specifying a built-infilter, or a list with the name of a built-in filter and additionalarguments to be passed to it. SeeDetails for availablefilters. If missing orNULL, no filtering is performed.

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, orNULL (default). By default, the system path is searched foraspell,hunspell andispell (in thatorder), and the first one found is used.

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.

Details

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.

Value

A data frame inheriting fromaspell (which has a useful printmethod) with the information about possibly misspelled words.

References

Kurt Hornik and Duncan Murdoch (2011).“Watch your spelling!”The R Journal,3(2), 22–28.doi:10.32614/RJ-2011-014.

See Also

aspell-utils for utilities for spell checking packages.

Examples

## 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"))

Spell Check Utilities

Description

Utilities for spell checking packages via Aspell, Hunspell or Ispell.

Usage

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)

Arguments

dir

a character string specifying the path to a package's rootdirectory.

drop

a character vector naming additional Rd sections to dropwhen selecting text viaRdTextFilter.

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, orNULL (default). By default, the system path is searched foraspell,hunspell andispell (in thatorder), and the first one found is used.

dictionaries

a character vector of names or file paths ofadditional R level dictionaries to use. Seeaspell.

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 toaspell().

out

a character string naming the personal dictionary file towrite to.

language

a character string indicating a language as used byAspell.

Details

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.

See Also

aspell


List Available Packages atCRAN-like Repositories

Description

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).

Usage

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,...)

Arguments

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, seedownload.file.

type

character string, indicate which type of packages: seeinstall.packages.

Iftype = "both" this will use the source repository.

fields

a character vector giving the fields to extract fromthe ‘PACKAGES’ file(s) in addition to the default ones, orNULL (default). Unavailable fields result inNAvalues.

filters

a character vector or list orNULL (default). See ‘Details’.

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

logical indicating if caching shouldhappen intools'R_user_dir("base", "cache")instead oftempdir().

quiet

logical, passed todownload.file(); changeonly if you know what you are doing.

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 todownload.file().

Details

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"

UseCRAN versions in preference to versionsfrom other repositories (even if these have a higher versionnumber). This needs to be appliedbefore the default"duplicates" filter, so cannot be used withadd = TRUE.

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.

Value

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.

See Also

packageStatus,update.packages,install.packages,download.packages,contrib.url.

The ‘R Installation and Administration’ manual for how toset up a repository.

Examples

## 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)

Batch Execution of R

Description

RunR non-interactively with input frominfile andsend output (stdout/stderr) to another file.

Usage

R CMD BATCH[options] infile[outfile]

Arguments

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.Ifinfile starts with a ‘⁠-⁠’, use-- as the finaloption.The default options are--restore --save --no-readline.(Without--no-readline on Windows.)

outfile

the name of a file to which to write output. If notgiven, the name used is that ofinfile, with a possible‘.R’ extension stripped, and ‘.Rout’ appended.

Details

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.

Note

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.


Bibliography Entries

Description

Functionality for representing and manipulating bibliographicinformation in enhanced BibTeX style.

Usage

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,...)

Arguments

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 ifformat(x, style = "text")works correctly. Only if special LaTeX macros (e.g., mathformatting) or special characters (e.g., with accents) arenecessary, atextVersion should be provided.

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.

...

forbibentry: arguments of the formtag=value giving the fields of the entry, withtag andvalue the name and value of the field,respectively. Arguments with empty values are dropped.Field names are case-insensitive.SeeEntry Fields for details.

For theprint() method, extra arguments to pass to therenderer which typically includes theformat() method.

For thecitation class methods, arguments passed to the nextmethod, i.e., the correspondingbibentry one.

For thetoBibtex() method, currently not used.

other

a list of arguments as in... (useful inparticular for fields named the same as formals ofbibentry).

mheader

a character string with optional “outer” headertext.

mfooter

a character string with optional “outer” footertext.

x

an object inheriting from class"bibentry".

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 toorder indicatingthe sort direction.

.bibstyle

a character string naming a bibliography style,seebibstyle.

bibtex

logical indicating if BibTeX code should be givenadditionally; currently applies only tostyle = "citation".The default for theprint() method depends on the number of(bib) entries andgetOption("citation.bibtex.max") (whichitself is 1 by default). For example, to see no BibTeX at all, you can change thedefault byoptions(citation.bibtex.max = 0).

citMsg

logical indicating if a “message” should beadded (to the footer) about how to get BibTeX code whenbibtexis falseandstyle = "citation".

sort

logical indicating if bibentries should be sorted, usingbibstyle(.bibstyle)$sortKeys(x).

macros

a character string or an object with already loaded Rdmacros, seeDetails.

drop

logical used asx[ ..., drop=drop] inside thesort() method.

object

an object inheriting from class"bibentry".

escape

a logical indicating whether non-ASCII characters shouldbe translated to LaTeX escape sequences.

Details

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.

Value

bibentry produces an object of class"bibentry".

Entry Types

bibentry creates"bibentry" objects, which are modeledafter BibTeX entries. The entry should be a valid BibTeX entry type,e.g.,

Article:

An article from a journal or magazine.

Book:

A book with an explicit publisher.

InBook:

A part of a book, which may be a chapter (or sectionor whatever) and/or a range of pages.

InCollection:

A part of a book having its own title.

InProceedings:

An article in a conference proceedings.

Manual:

Technical documentation like a software manual.

MastersThesis:

A Master's thesis.

Misc:

Use this type when nothing else fits.

PhdThesis:

A PhD thesis.

Proceedings:

The proceedings of a conference.

TechReport:

A report published by a school or otherinstitution, usually numbered within a series.

Unpublished:

A document having an author and title, but notformally published.

Entry Fields

The... argument ofbibentry can be any number ofBibTeX fields, including

address:

The address of the publisher or other type ofinstitution.

author:

The name(s) of the author(s), either as aperson object, or as a character string whichas.person correctly coerces to such.

booktitle:

Title of a book, part of which is being cited.

chapter:

A chapter (or section or whatever) number.

doi:

TheDOI(https://en.wikipedia.org/wiki/Digital_Object_Identifier)for the reference.

editor:

Name(s) of editor(s), same format asauthor.

institution:

The publishing institution of a technical report.

journal:

A journal name.

note:

Any additional information that can help the reader.The first word should be capitalized.

number:

The number of a journal, magazine, technical report,or of a work in a series.

pages:

One or more page numbers or range of numbers.

publisher:

The publisher's name.

school:

The name of the school where a thesis was written.

series:

The name of a series or set of books.

title:

The work's title.

url:

A URL for the reference.(If the URL is an expandedDOI, we recommend to use the‘⁠doi⁠’ field with the unexpandedDOI instead.)

volume:

The volume of a journal or multi-volume book.

year:

The year of publication.

See Also

person

Examples

## 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))

Browse Objects in Environment

Description

ThebrowseEnv function opens a browser with list of objectscurrently insys.frame() environment.

Usage

browseEnv(envir= .GlobalEnv, pattern,          excludepatt="^last\\.warning",          html= .Platform$GUI!="AQUA",          expanded=TRUE, properties=NULL,          main=NULL, debugMe=FALSE)

Arguments

envir

anenvironment the objects of which are tobe browsed.

pattern

aregular expression for object subselectionis passed to the internalls() call.

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 fromR.app on macOS.

expanded

whether to show one level of recursion. It can be usefulto switch it toFALSE if your workspace is large. Thisoption is ignored ifhtml is set toFALSE.

properties

a named list of global properties (of the objects chosen)to be showed in the browser; whenNULL (as per default),user, date, and machine information is used.

main

a title string to be used in the browser; whenNULL(as per default) a title is constructed.

debugMe

logical switch; if true, some diagnostic output is produced.

Details

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).

See Also

str,ls.

Examples

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 URL into an HTML Browser

Description

Load a given URL into an HTML browser.

Usage

browseURL(url, browser= getOption("browser"),          encodeIfNeeded=FALSE)

Arguments

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 WindowsNULL is also allowed (and is the default), andimplies that the file association mechanism will be used.

encodeIfNeeded

Should the URL be encoded byURLencode before passing to the browser? This is notneeded (and might be harmful) if thebrowser program/functionitself does encoding, and can be harmful for ‘⁠file://⁠’ URLs on somesystems and for ‘⁠http://⁠’ URLs passed to some CGI applications.Fortunately, most URLs do not need encoding.

Details

On Unix-alikes:

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.

On Windows:

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".

URL schemes

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.

Examples

## 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 Vignettes in an HTML Browser

Description

List available vignettes in an HTML browser with links to PDF,LaTeX/Noweb source, and (tangled) R code (if available).

Usage

browseVignettes(package=NULL, lib.loc=NULL, all=TRUE)## S3 method for class 'browseVignettes'print(x,...)

Arguments

package

a character vector with the names of packages tosearch through, orNULL in which "all" packages (as definedby argumentall) are searched.

lib.loc

a character vector of directory names ofR libraries,orNULL. The default value ofNULL corresponds to alllibraries currently known.

all

logical; ifTRUE searchall available packages in the library trees specified bylib.loc,and ifFALSE, search only attached packages.

x

Object of classbrowseVignettes.

...

Further arguments, ignored by theprint method.

Details

FunctionbrowseVignettes returns an object of the same class;the print method displays it as an HTML page in a browser (usingbrowseURL).

See Also

browseURL,vignette

Examples

## List vignettes from all *attached* packagesbrowseVignettes(all=FALSE)## List vignettes from a specific packagebrowseVignettes("grid")

Send a Bug Report

Description

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.

Usage

bug.report(subject="",  address,           file="R.bug.report", package=NULL, lib.loc=NULL,...)

Arguments

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, orNULL.The default value ofNULL corresponds to all librariescurrently known.

...

additional named arguments such asmethod andccaddress to pass tocreate.post.

Details

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.

Value

Nothing useful.

When is there a bug?

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.

How to report a 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.

Author(s)

This help page is adapted from the Emacs manual and the R FAQ

See Also

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.


Send Output to a Character String or File

Description

Evaluates its arguments with the output being returned as a characterstring or sent to a file. Related tosink similarly to howwith is related toattach.

Usage

capture.output(..., file=NULL, append=FALSE,               type= c("output","message"), split=FALSE)

Arguments

...

Expressions to be evaluated.

file

A file name or aconnection, orNULL to returnthe output as a character vector. If the connection is not open,it will be opened initially and closed on exit.

append

logical. Iffile a file name or unopenedconnection, append or overwrite?

type,split

are passed tosink(), see there.

Details

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.

Value

A character string (iffile = NULL), or invisibleNULL.

See Also

sink,textConnection

Examples

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)

Detect which Files Have Changed

Description

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.

Usage

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,...)

Arguments

path

character vector; the path(s) to record.

file.info

logical; whether to recordfile.infovalues for each file.

timestamp

character string orNULL; the name of a fileto write at the time the snapshot is taken. This gives a quick testfor modification, but may be unreliable; see the Details.

md5sum

logical; whether MD5 summaries of each file should betaken as part of the snapshot.

digest

a function orNULL; a function with headerfunction(filename) which will take a vector of filenames andproduce a vector of values of the same length, or a matrix with thatnumber of rows.

full.names

logical; whether full names (as inlist.files) should be recorded. Must beTRUE iflength(path) > 1.

...

additional parameters to pass tolist.files to control the set of files in the snapshots.

before,after

objects produced byfileSnapshot; twosnapshots to compare. Ifafter is missing, a new snapshot ofthe current file system will be produced for comparison, usingarguments recorded inbefore as defaults.

check.file.info

character vector; which columns fromfile.info should be compared.

x

the object to print.

verbose

logical; whether to list all data when printing.

Details

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.

Value

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 normalizedpath from the call

timestamp,file.info,md5sum,digest,full.names

a record ofthe other arguments from the call

args

other arguments passed via... tolist.files.

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.TRUE indicates a change inthat 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.

Author(s)

Duncan Murdoch, using suggestions from Karl Millar and others.

See Also

file.info,file_test,md5sum.

Examples

# 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

Character Classification

Description

An interface to the (C99) wide character classification functions in use.

Usage

charClass(x, class)

Arguments

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.

Details

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.

Value

A logical vector of the length the number of characters or integers inx.

Note

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.

See Also

Character classes are used inregular expressions.

The OS'sman pages foriswctype andwctype.

Examples

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)

Choose a Folder Interactively on MS Windows

Description

Use a Windows shell folder widget to choose a folder interactively.

Usage

choose.dir(default="", caption="Select folder")

Arguments

default

which folder to show initially.

caption

the caption on the selection dialog.

Details

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.

Value

A length-one character vector, characterNA if‘Cancel’ was selected.

Note

This is only available on Windows.

See Also

choose.files (on Windows) andfile.choose(on all platforms).

Examples

if(interactive()&& .Platform$OS.type=="windows")        choose.dir(getwd(),"Choose a suitable folder")

Choose a List of Files Interactively on MS Windows

Description

Use a Windows file dialog to choose a list of zero or more filesinteractively.

Usage

choose.files(default="", caption="Select files",             multi=TRUE, filters= Filters,             index= nrow(Filters))Filters

Arguments

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

Details

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.

Value

A character vector giving zero or more file paths.

Note

This is only available on Windows.

See Also

file.choose,choose.dir.

Sys.glob orlist.files to select multiplefiles by pattern.

Examples

if(interactive()&& .Platform$OS.type=="windows")       choose.files(filters= Filters[c("zip","All"),])

Select a Bioconductor Mirror

Description

Interact with the user to choose a Bioconductor mirror.

Usage

chooseBioCmirror(graphics= getOption("menu.graphics"), ind=NULL,                 local.only=FALSE)

Arguments

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 atextmenu.

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.

Details

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.

Value

None: this function is invoked for itsside effect of updatingoptions("BioC_mirror").

See Also

setRepositories,chooseCRANmirror.


Select a CRAN Mirror

Description

Interact with the user to choose a CRAN mirror.

Usage

chooseCRANmirror(graphics= getOption("menu.graphics"), ind=NULL,                 local.only=FALSE)getCRANmirrors(all=FALSE, local.only=FALSE)

Arguments

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 atextmenu.

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.

Details

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").

HTTPS mirrors with mirroring overssh will be offered inpreference to other mirrors (which are listed in a sub-menu).

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’.

Value

None forchooseCRANmirror(), this function is invoked for itsside effect of updatingoptions("repos").

getCRANmirrors() returns a data frame with mirror information.

See Also

setRepositories,findCRANmirror,chooseBioCmirror,contrib.url.


Citing R and R Packages in Publications

Description

How to citeR andR packages in publications.

Usage

citation(package="base", lib.loc=NULL, auto=NULL)readCitationFile(file, meta=NULL)citHeader(...)citFooter(...)

Arguments

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 forpackage, orNULL. The default value ofNULL corresponds to alllibraries currently known. If the default is used, the loadedpackages are searched before the libraries.

auto

a logical indicating whether the default citationauto-generated from the package ‘DESCRIPTION’ metadata shouldbe used or not, orNULL (default), indicating that a‘CITATION’ file is used if it exists, or an object of class"packageDescription" with package metadata (seebelow).

file

a file name.

meta

a list of package metadata as obtained bypackageDescription, orNULL (the default).

...

character strings (which will bepasted).

Details

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.

Value

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.

See Also

bibentry

Examples

## 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 a Bibliography Entry

Description

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⁠’.

Usage

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)

Arguments

keys

A character vector of keys of entries to cite. May contain multiple keys ina single entry, separated by commas.

bib

A"bibentry" object containing the list of documents in whichto find the keys.

...

Additional arguments to pass to thecite() function for thedefault style.

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

Ifabbreviate == TRUE, whether to leave the first citation long.

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 whenabbreviate == TRUE andlongnamesfirst == TRUE

Details

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:

  1. The left delimiter.

  2. The right delimiter.

  3. The separator between references within a citation.

  4. An indicator of the “mode”:"n" for numbers,"s" for superscripts, anything else for author-year.

  5. Punctuation to go between the author and year.

  6. 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.

Value

A single element character string is returned, containing the citation.

Author(s)

Duncan Murdoch

Examples

## 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)

Bibliography Entries (Older Interface)

Description

Old interface providing functionality for specifying bibliographic information in enhanced BibTeX style. Since R 2.14.0 this has been superseded bybibentry.

Usage

citEntry(entry, textVersion=NULL, header=NULL, footer=NULL,...)

Arguments

entry

a character string with a BibTeX entry type.See sectionEntry Types inbibentry fordetails.

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.

...

forcitEntry, arguments of the formtag=value giving the fields of the entry, withtag andvalue the name and value of the field,respectively.See sectionEntry Fields inbibentry fordetails.

Value

citEntry produces an object of class"bibentry".

See Also

citation for more information about citing R and Rpackages and ‘CITATION’ files;bibentry for the newer functionality for representingand manipulating bibliographic information.


Read/Write to/from the Clipboard in MS Windows

Description

Transfer text between a character vector and the Windows clipboard inMS Windows (only).

Usage

getClipboardFormats(numeric=FALSE)readClipboard(format=13, raw=FALSE)writeClipboard(str, format=13)

Arguments

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.

Details

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.)

Value

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.

Note

This is only available on Windows.

See Also

file which can be used to set up a connection to a clipboard.


Close a Socket

Description

Closes the socket and frees the space in the file descriptor table. Theport may not be freed immediately.

Usage

close.socket(socket,...)

Arguments

socket

asocket object

...

further arguments passed to or from other methods.

Value

logical indicating success or failure

Author(s)

Thomas Lumley

See Also

make.socket,read.socket

Compiling in support for sockets was optional prior toR 3.3.0: seecapabilities("sockets") to see if it is available.


Generate All Combinations of n Elements, Taken m at a Time

Description

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.

Usage

combn(x, m, FUN=NULL, simplify=TRUE,...)

Arguments

x

vector source for combinations, or integern forx <-seq_len(n).

m

number of elements to choose.

FUN

function to be applied to each combination; defaultNULL means the identity, i.e., to return the combination(vector of lengthm).

simplify

logical indicating if the result should be simplifiedto anarray (typically amatrix); ifFALSE, the function returns alist. Note that whensimplify = TRUE as by default, the dimension of the result issimply determined fromFUN(1st combination) (forefficiency reasons). This will badly fail ifFUN(u) is not ofconstant length.

...

optionally, further arguments toFUN.

Details

Factorsx are accepted.

Value

Alist orarray, see thesimplifyargument above. In the latter case, the identitydim(combn(n, m)) == c(m, choose(n, m)) holds.

Author(s)

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).

References

Nijenhuis, A. and Wilf, H.S. (1978)Combinatorial Algorithms for Computers and Calculators;Academic Press, NY.

See Also

choose for fast computation of thenumber ofcombinations.expand.grid for creating a data frame fromall combinations of factors or vectors.

Examples

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

Description

Compare two package version numbers to see which is later.

Usage

compareVersion(a, b)

Arguments

a,b

Character strings representing package version numbers.

Details

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).

Value

0 if the numbers are equal,-1 ifb is laterand1 ifa is later (analogous to the C functionstrcmp).

Gives anR error on malformed inputs.

See Also

package_version,library,packageStatus.

Examples

compareVersion("1.0","1.0-1")compareVersion("7.2-0","7.1-12")

Compile Files for Use with R on Unix-alikes

Description

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.

Usage

R CMD COMPILE[options] srcfiles

Arguments

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.

Details

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.

Note

Some binary distributions ofR haveCOMPILE in a separatebundle, e.g. anR-devel RPM.

This is not available on Windows.

See Also

LINK,SHLIB,dyn.load

The section on “Customizing package compilation” inthe ‘R Administration and Installation’ manual:RShowDoc("R-admin").


Find Appropriate Paths in CRAN-like Repositories

Description

contrib.url adds the appropriate type-specific path within arepository to each URL inrepos.

Usage

contrib.url(repos, type= getOption("pkgType"))

Arguments

repos

character vector, the base URL(s) of the repositoriesto use.

type

character string, indicating which type of packages: seeinstall.packages.

Details

Iftype = "both" this will use the source repository.

Value

A character vector of the same length asrepos.

See Also

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 the Number of Fields per Line

Description

count.fields counts the number of fields, as separated bysep, in each of the lines offile read.

Usage

count.fields(file, sep="", quote="\"'", skip=0,             blank.lines.skip=TRUE, comment.char="#")

Arguments

file

a character string naming an ASCII data file, or aconnection, which will be opened if necessary,and if so closed at the end of the function call.

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: ifTRUE blank lines in theinput are ignored.

comment.char

character: a character vector of length onecontaining a single character or an empty string.

Details

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.

Value

A vector with the numbers of fields found.

See Also

read.table

Examples

fil<- tempfile()cat("NAME","1:John","2:Paul", file= fil, sep="\n")count.fields(fil, sep=":")unlink(fil)

Ancillary Function for Preparing Emails and Postings

Description

An ancillary function used bybug.report andhelp.request to prepare emails for submission to packagemaintainers or toR mailing lists.

Usage

create.post(instructions= character(), description="post",            subject="",            method= getOption("mailer"),            address="the relevant mailing list",            ccaddress= getOption("ccaddress",""),            filename="R.post", info= character())

Arguments

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"mailx" method.

method

Submission method, one of"none","mailto","gnudoit","ess" or (Unix only)"mailx".See ‘Details’.

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"mailx" and"mailto" methods.Useccaddress = "" for no copy.

filename

Filename to use for setting up the email (or storing it whenmethod is"none" or sending mail fails).

info

character vector of information to include in the templateemail below the ‘please do not edit the information below’ line.

Details

What this does depends on themethod. The function firstcreates a template email body.

none

A 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.

mailto

This 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.

gnudoit

An (X)emacs mail buffer is opened for the email to be edited andsent: this requires thegnudoit program to beavailable. Currentlysubject is ignored.

ess

The body of the template email is sent tostdout.

Value

InvisibleNULL.

See Also

bug.report,help.request.


Data Sets

Description

Loads specified data sets, or list the available data sets.

Usage

data(..., list= character(), package=NULL, lib.loc=NULL,     verbose= getOption("verbose"), envir= .GlobalEnv,     overwrite=TRUE)

Arguments

...

literal character strings or names.

list

a character vector.

package

a character vector giving the package(s) to lookin for data sets, orNULL.

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,orNULL. The default value ofNULL corresponds to alllibraries currently known.

verbose

a logical. IfTRUE, additional diagnostics areprinted.

envir

theenvironment where the data should be loaded.

overwrite

logical: should existing objects of the same name inenvir be replaced?

Details

Currently, four formats of data files are supported:

  1. 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.)

  2. files ending ‘.RData’ or ‘.rdata’ or ‘.rda’ areload()ed.

  3. files ending ‘.tab’, ‘.txt’ or ‘.TXT’ are readusingread.table(..., header = TRUE, as.is=FALSE),and henceresult in a data frame.

  4. 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).

Value

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.

Good practice

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.

Warning

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.

Note

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.

See Also

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.

Examples

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'

Spreadsheet Interface for Entering Data

Description

A spreadsheet-like editor for entering or editing data.

Usage

data.entry(..., Modes=NULL, Names=NULL)dataentry(data, modes)de(..., Modes= list(), Names=NULL)

Arguments

...

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 ofdata giving themodes of (some of) the variables.list() is allowed.

Details

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.

Value

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.

Resources

The data entry window responds to X resources of classR_dataentry. Resourcesforeground,background andgeometry are utilized.

Note

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).

See Also

vi,edit:edit usesdataentry to edit data frames.

Examples

# call data entry with variables x and y## Not run: data.entry(x, y)

Debug a Call

Description

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.

Usage

debugcall(call, once=FALSE)undebugcall(call)

Arguments

call

An R expression calling a function. The called functionwill be debugged. See Details.

once

logical; ifTRUE, debugging only occurs once, as viadebugonce. Defaults toFALSE

Details

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).

Value

debugcall invisibly returns the debugged call expression.

Note

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.

See Also

debug for the primary debugging interface

Examples

## Not run:## Evaluate call after setting debugging##f<- factor(1:10)res<- eval(debugcall(summary(f)))## End(Not run)

Post-Mortem Debugging

Description

Functions to dump the evaluation environments (frames) and to examinedumped frames.

Usage

dump.frames(dumpto="last.dump", to.file=FALSE,            include.GlobalEnv=FALSE)debugger(dump= last.dump)limitedLabels(value, maxwidth= getOption("width")-5L)

Arguments

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.GlobalEnv environment should be included in additionto thesys.frames(). Will be particularly useful whenused in a batch job.

dump

anR dump object created bydump.frames.

value

alist ofcalls to be formatted,e.g., for user menus.

maxwidth

optional length to which to trim the result oflimitedLabels();values smaller than 40 or larger than 1000 are winsorized.

Details

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.

Value

InvisibleNULL.

Note

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.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.

See Also

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.

Examples

## 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)

Demonstrations of R Functionality

Description

demo is a user-friendly interface to running some demonstrationR scripts.demo() gives the list of available topics.

Usage

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"))

Arguments

topic

the topic which should be demonstrated, given as aname or literal character string, or a character string,depending on whethercharacter.only isFALSE (default)orTRUE. If omitted, the list of available topics isdisplayed.

package

a character vector giving the packages to look into fordemos, orNULL. By default, all packages in the search pathare used.

lib.loc

a character vector of directory names ofR libraries,orNULL. The default value ofNULL corresponds to alllibraries currently known. If the default is used, the loadedpackages are searched before the libraries.

character.only

logical; ifTRUE, usetopic ascharacter string.

verbose

a logical. IfTRUE, additional diagnostics areprinted.

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 theknitrpackage is installed. Several other arguments are silently ignoredin that case, includinglib.loc.

echo

a logical. IfTRUE, show theR input when sourcing.

ask

a logical (or"default") indicating ifdevAskNewPage(ask = TRUE) should be called beforegraphical output happens from the demo code. The value"default" (the factory-fresh default) means to ask ifecho == TRUE and the graphics device appears to beinteractive. This parameter applies both to any currently openeddevice and to any devices opened by the demo code. If this isevaluated toTRUE and the session isinteractive, theuser is asked to press RETURN to start.

encoding

Seesource. If the package has adeclared encoding, that takes preference.

Details

If no topics are given,demo lists the available demos. Fortype = "console", the corresponding information is returned inan object of class"packageIQR".

See Also

source anddevAskNewPage whichare called bydemo.example to run codein the Examples section of help pages.

Examples

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")

DLL Version Information on MS Windows

Description

On MS Windows only, return the version of the package and the version ofR used tobuild the DLL, if available.

Usage

DLL.version(path)

Arguments

path

character vector of length one giving the complete path tothe DLL.

Value

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.

Note

This is only available on Windows.

Examples

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"))})

Download File from the Internet

Description

This function can be used to download a file from the Internet.

Usage

download.file(url, destfile, method, quiet=FALSE, mode="w",              cacheOK=TRUE,              extra= getOption("download.file.extra"),              headers=NULL,...)

Arguments

url

acharacter string (or longer vectorfor the"libcurl" method) naming the URL of a resource to bedownloaded.

destfile

a character string (or vector, see theurlargument) with the file path where the downloaded file is to besaved. Tilde-expansion is performed.

method

Method to be used for downloading files. Currentdownload methods are"internal","libcurl","wget","curl" and"wininet" (Windowsonly), and there is a value"auto": see ‘Details’ and‘Note’.

The method can also be set through the option"download.file.method": seeoptions().

quiet

IfTRUE, suppress status messages (if any), andthe progress bar.

mode

character. The mode with which to write the file. Usefulvalues are"w","wb" (binary),"a" (append) and"ab". Not used for methods"wget" and"curl".See also ‘Details’, notably about using"wb" for Windows.

cacheOK

logical. Is a server-side cached value acceptable?

extra

character vector of additional command-line arguments forthe"wget" and"curl" methods.

headers

named character vector of additional HTTP headers touse in HTTP[S] requests. It is ignored for non-HTTP[S] URLs. TheUser-Agent header taken from theHTTPUserAgent option(seeoptions) is automatically used as the first header.

...

allow additional arguments to be passed, unused.

Details

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:

On Windows

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.)

On a Unix-alike

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.

Value

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.

Setting Proxies

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.

Secure URLs

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).

Good practice

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.

⁠ftp://⁠’ URLs

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.

Note

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.

See Also

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.


Download Packages from CRAN-like Repositories

Description

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.

Usage

download.packages(pkgs, destdir, available=NULL,                  repos= getOption("repos"),                  contriburl= contrib.url(repos, type),                  method, type= getOption("pkgType"),...)

Arguments

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 byavailable.packageslisting packages available at the repositories, orNULL whichmakes an internal call toavailable.packages.

repos

character vector, the base URL(s) of the repositoriesto use, i.e., the URL of the CRAN master such as"https://cran.r-project.org" or ones of its mirrors,"https://cloud.r-project.org".

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 argumentrepos.

method

Download method, seedownload.file.

type

character string, indicate which type of packages: seeinstall.packages and ‘Details’.

...

additional arguments to be passed todownload.fileandavailable.packages.

Details

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.

Value

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.

See Also

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

Description

Invoke a text editor on anR object.

Usage

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="")

Arguments

name

a named object that you want to edit. For the defaultmethod, ifname is missing then the file specified byfile is opened for editing.

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, otherwisevi is used. On Windows it defaults to"internal", the script editor. On the macOS GUI the argumentis ignored and the document editor is always 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.

...

further arguments to be passed to or from methods.

Details

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.

Note

The functionsvi,emacs,pico,xemacs,xedit rely on the corresponding editor being available andbeing on the path. This is system-dependent.

See Also

edit.data.frame,data.entry,fix.

Examples

## 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)

Edit Data Frames and Matrices

Description

Use data editor on data frame or matrix contents.

Usage

## 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]]),...)

Arguments

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 withNULL row names.

...

further arguments passed to or from other methods.

Details

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.

Value

The edited data frame or matrix.

Note

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.

Author(s)

Peter Dalgaard

See Also

data.entry,edit

Examples

## Not run:edit(InsectSprays)edit(InsectSprays, factor.mode="numeric")## End(Not run)

Run an Examples Section from the Online Help

Description

Run all theR code from theExamples part ofR's online helptopictopic, with possible exceptions due to⁠\dontrun⁠,⁠\dontshow⁠, and⁠\donttest⁠ tags, see ‘Details’ below.

Usage

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())

Arguments

topic

name or literal character string: the onlinehelp topic the examples of which should be run.

package

a character vector giving the package names to lookinto for the topic, orNULL (the default), when all packages onthesearch path are used.

lib.loc

a character vector of directory names ofR libraries,orNULL. The default value ofNULL corresponds to alllibraries currently known. If the default is used, the loadedpackages are searched before the libraries.

character.only

a logical indicating whethertopic can beassumed to be a character string.

give.lines

logical: if true, thelines of the examplesource code are returned as a character vector.

local

logical: ifTRUE evaluate locally, ifFALSEevaluate in the workspace.

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 theknitrpackage is installed. Several other arguments are silently ignoredin that case, includingsetRNG andlib.loc.

echo

logical; ifTRUE, show theR input when sourcing.

verbose

logical; ifTRUE, show even more when runningexample code.

setRNG

logical or expression; if notFALSE, the randomnumber generator state is saved, then initialized to a specified state,the example is run and the (saved) state is restored.setRNG = TRUE sets the same state asR CMDcheck does forrunning a package's examples. This is currently equivalent tosetRNG = {RNGkind("default", "default", "default"); set.seed(1)}.

ask

logical (or"default") indicating ifdevAskNewPage(ask = TRUE) should be calledbefore graphical output happens from the example code. The value"default" (the factory-fresh default) means to ask ifecho is true and the graphics device appears to beinteractive. This parameter applies both to any currently openeddevice and to any devices opened by the example code.

prompt.prefix

character; prefixes the prompt to be used ifecho is true (as it is by default).

catch.aborts

logical, passed on tosource(),indicating that “abort”ing errors should be caught.

run.dontrun

logical indicating that⁠\dontrun⁠should be ignored, i.e., do run the enclosed code.

run.donttest

logical indicating that⁠\donttest⁠should be ignored, i.e., do run the enclosed code.

Details

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:

⁠\dontrun⁠

encloses code that should not be run.

⁠\dontshow⁠

encloses 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.

⁠\donttest⁠

encloses 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\ge 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.

Value

The value of the last evaluated expression, unlessgive.linesis true, where acharacter vector is returned.

Author(s)

Martin Maechler and others

See Also

demo

Examples

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)

Shell-style Tests on Files

Description

Utility for shell-style file tests.

Usage

file_test(op, x, y)

Arguments

op

a character string specifying the test to be performed.Unary tests (onlyx is used) are"-f" (existence and not being a directory),"-d" (existence and directory),"-L" or"-h" (existence and symbolic link),"-x" (executable as a file or searchable as a directory),"-w" (writable) and"-r" (readable).Binary tests are"-nt" (strictly newer than, using the modification dates) and"-ot" (strictly older than):in both cases the test is false unless both files exist.

x,y

character vectors giving file paths.

Details

‘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.

See Also

file.exists which only tests for existence(test -e on some systems) but not for not being a directory.

file.path,file.info

Examples

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

Description

Edit one or more files in a text editor.

Usage

file.edit(..., title= file, editor= getOption("editor"),          fileEncoding="")

Arguments

...

one or more character vectors containing the names of thefiles to be displayed. These will be tilde-expanded: seepath.expand.

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 forfile.

Details

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.

See Also

files,file.show,edit,fix,

Examples

## Not run:# open two R scripts for editingfile.edit("script1.R","script2.R")## End(Not run)

Find CRAN Mirror Preference

Description

Find out if a CRAN mirror has been selected for the current session.

Usage

findCRANmirror(type= c("src","web"))

Arguments

type

Is the mirror to be used for package sources or web information?

Details

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.

Value

A character string.

See Also

setRepositories,chooseCRANmirror

Examples

c(findCRANmirror("src"), findCRANmirror("web"))Sys.setenv(R_CRAN_WEB="https://cloud.r-project.org")c(findCRANmirror("src"), findCRANmirror("web"))

Find the Location of a Line of Source Code, or Set a Breakpoint There

Description

These functions locate objects containing particular lines of sourcecode, using the information saved when the code was parsed withkeep.source = TRUE.

Usage

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,...)

Arguments

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

IfTRUE (the default), we require only a matchtobasename(srcfile), not to the full path.

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 optionaltracer function to pass totrace. By default, a call tobrowseris inserted.

print

Theprint argument to pass totrace.

clear

IfTRUE, calluntrace rather thantrace.

...

Additional arguments to pass totrace.

Details

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.

Value

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.

Author(s)

Duncan Murdoch

See Also

trace

Examples

## 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 an Object

Description

fix invokesedit onx and then assigns the new(edited) version ofx in the user's workspace.

Usage

fix(x,...)

Arguments

x

the name of anR object, as a name or a character string.

...

arguments to pass to editor: seeedit.

Details

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.

See Also

edit,edit.data.frame

Examples

## 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)

Flush Output to a Console

Description

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.

Usage

flush.console()

Format Unordered and Ordered Lists

Description

Format unordered (itemize) and ordered (enumerate) lists.

Usage

formatUL(x, label="*", offset=0,         width=0.9* getOption("width"))formatOL(x, type="arabic", offset=0, start=1,         width=0.9* getOption("width"))

Arguments

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"arabic" (default), arabicnumerals are used. For"Alph" or"alph", single upperor lower case letters are employed (in this case, the number of thelast item must not exceed 26). Finally, for"Roman" or"roman", the labels are given as upper or lower case romannumerals (with the number of the last item maximally 3999).type can be given as a unique abbreviation of the above, oras one of theHTML style tokens"1" (arabic),"A"/"a" (alphabetic), or"I"/"i"(roman), respectively.

start

a positive integer specifying the starting number of thefirst item in an ordered list.

Value

A character vector with the formatted entries.

See Also

formatDL for formatting description lists.

Examples

## 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))

Retrieve an R Object, Including from a Namespace

Description

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.

Usage

getAnywhere(x)argsAnywhere(x)

Arguments

x

a character string or name.

Details

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.

Value

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.

See Also

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.

get,getFromNamespace,args

Examples

getAnywhere("format.dist")getAnywhere("simpleLoess")# not exported from statsargsAnywhere(format.dist)

Utility Functions for Developing Namespaces

Description

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’).

Usage

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),...)

Arguments

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: seeget.

envir

an alternative way to specify an environment to look in.

...

arguments to pass to the editor: seeedit.

Details

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.

Value

getFromNamespace returns the object found (or gives an error).

assignInNamespace,assignInMyNamespace andfixInNamespace are invoked for their side effect of changingthe object in the namespace.

Warning

assignInNamespace should not be used in final code, and will infuture throw an error if called from a package. Already certain usesare disallowed.

Note

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.

See Also

get,fix,getS3method

Examples

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)

Get Detailed Parse Information from Object

Description

If the"keep.source" option isTRUE,R's parserwill attach detailed information on the object it has parsed. Thesefunctions retrieve that information.

Usage

getParseData(x, includeText=NA)getParseText(parseData, id)

Arguments

x

an expression returned fromparse, or a function or otherobject with source reference information

includeText

logical; whether to include the text of parsed items in the result

parseData

a data frame returned fromgetParseData

id

a vector of item identifiers whose text is to be retrieved

Details

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.

Value

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"parse" ingetSrcLocation,which ignores⁠#line⁠ directives.

col1

integer. The column number where the item starts. The first characteris column 1. This corresponds to"column" ingetSrcLocation.

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. Theid of the parent of this item.

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. IfincludeText isTRUE, thetext of all tokens; if it isNA (the default), the text of terminaltokens. IfincludeText == FALSE, this column is not included.Very long strings (with source of 1000 characters or more) will not be stored;a message giving their length and delimiter will be included instead.

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.

Note

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.

Author(s)

Duncan Murdoch

References

Romain Francois (2012). parser: Detailed R source code parser. Rpackage version 0.0-16.https://github.com/halpo/parser.

See Also

parse,srcref

Examples

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 an S3 Method

Description

Get a method for an S3 generic, possibly from a namespace or thegeneric's registry.

Usage

getS3method(f, class, optional=FALSE, envir= parent.frame())

Arguments

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

theenvironment in which the method and itsgeneric are searched first.

Details

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.

Value

The function found, orNULL if no function is found andoptional = TRUE.

See Also

methods,get,getAnywhere

Examples

require(stats)exists("predict.ppr")# falsegetS3method("predict","ppr")

Get a Windows Handle

Description

Get the Windows handle of a window or of theR process in MS Windows.

Usage

getWindowsHandle(which="Console")

Arguments

which

a string (see below), or the number of a graphics devicewindow (which must awindows one).

Details

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.

Value

An external pointer holding the Windows handle, orNULL.

Note

This is only available on Windows.

See Also

getIdentification,getWindowsHandles

Examples

if(.Platform$OS.type=="windows")  print( getWindowsHandle())

Get handles of Windows in the MS Windows RGui

Description

This function gets the Windows handles of visible top level windows orwindows within theR MDI frame (when using theRgui).

Usage

getWindowsHandles(which="R", pattern="", minimized=FALSE)

Arguments

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.

Details

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.

Value

A list of external pointers containing the window handles.

Note

This is only available on Windows.

Author(s)

Duncan Murdoch

See Also

arrangeWindows,getWindowsHandle (singular).

Examples

if(.Platform$OS.type=="windows") withAutoprint({  getWindowsHandles()  getWindowsHandles("all")})

Change Wildcard or Globbing Pattern into Regular Expression

Description

Changewildcard akaglobbing patterns into thecorresponding regular expressions (regexp).

This is also a practical didactical example for the use ofsub() and regular expressions.

Usage

glob2rx(pattern, trim.head=FALSE, trim.tail=TRUE)

Arguments

pattern

character vector

trim.head

logical specifying if leading"^.*" should betrimmed from the result.

trim.tail

logical specifying if trailing".*$" should betrimmed from the result.

Details

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.

Value

A character vector of the same length as the inputpatternwhere each wildcard is translated to the correspondingregular expression.

Author(s)

Martin Maechler, Unix/sed based version, 1991; current: 2004

See Also

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.

Examples

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("*[*")=="^.*\\[")

Declarations Used in Checking a Package

Description

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.)

Usage

globalVariables(names, package, add=TRUE)suppressForeignCheck(names, package, add=TRUE)

Arguments

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 toglobalVariables orsuppressForeignCheck comes in the package's source file,the argument is normally omitted, as in the example below.

add

Should the contents ofnames be added to the current globalvariables or replace it?

Details

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.

Value

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.

Note

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.

Author(s)

John Chambers and Duncan Murdoch

See Also

dontCheck

Examples

## 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)

Hash Tables (Experimental)

Description

Create and manipulate mutable hash tables.

Usage

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,...)

Arguments

type

character string specifying the hash table 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 ifkey does not match.

value

new value to associate withkey.

FUN

afunction of two arguments, the key and the value, to callfor each entry.

x

object to be tested, printed, or formatted.

...

additional arguments.

Details

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.

Value

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.

Notes

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.

Examples

## 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"

Check for Name

Description

hasName is a convenient way to test for one or more namesin an R object.

Usage

hasName(x, name)

Arguments

x

Any object.

name

One or more character values to look for.

Details

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.

Value

A logical vector of the same length asname containingTRUE if the corresponding entry is innames(x).

See Also

%in%,exists

Examples

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")

Return the First or Last Parts of an Object

Description

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.

Usage

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)

Arguments

x

an object

n

an integer vector of length up todim(x) (or 1,for non-dimensioned objects). Alogical is silently coerced tointeger. Values specify the indices to beselected in the corresponding dimension (or along the length) of theobject. A positive value ofn[i] includes the first/lastn[i] indices in that dimension, while a negative valueexcludes the last/firstabs(n[i]), including all remainingindices.NA or non-specified values (whenlength(n) < length(dim(x))) select all indices in that dimension. Mustcontain at least one non-missing value.

keepnums

in each dimension, if no names in that dimension arepresent, create them using the indices included in that dimension.Ignored ifdim(x) isNULL or its length 1.

addrownums

deprecated -keepnums should be usedinstead. Taken as the value ofkeepnums if it is explicitlyset whenkeepnums is not.

...

arguments to be passed to or from other methods.

d

typicallydim(x), henceNULL or a (typically integer, short)vector.

Details

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.

Value

An object (usually) likex but generally smaller. Hence, forarrays, the result corresponds tox[.., drop=FALSE].Forftable objectsx, a transformedformat(x).

Note

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.

Author(s)

Patrick Burns, improved and corrected by R-Core. Negative argumentadded by Vincent Goulet. Multi-dimension support added by Gabriel Becker.

Examples

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})

Documentation

Description

help is the primary interface to the help systems.

Usage

help(topic, package=NULL, lib.loc=NULL,     verbose= getOption("verbose"),     try.all.packages= getOption("help.try.all.packages"),     help_type= getOption("help_type"))

Arguments

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 oftopic is a length-onecharacter vector the topic is taken to be the value of the onlyelement. Otherwisetopic must be a name or areservedword (if syntactically valid) or character string.

See ‘Details’ for what happens if this is omitted.

package

a name or character vector giving the packages to lookinto for documentation, orNULL. By default, all packageswhose namespaces are loaded are used. To avoid a name being deparsed use e.g.(pkg_ref) (see the examples).

lib.loc

a character vector of directory names ofR libraries,orNULL. The default value ofNULL corresponds to alllibraries currently known. If the default is used, the loadedpackages are searched before the libraries. This is not used forHTML help (see ‘Details’).

verbose

logical; ifTRUE, the file name is reported.

try.all.packages

logical; seeNote.

help_type

character string: the type of help required.Possible values are"text","html" and"pdf".Case is ignored, and partial matching is allowed.

Details

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’.

On Unix-alikes:

The ‘factory-fresh’ default is text help except from the macOSGUI, which uses HTML help displayed in its own browser window.

On Windows:

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().

Offline help

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.

Note

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).

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.

See Also

? 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.

Examples

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))

Send a Post to R-help

Description

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).

Usage

help.request(subject="",             address="[email protected]",             file="R.help.request",...)

Arguments

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 asmethod andccaddress to pass tocreate.post.

Details

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.

Value

Nothing useful.

Author(s)

Heather Turner, based on the then current code and help page ofbug.report().

See Also

The posting guide (https://www.r-project.org/posting-guide.html),alsosessionInfo() from which you may add to the help request.

create.post.


Search the Help System

Description

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.

Usage

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??pattern

Arguments

pattern

a character string to be matched in the specifiedfields. If this is given, the argumentsapropos,keyword, andwhatis are ignored.

fields

a character vector specifying the fields of the helpdatabase to be searched. The entries must be abbreviations of"name","title","alias","concept", and"keyword", corresponding to the help page's (file) name, itstitle, the topics and concepts it provides documentation for, andthe keywords it can be classified to. See below for details and howvignettes and demos are searched.

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. Ifkeyword is specified,agrep defaults toFALSE.

whatis

a character string to be matched inthe help page topics.

ignore.case

a logical. IfTRUE, case is ignored duringmatching; ifFALSE, pattern matching is case sensitive.

package

a character vector with the names of packages tosearch through, orNULL in which caseall availablepackages in the library trees specified bylib.loc aresearched.

lib.loc

a character vector describing the location ofRlibrary trees to search through, orNULL. The default valueofNULL corresponds to all libraries currently known.

help.db

a character string giving the file path to a previouslybuilt and saved help database, orNULL.

verbose

logical; ifTRUE, the search process is traced.Integer values are also accepted, withTRUE being equivalentto2, and1 being less verbose. On Windows a progressbar is shown during rebuilding, and on Unix a heartbeat is shown forverbose = 1 and a package-by-package list forverbose >= 2.

rebuild

a logical indicating whether the help database shouldbe rebuilt. This will be done automatically iflib.loc orthe search path is changed, or ifpackage is used and a valueis not found.

agrep

ifNULL (the default unlesskeyword isused) and the character string tobe matched consists of alphanumeric characters, whitespace or a dashonly, approximate (fuzzy) matching viaagrep is usedunless the string has fewer than 5 characters; otherwise, it istaken to contain aregular expression to be matched viagrep. IfFALSE, approximate matching is notused. Otherwise, one can give a numeric or a list specifying themaximal distance for the approximate match, see argumentmax.distance in the documentation foragrep.

use_UTF8

logical: should results be given in UTF-8 encoding?Also changes the meaning of regexps inagrep to be Perl regexps.

types

a character vector listing the types of documentationto search. The entries must be abbreviations of"vignette""help" or"demo". Results will be presented in theorder specified.

field

a single value offields to search.

Details

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.

Value

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.

See Also

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.

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

Hypertext Documentation

Description

Start the hypertext (currently HTML) version ofR's onlinedocumentation.

Usage

help.start(update=FALSE, gui="irrelevant",           browser= getOption("browser"), remote=NULL)

Arguments

update

logical: should this attempt to update the package index toreflect the currently available packages. (Not attempted ifremote is non-NULL.)

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.

Details

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.

See Also

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.

Examples

help.start()## the 'remote' arg can be tested byhelp.start(remote= paste0("file://", R.home()))

Help Search Utilities

Description

Utilities for searching the help system.

Usage

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())

Arguments

package

a character vector with the names of packages tosearch through, orNULL in which caseall availablepackages in the library trees specified bylib.loc aresearched.

lib.loc

a character vector describing the location ofRlibrary trees to search through, orNULL. The default valueofNULL corresponds to all libraries currently known.

types

a character vector listing the types of documentationto search.Seehelp.search for details.

verbose

a logical controlling the verbosity of building thehelp search database.Seehelp.search for details.

rebuild

a logical indicating whether the help search databaseshould be rebuilt.Seehelp.search for details.

use_UTF8

logical: should results be given in UTF-8 encoding?

db

a help search database as obtained by calls tohsearch_db().

Details

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.

Examples

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)

Install Add-on Packages

Description

Utility for installing add-on packages.

Usage

R CMD INSTALL[options][-l lib] pkgs

Arguments

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.UseR CMD INSTALL --help for the full current list of options.

Details

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.

Sub-architectures

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

Staged installation

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.

Note

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.

See Also

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.


Install Packages from Repositories or Local Files

Description

Download and install packages from CRAN-like repositories or fromlocal files.

Usage

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,...)

Arguments

pkgs

character vector of the names of packages whosecurrent versions should be downloaded from the repositories.

Ifrepos = NULL, a character vector of file paths,

on Windows,

file paths of ‘.zip’ files containing binary builds ofpackages. (‘⁠http://⁠’ and ‘⁠file://⁠’ URLs are also acceptedand the files will be downloaded and installed from local copies.)Source directories or file paths or URLs of archives may bespecified withtype = "source", but some packages needsuitable tools installed (see the ‘Details’ section).

On Unix-alikes,

these file paths can be source directories or archivesor binary package archive files (as created byR CMD build --binary). (‘⁠http://⁠’ and ‘⁠file://⁠’ URLs are alsoaccepted and the files will be downloaded and installed from localcopies.) On a CRAN build ofR for macOS these can be ‘.tgz’files containing binary package archives.Tilde-expansion will be done on file paths.

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.libPaths().

repos

character vector, the base URL(s) of the repositoriesto use, e.g., the URL of a CRAN mirror such as"https://cloud.r-project.org". For more details onsupported URL schemes seeurl.

Can beNULL to install from local files, directories or URLs:this will be inferred by extension frompkgs if of length one.

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 argumentrepos.Incompatible withtype = "both".

method

download method, seedownload.file.

available

a matrix as returned byavailable.packageslisting packages available at the repositories, orNULL whenthe function makes an internal call toavailable.packages.Incompatible withtype = "both".

destdir

directory where downloaded packages are stored. If it isNULL (the default) a subdirectorydownloaded_packages of the session temporarydirectory will be used (and the files will be deletedat the end of the session).

dependencies

logical indicating whether to also installuninstalled packages which these packages depend on/linkto/import/suggest (and so on recursively).Not used ifrepos = NULL.Can also be a character vector, a subset ofc("Depends", "Imports", "LinkingTo", "Suggests", "Enhances").

Only supported iflib is of length one (or missing),so it is unambiguous where to install the dependent packages. Ifthis is not the case it is ignored, with a warning.

The default,NA, meansc("Depends", "Imports", "LinkingTo").

TRUE means to usec("Depends", "Imports", "LinkingTo", "Suggests") forpkgs andc("Depends", "Imports", "LinkingTo") for added dependencies:this installs all the packages needed to runpkgs, theirexamples, tests and vignettes (if the package author specified themcorrectly).

In all of these,"LinkingTo" is omitted for binary packages.

type

character, indicating the type of package to download andinstall. Will be"source" except on Windows and some macOSbuilds: see the section on ‘Binary packages’ for those.

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 toR CMD INSTALL. If the character vector has names theseare assumed to identify values for--configure-args forindividual packages. This allows one to specify settings for anentire collection of packages which will be used if any of thosepackages are to be installed. (These settings can therefore bere-used and act as default settings.)

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 toconfigure.argsfor flag--configure-vars, which is used to set environmentvariables for theconfigure run.

clean

a logical value indicating whether to add the--clean flag to the call toR CMD INSTALL.This is sometimes used to perform additional operations at the endof the package installation in addition to removing intermediate files.

Ncpus

the number of parallel processes to use for a parallelinstall of more than one source package. Values greater than oneare supported if themake command specified bySys.getenv("MAKE", "make") accepts argument-k -j <Ncpus>.

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 alsoINSTALL_opts.) This can also be used on Windows to installjust the DLL(s) from a binary package, e.g. to add 64-bitDLLs to a 32-bit install.

INSTALL_opts

an optional character vector of additional option(s) to be passed toR CMD INSTALL for a source package install. E.g.,c("--html", "--no-multiarch", "--no-test-load") or, formacOS,"--dsym".

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 toavailable.packages() in case that is called, onpurpose.

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 todownload.file,available.packages, or to the functions for binaryinstalls on macOS and Windows (which accept an argument"lock":see the section on ‘Locking’).

Details

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.

Value

InvisibleNULL.

Binary packages

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,

on Windows,

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.

On Unix-alikes,

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.

Locking

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

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.

Timeouts

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.

Version requirements on source installs

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).

Note

On Unix-alikes:

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.

On Windows:

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.

See Also

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.

Examples

## 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 Installed Packages

Description

Find (or retrieve) details of all packages installed in the specifiedlibraries.

Usage

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,...)

Arguments

lib.loc

character vector describing the location ofR library trees tosearch through, orNULL for all known trees(see.libPaths).

priority

character vector orNULL (default). If non-null, used toselect packages;"high" is equivalent toc("base", "recommended"). To select all packages without anassigned priority usepriority = NA_character_.

noCache

do not use cached information, nor cache it.

cache_user_dir

logical indicating if caching shouldhappen intools'R_user_dir("base", "cache")instead oftempdir().

fields

a character vector giving the fields to extract fromeach package's ‘DESCRIPTION’ file in addition to the defaultones, orNULL (default). Unavailable fields result inNA values.

subarch

character string orNULL. If non-null andnon-empty, used to select packages which are installed for thatsub-architecture.

...

allows unused arguments to be passed down from other functions.

Details

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.

Value

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.

Note

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).

See Also

update.packages,install.packages,INSTALL,REMOVE.

Examples

## 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

Is 'method' the Name of an S3 Method?

Description

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.

Usage

isS3method(method, f, class, envir= parent.frame())

Arguments

method

a character string, typically of the form"fn.class". If omitted,f andclass haveto be specified instead.

f

optional character string, typically specifying an S3 genericfunction. Used, whenmethod is not specified.

class

optional character string, typically specifying an S3class name. Used, whenmethod is not specified.

envir

theenvironment in which the method and itsgeneric are searched first, as ingetS3method().

Value

logicalTRUE orFALSE

See Also

methods,getS3method.

Examples

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)

Check if a Function Acts as an S3 Generic

Description

Determines whetherf acts as a standard S3-style genericfunction.

Usage

isS3stdGeneric(f)

Arguments

f

a function object

Details

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.

Value

Iff is an S3 generic, a logical containingTRUEwith the name of the S3 generic (the string passed toUseMethod). Otherwise,FALSE (unnamed).


Create Executable Programs on Unix-alikes

Description

Front-end for creating executable programs on unix-alikes, i.e., noton Windows.

Usage

R CMD LINK[options] linkcmd

Arguments

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.

Details

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.

Note

Some binary distributions ofR haveLINK in a separatebundle, e.g. anR-devel RPM.

This is not available on Windows.

See Also

COMPILE.

Examples

## 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)

Select a Suitable Encoding Name from a Locale Name

Description

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.

Usage

localeToCharset(locale= Sys.getlocale("LC_CTYPE"))

Arguments

locale

character string naming a locale.

Details

The operation differs by OS.

On Windows,

a locale is specified like"English_United Kingdom.1252".The final component gives the codepage, and this defines the encoding.

On Unix-alikes:

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".

Value

A character vector naming an encoding and possibly a fallbacksingle-encoding,NA if unknown.

Note

The encoding names are those used bylibiconv, and ought alsoto work withglibc but maybe not with commercial Unixen.

See Also

Sys.getlocale,iconv.

Examples

localeToCharset()

List Objects and their Structure

Description

ls.str andlsf.str are variations oflsapplyingstr() to each matched name: see section Value.

Usage

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))

Arguments

pos

integer indicatingsearch path position, or-1 for the current environment.

name

optional name indicatingsearch pathposition, seels.

envir

environment to use, seels.

all.names

logical indicating if names which begin with a. are omitted; seels.

pattern

aregular expression passed tols.Only names matchingpattern are considered.

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; ifTRUE (default), show attributesas sub structures.

mode

character specifying themode of objects toconsider. Passed toexists andget.

x

an object of class"ls_str".

...

further arguments to pass.lsf.str passes them tols.str which passes them on tols. The(non-exported) print methodprint.ls_str passes them tostr.

digits

the number of significant digits to use for printing.

Value

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.

Author(s)

Martin Maechler

See Also

str,summary,args.

Examples

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 Package Maintainer

Description

Show the name and email address of the maintainer of an installed package.

Usage

maintainer(pkg)

Arguments

pkg

a character string, the name of an installed package.

Details

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.

Value

A character string giving the name and email address of the maintainerof the package, orNA_character_ if no such package is installed.

Author(s)

David Scott[email protected] from code on R-help originallydue to Charlie Sharpsteen[email protected]; multiple correctionsby R-core.

References

https://stat.ethz.ch/pipermail/r-help/2010-February/230027.html

See Also

packageDescription,bug.report

Examples

maintainer("MASS")

Update HTML Package List

Description

Re-create the HTML list of packages.

Usage

make.packages.html(lib.loc= .libPaths(), temp=FALSE,                   verbose=TRUE, docdir= R.home("doc"))

Arguments

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

Iftemp is false, directory in whose ‘html’directory the ‘packages.html’ file is to be created/updated.

Details

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.

Value

Invisible logical, withFALSE indicating a failure to createthe file, probably due to lack of suitable permissions.

See Also

help.start

Examples

## Not run:make.packages.html()# this can be slow for large numbers of installed packages.## End(Not run)

Create a Socket Connection

Description

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.

Usage

make.socket(host="localhost", port, fail=TRUE, server=FALSE)

Arguments

host

name of remote host

port

port to connect to/listen on

fail

failure to connect is an error?

server

a server socket?

Value

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.

Warning

I don't know if the connecting host name returnedwhenserver = TRUE can be trusted. I suspect not.

Author(s)

Thomas Lumley

References

Adapted from Luke Tierney's code forXLISP-Stat, in turnbased on code from Robbins and Robbins “Practical UNIX Programming”.

See Also

close.socket,read.socket.

Compiling in support for sockets was optional prior toR 3.3.0: seecapabilities("sockets") to see if it is available.

Examples

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 Interaction Function

Description

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⁠’.

Usage

menu(choices, graphics=FALSE, title=NULL)

Arguments

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.NULL is also accepted.

Details

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"".

Value

The number corresponding to the selected item, or 0 if no choice wasmade.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.

See Also

select.list, which is used to implement the graphicalmenu, and allows multiple selections.

Examples

## Not run:switch(menu(c("List letters","List LETTERS"))+1,       cat("Nothing done\n"), letters, LETTERS)## End(Not run)

List Methods for S3 Generic Functions or Classes

Description

List all available methods for a S3 and S4 generic function, or allmethods for an S3 or S4 class.

Usage

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"),...)

Arguments

generic.function

a generic function, or a character string naming ageneric function.

class

a symbol or character string naming a class: only used ifgeneric.function is not supplied.

envir

the environment in which to look for the definition ofthe generic function, when the generic function is passed as acharacter string.

all.names

alogical indicating if allobject names are returned. WhenFALSE as by default, namesbeginning with a ‘⁠.⁠’ are omitted.

dropPath

alogical indicating if thesearch() path, apart from.GlobalEnv andpackage:base (i.e.,baseenv()), should be skippedwhen searching for method definitions. The defaultFALSE isback compatible and typically desired forprint()ing, with orwithout asterisk;dropPath=TRUE has been hard coded inR 4.3.0and is faster for non-smallsearch() paths.

useEnv

alogical indicating ifenvir shouldbe searched in addition to the (much reduced in case ofdropPath=TRUE)search() path.

x

typically the result ofmethods(..), anR object of S3class"MethodsFunction", see ‘Value’ below.

byclass

an optionallogical allowing to overridethe"byclass" attribute determining how the result isprinted, see ‘Details’.

...

potentially further arguments passed to and from methods;unused currently.

Details

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).

Value

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:

generic

character vector of the names of the generic.

visible

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.

isS4

logical(), true when the method is an S4 method.

from

afactor, the location or package namewhere the method was found.

Note

The originalmethods function was written by Martin Maechler.

References

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.

See Also

S3Methods,class,getS3method.

For S4,getMethod,showMethods,Introduction orMethods_Details.

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}

Managing Repository Mirrors

Description

Functions helping to maintain CRAN, some of them may also be usefulfor administrators of other repository networks.

Usage

mirror2html(mirrors=NULL, file="mirrors.html",  head="mirrors-head.html", foot="mirrors-foot.html")checkCRAN(method)

Arguments

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, seedownload.file.

Details

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.


Recursively Modify Elements of a List

Description

Modifies a possibly nested list recursively by changing a subset ofelements at each level to match a second list.

Usage

modifyList(x, val, keep.null=FALSE)

Arguments

x

A namedlist, possibly empty.

val

A named list with components to replace correspondingcomponents inx or add new components.

keep.null

IfTRUE,NULL elements invalbecomeNULL elements inx. Otherwise, thecorresponding element, if present, is deleted fromx.

Value

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]]).

Author(s)

Deepayan Sarkar[email protected]

Examples

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 R or Package News Information

Description

Build and query the news data base forR or add-on packages.

Usage

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"),...)

Arguments

query

an optional expression for selecting news entries.

package

a character string giving the name of an installedadd-on package, or"R" or"R-3" or"R-2".

lib.loc

a character vector of directory names of R libraries,orNULL. The default value ofNULL corresponds to alllibraries currently known.

format

Not yet used.

reader

Not yet used.

db,x

a news db obtained fromnews().

doBrowse

logical specifying that the news should be opened inthe browser (bybrowseURL, accessible as viahelp.start) instead of printed to the console.

browser

the browser to be used, seebrowseURL.

...

potentially further arguments passed toprint().

Details

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.

Value

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).

NEWS Formats

inst/NEWS.Rd

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).

NEWS.md

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.

NEWS

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.

Examples

## 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)

Look up the IP Address by Hostname (on Unix-alikes)

Description

Interface to the systemgethostbyname, currently availableonly on unix-alikes, i.e., not on Windows.

Usage

nsl(hostname)

Arguments

hostname

the name of the host.

Details

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.

Value

The IP address, as a character string, orNULL if the call fails.

Examples

if(.Platform$OS.type=="unix")# includes Mac  print( nsl("www.r-project.org"))

Report the Space Allocated for an Object

Description

Provides an estimate of the memory that is being used to store anR object.

Usage

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,...)

Arguments

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 differentstandards are

standard = "legacy":

"b","Kb","Mb","Gb","Tb","Pb","B","KB","MB","GB","TB" and"PB".

standard = "IEC":

"B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB" and"YiB".

standard = "SI":

"B","kB","MB","GB","TB","PB","EB","ZB","YB","RB", and"QB".

For all standards,units = "auto" is also allowed.Ifstandard = "auto", any of the "legacy" andIECunits are allowed.See ‘Formatting and printing object sizes’ for details.

standard

the byte-size unit standard to be used. A characterstring, possibly abbreviated from"legacy","IEC","SI" and"auto". See ‘Formatting and printingobject sizes’ for details.

digits

the number of digits after the decimal point, passed toround.

...

arguments to be passed to or from other methods.

Details

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.

Value

An object of class"object_size" with a length-one double value,an estimate of the memory allocation attributable to the object in bytes.

Formatting and printing object sizes

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 sizelegacyIEC
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 sizeSI
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

Author(s)

R Core; Henrik Bengtsson for the non-legacystandards.

References

The wikipedia page,https://en.wikipedia.org/wiki/Binary_prefix,is extensive on the different standards, usages and their history.

See Also

Memory-limits for the design limitations on object size.

Examples

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])

Create a Skeleton for a New Source Package

Description

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.

Usage

package.skeleton(name="anRpackage", list,                 environment= .GlobalEnv,                 path=".", force=FALSE,                 code_files= character(), encoding="unknown")

Arguments

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 oflist,environment,orcode_files will be supplied. See ‘Details’.

environment

an environment where objects are looked for. See‘Details’.

path

path to put the package directory in.

force

IfFALSE will not overwrite an existing directory.

code_files

a character vector with the paths to R code files tobuild the package around. See ‘Details’.

encoding

optionally acharacter string with anencoding for an optional ‘⁠Encoding:⁠’ line in‘DESCRIPTION’ when non-ASCII characters will be used; typicallyone of"latin1","latin2", or"UTF-8"; see theWRE manual.

Details

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.

Value

Used for its side-effects.

References

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.

See Also

prompt,promptClass, andpromptMethods.

package_native_routine_registration_skeleton for helpingin preparing packages with compiled code.

Examples

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")

Package Description

Description

Parses and returns the ‘DESCRIPTION’ file of a package as a"packageDescription".

Utility functions return (transformed) parts of that.

Usage

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)

Arguments

pkg

a character string with the package name.

lib.loc

a character vector of directory names ofR libraries,orNULL. The default value ofNULL corresponds to alllibraries currently known. If the default is used, the loadedpackages and namespaces are searched before the libraries.

fields

a character vector giving the tags of fields to return(if other fields occur in the file they are ignored).

drop

IfTRUE and the length offields is 1, thena single character string with the value of the respective field isreturned instead of an object of class"packageDescription".

encoding

If there is anEncoding field, to what encodingshould re-encoding be attempted? IfNA, no re-encoding. Theother values are as used byiconv, so the default"" indicates the encoding of the current locale.

date.fields

character vector of field tags to be tried. Thefirst for whichas.Date(.) is notNAwill be returned. (Partly experimental, seeNote.)

tryFormats

date formats to try, seeas.Date.character().

desc

optionally, a namedlist with componentsnamed fromdate.fields; where the default is fine, acompletepackageDescription() maybe specified as well.

built

forasDateBuilt(), acharacterstring as frompackageDescription(*, fields="Built").

Details

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.

Value

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.

Note

The default behavior ofpackageDate(), notably fordate.fields, is somewhat experimental and may change.

See Also

read.dcf

Examples

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 time

Find Package Associated with an Environment

Description

Many environments are associated with a package; this functionattempts to determine that package.

Usage

packageName(env= parent.frame())

Arguments

env

The environment whose name we seek.

Details

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.

Value

A length one character vector containing the name of the package,orNULL if there is no name.

See Also

getPackageName is a more elaborate functionthat can construct a name if none is found.

Examples

packageName()packageName(environment(mean))

Package Management Tools

Description

Summarize information about installed packages and packagesavailable at various repositories, and automatically upgrade outdatedpackages.

Usage

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,...)

Arguments

lib.loc

a character vector describing the location ofRlibrary trees to search through, orNULL. The default valueofNULL corresponds to all libraries currently known.

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 (NULL) derives URLs fromoption"repos" andtype.

method

download method, seedownload.file.

type

type of package distribution:seeinstall.packages.

object

an object of class"packageStatus" as returned bypackageStatus.

ask

ifTRUE, the user is prompted which packages shouldbe upgraded and which not.

...

forpackageStatus: arguments to be passed toavailable.packages andinstalled.packages.
for theupgrade method, arguments to be passed toinstall.packages
for other methods: currently not used.

Details

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.

Value

An object of class"packageStatus". This is a list with twocomponents

inst

a data frame with columns as thematrix returned byinstalled.packages plus"Status", a factor withlevelsc("ok", "upgrade", "unavailable"). Only the newest version of each package is reported, in the first repository in which it appears.

avail

a data frame with columns as thematrix returned byavailable.packages plus"Status", a factor withlevelsc("installed", "not installed").

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.

See Also

installed.packages,available.packages

Examples

x<- packageStatus(repositories= contrib.url(findCRANmirror("web")))print(x)summary(x)## Not run:upgrade(x)x<- update(x)print(x)## End(Not run)

Invoke a Pager on an R Object

Description

Displays a representation of the object named byx in a pagerviafile.show.

Usage

page(x, method= c("dput","print"),...)

Arguments

x

AnR object, or a character string naming an object.

method

The default method is to dump the objectviadput. An alternative is to useprint andcapture the output to be shown in the pager. Can be abbreviated.

...

additional arguments fordput,print orfile.show (such astitle).

Details

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....

See Also

file.show,edit,fix.

To go to a new page when graphing, seeframe.

Examples

## 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)

Persons

Description

A class and utility methods for holding information about personslike name and email address.

Usage

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,...)

Arguments

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 theas.person generic;a character string for theas.person default method;an object of class"person" otherwise.

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"R" yielding formatting as R code and"md" yieldingMarkdown (with email addresses and ORCID iDs hyperlinked).

object

anR object inhering from class"person".

escape

a logical indicating whether non-ASCII characters shouldbe translated to LaTeX escape sequences.

Details

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.

Value

person() andas.person() return objects of class"person".

See Also

citation

Examples

## 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")))

Collections of Persons (Older Interface)

Description

Old interface providing functionality for information aboutcollections of persons. Since R 2.14.0person objectscan be combined with the correspondingc method whichsupersedes thepersonList function.

Usage

personList(...)as.personList(x)

Arguments

...

person objects (inheriting from class"person")

x

an object the elements of which are coercible viaas.person

Value

a person object (inheriting from class"person")

See Also

person for the new functionality for representing andmanipulating information about persons.


Utilities for Building and Checking Add-on Packages

Description

Utilities for checking whether the sources of anR add-on packagework correctly, and for building a source package from them.

Usage

R CMD check[options] pkgdirsR CMD build[options] pkgdirs

Arguments

pkgdirs

a list of names of directories with sources ofRadd-on packages. Forcheck these can also be the filenames ofcompressedtar archives with extension ‘.tar.gz’,‘.tgz’, ‘.tar.bz2’ or ‘.tar.xz’.

options

further options to control the processing, or forobtaining information about usage and version of the utility.

Details

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.)

Note

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.

See Also

The sections on ‘Checking and building packages’ and‘Processing documentation files’ in ‘Writing R Extensions’:RShowDoc("R-exts").


Trigger Event Handling

Description

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.

Usage

process.events()

Details

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.

Value

NULL is returned invisibly.

Author(s)

Duncan Murdoch

See Also

See ‘Writing R Extensions’ and the ‘R for Windows FAQ’for more discussion of theR_ProcessEvents function.


Produce Prototype of an R Documentation File

Description

Facilitate the constructing of files documentingR objects.

Usage

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,...)

Arguments

object

anR object, typically a function for the defaultmethod. Can bemissing whenname is specified.

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 isname followedby".Rd". Can also beNA (see below).

name

a character string specifying the name of the object.

force.function

a logical. IfTRUE, treatobjectas function in any case.

...

further arguments passed to or from other methods.

importedFrom

a character string naming the package fromwhichobject was imported. Defaults to the environmentofobject ifobject is a function.

importPage

a character string naming the help pagein the package from whichobject was imported.

Details

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.

Value

Iffilename isNA, a list-style representation of thedocumentation shell. Otherwise, the name of the file written to isreturned invisibly.

Warning

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.

Note

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.

Author(s)

Douglas Bates forprompt.data.frame

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.

See Also

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.

Examples

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)

Generate Outline Documentation for a Data Set

Description

Generates a shell of documentation for a data set.

Usage

promptData(object, filename=NULL, name=NULL)

Arguments

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 isname followedby".Rd". Can also beNA (see below).

name

a character string specifying the name of the object.

Details

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.

Value

Iffilename isNA, a list-style representation of thedocumentation shell. Otherwise, the name of the file written to isreturned invisibly.

See Also

prompt

Examples

promptData(sunspots)unlink("sunspots.Rd")

Generate a Shell for Documentation of a Package

Description

Generates a prototype of a package overview help pageusing Rd macros that dynamically extract informationfrom package metadata when building the package.

Usage

promptPackage(package, lib.loc=NULL, filename=NULL,              name=NULL, final=FALSE)

Arguments

package

acharacter string with the name of thepackage to be documented.

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 isname followedby".Rd". Can also beNA (see below).

name

a character string specifying the name of the help topic;defaults to"pkgname-package", which is the required⁠\alias⁠ for the overview help page.

final

a logical value indicating whether to attempt tocreate a usable version of the help topic, rather than just a shell.

Details

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).

Value

Iffilename isNA, a list-style representation of thedocumentation shell. Otherwise, the name of the file written to isreturned invisibly.

See Also

prompt,package.skeleton

Examples

filename<- tempfile()promptPackage("utils", filename= filename)file.show(filename)unlink(filename)

Documentation Shortcuts

Description

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.

Usage

?topictype?topic

Arguments

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 callspkg::topic andpkg:::topic are treated specially, and look forhelp ontopic in packagepkg.

type

the special type of documentation to use for this topic;for example, typepackage will look for the overview help pageof a package namedtopic, and if the type isclass,documentation is provided for the S4 class with nametopic.See the Section ‘S4 Method Documentation’ for the uses oftype to get help on formal methods, includingmethods?function andmethod?call.

Details

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.

S4 Method Documentation

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.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.

See Also

help

?? for finding help pages on a vague topic.

Examples

?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)

A Completion Generator for R

Description

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.

Usage

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)

Arguments

ops

Logical flag. Activates completion after the$ and@ operators.

ns

Logical flag. Controls namespace related completions.

args

Logical flag. Enables completion of function arguments.

dots

Logical flag. If disabled, drops... from list offunction arguments. Relevant only ifargs is enabled.

func

Logical flag. Enables detection of functions. Ifenabled, a customizable extension ("(" by default) isappended to function names. The process of determining whether apotential completion is a function requires evaluation, includingfor lazy loaded symbols. This is undesirable for large objects,because of potentially wasteful use of memory in addition to thetime overhead associated with loading. For this reason, thisfeature is disabled by default.

S3

Logical flag. Whenargs = TRUE, activates completion onarguments of all S3 methods (otherwise just the generic, whichusually has very few arguments).

ipck

Logical flag. Enables completion of installed package namesinsidelibrary andrequire.

data

Logical flag. Enables completion of data sets (includingthose already visible) insidedata.

help

Logical flag. Enables completion of help requests startingwith a question mark, by looking inside help index files.

argdb

Logical flag. Whenargs = TRUE, completion isattempted on function arguments. Generally, the list of validarguments is determined by dynamic calls toargs.While this gives results that are technically correct, the use ofthe... argument often hides some useful arguments. Togive more flexibility in this regard, an optional table of validarguments names for specific functions is retained internally.Settingargdb = TRUE enables preferential lookup in thisinternal data base for functions with an entry in it. Of course,this is useful only when the data base contains information aboutthe function of interest. Some functions are already included, andmore can be added by the user through the unexported function.addFunctionInfo (see below).

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. InfindMatches, this argument defaults tothe current setting.

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. InfindMatches, this argument defaults to the current setting.

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?), help completion is invoked. Setting this toFALSE relegates completion to the underlying completionfront-end, which may do its own processing (for example,readline on Unix-alikes will do filename completion).

files

Logical flag. Deprecated. Usequotes instead.

name,...

user-settable options. Currently valid names are

function.suffix:

default"("

funarg.suffix:

default"="

package.suffix

default"::"

Usage is similar to that ofoptions.

x

An R object for which valid names after"$"are computed and returned.

pattern

A regular expression. Only matching names arereturned.

values

character string giving set of candidate valuesin which matches are to be found.

Details

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.

Completion after extractors$ 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.

Completion inside namespaces:

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.

Completion for help items:

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.

Completion of function arguments:

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.

Special functions:

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.

Value

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.completeToken, as a character vector.

token

The token that was (or, is to be) completed, asset by the last call to.assignToken (possibly inside a callto.guessTokenFromLine).

linebuffer

The full line, as set by the last call to.assignLinebuffer.

start

The start position of the token in the linebuffer, as set by the last call to.assignStart.

end

The end position of the token in the linebuffer, as set by the last call to.assignEnd.

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.

Unexported API

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.

Note

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.

Author(s)

Deepayan Sarkar,[email protected]


Data Input from Spreadsheet

Description

Reads a file in Data Interchange Format (DIF) and creates adata frame from it.DIF is a format for data matrices such assingle spreadsheets.

Usage

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="")

Arguments

file

the name of the file which the data are to be read from,or aconnection, or a complete URL.

The name"clipboard" may also be used on Windows, in whichcaseread.DIF("clipboard") will look for aDIF format entryin the Windows clipboard.

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:header is set toTRUEif and only if the first row contains only character values andthe top left cell is empty.

dec

the character used in the file for decimal points.

numerals

string indicating how to convert numbers whose conversionto double precision would lose accuracy, seetype.convert.

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 ifrow.names is missing, the rows arenumbered.

Usingrow.names = NULL forces row numbering.

col.names

a vector of optional names for the variables.The default is to use"V" followed by the column number.

as.is

controls conversion of character variables (insofar asthey are not converted to logical, numeric or complex) to factors,if not otherwise specified bycolClasses.Its value is either a vector of logicals (values are recycled ifnecessary), or a vector of numeric or character indices whichspecify which columns should not be converted to factors.

Note: In releases prior toR 2.12.1, cells marked as being ofcharacter type were converted to logical, numeric or complex usingtype.convert as inread.table.

Note: to suppress all conversions including those of numericcolumns, setcolClasses = "character".

Note thatas.is is specified per column (not pervariable) and so includes the column of row names (if any) and anycolumns to be skipped.

na.strings

a character vector of strings which are to beinterpreted asNA values. Blank fields are alsoconsidered to be missing values in logical, integer, numeric andcomplex fields.

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 beNA.

Possible values areNA (whentype.convert isused),"NULL" (when the column is skipped), one of the atomicvector classes (logical, integer, numeric, complex, character, raw),or"factor","Date" or"POSIXct". Otherwisethere needs to be anas method (from packagemethods)for conversion from"character" to the specified formalclass.

Note thatcolClasses is specified per column (not pervariable) and so includes the column of row names (if any).

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. IfTRUE then the names of thevariables in the data frame are checked to ensure that they aresyntactically valid variable names. If necessary they are adjusted(bymake.names) so that they are, and also to ensurethat there are no duplicates.

blank.lines.skip

logical: ifTRUE blank lines in theinput are ignored.

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 wouldneedtranspose = TRUE to be read correctly.

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 forfile, the ‘R Data Import/Export’manual and ‘Note’.

Value

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.

Note

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.

Author(s)

R Core;transpose option by Christoph Buser, ETH Zurich

References

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.

See Also

TheR Data Import/Export manual.

scan,type.convert,read.fwf for readingfixedwidthformatted input;read.table;data.frame.

Examples

## 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 in a Fortran-like Style

Description

Read fixed-format data files using Fortran-style format specifications.

Usage

read.fortran(file, format,..., as.is=TRUE, colClasses=NA)

Arguments

file

File orconnection to read from.

format

Character vector or list of vectors. See‘Details’ below.

...

Other arguments forread.fwf.

as.is

Keep characters as characters?

colClasses

Variable classes to override defaults. Seeread.table for details.

Details

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.

Value

A data frame

Note

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.

See Also

read.fwf,read.table,read.csv

Examples

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 Fixed Width Format Files

Description

Read a table offixedwidthformatteddata into adata.frame.

Usage

read.fwf(file, widths, header=FALSE, sep="\t",         skip=0, row.names, col.names, n=-1,         buffersize=2000, fileEncoding="",...)

Arguments

file

the name of the file which the data are to be read from.

Alternatively,file can be aconnection, whichwill be opened if necessary, and if so closed at the end of thefunction call.

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 bysep.

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; seeread.table.

row.names

seeread.table.

col.names

seeread.table.

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 forfile, the ‘R Data Import/Export’ manual and‘Note’.

...

further arguments to be passed toread.table. Useful such arguments includeas.is,na.strings,colClasses andstrip.white.

Details

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.

Value

Adata.frame as produced byread.tablewhich is called internally.

Author(s)

Brian Ripley forR version: originally inPerl by Kurt Hornik.

See Also

scan andread.table.

read.fortran for another style of fixed-format files.

Examples

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 from or Write to a Socket

Description

read.socket reads a string from the specified socket,write.socket writes to the specified socket. There is verylittle error checking done by either.

Usage

read.socket(socket, maxlen=256L, loop=FALSE)write.socket(socket, string)

Arguments

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.

Value

read.socket returns the string read as a length-one character vector.

write.socket returns the number of bytes written.

Author(s)

Thomas Lumley

See Also

close.socket,make.socket

Examples

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)

Data Input

Description

Reads a file in table format and creates a data frame from it, withcases corresponding to lines and variables to fields in the file.

Usage

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="",...)

Arguments

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,getwd(). Tilde-expansion is performed where supported.This can be a compressed file (seefile).

Alternatively,file can be a readable text-modeconnection (which will be opened for reading ifnecessary, and if soclosed (and hence destroyed) atthe end of the function call). (Ifstdin() is used,the prompts for lines may be somewhat confusing. Terminate inputwith a blank line or anEOF signal,Ctrl-D on Unix andCtrl-Z on Windows. Any pushback onstdin() will becleared before return.)

file can also be a complete URL. (For the supported URLschemes, see the ‘URLs’ section of the help forurl.)

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:header is set toTRUEif and only if the first row contains one fewer field than thenumber of columns.

sep

the field separator character. Values on each line of thefile are separated by this character. Ifsep = "" (thedefault forread.table) the separator is ‘white space’,that is one or more spaces, tabs, newlines or carriage returns.

quote

the set of quoting characters. To disable quotingaltogether, usequote = "". Seescan for thebehaviour on quotes embedded in quotes. Quoting is only consideredfor columns read as character, which is all of them unlesscolClasses is specified.

dec

the character used in the file for decimal points.

numerals

string indicating how to convert numbers whose conversionto double precision would lose accuracy, seetype.convert.Can be abbreviated. (Applies also to complex-number inputs.)

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 ifrow.names is missing, the rows arenumbered.

Usingrow.names = NULL forces row numbering. Missing orNULLrow.names generate row names that are consideredto be ‘automatic’ (and not preserved byas.matrix).

col.names

a vector of optional names for the variables.The default is to use"V" followed by the column number.

as.is

controls conversion of character variables (insofar asthey are not converted to logical, numeric or complex) to factors,if not otherwise specified bycolClasses.Its value is either a vector of logicals (values are recycled ifnecessary), or a vector of numeric or character indices whichspecify which columns should not be converted to factors.

Note: to suppress all conversions including those of numericcolumns, setcolClasses = "character".

Note thatas.is is specified per column (not pervariable) and so includes the column of row names (if any) and anycolumns to be skipped.

tryLogical

alogical determining if columnsconsisting entirely of"F","T","FALSE", and"TRUE" should be converted tological; passed totype.convert, true by default.

na.strings

a character vector of strings which are to beinterpreted asNA values. Blank fields are alsoconsidered to be missing values in logical, integer, numeric andcomplex fields. Note that the test happensafterwhite space is stripped from the input (if enabled), sona.stringsvalues may need their own white space stripped in advance.

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 beNA.

Possible values areNA (the default, whentype.convert is used),"NULL" (when the columnis skipped), one of the atomic vector classes (logical, integer,numeric, complex, character, raw), or"factor","Date"or"POSIXct". Otherwise there needs to be anasmethod (from packagemethods) for conversion from"character" to the specified formal class.

Note thatcolClasses is specified per column (not pervariable) and so includes the column of row names (if any).

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. IfTRUE then the names of thevariables in the data frame are checked to ensure that they aresyntactically valid variable names. If necessary they are adjusted(bymake.names) so that they are, and also to ensurethat there are no duplicates.

fill

logical. IfTRUE then in case the rows have unequallength, blank fields are implicitly added. See ‘Details’.

strip.white

logical. Used only whensep hasbeen specified, and allows the stripping of leading and trailingwhite space from unquotedcharacter fields (numeric fieldsare always stripped). Seescan for further details(including the exact meaning of ‘white space’),remembering that the columns may include the row names.

blank.lines.skip

logical: ifTRUE blank lines in theinput are ignored.

comment.char

character: a character vector of length onecontaining a single character or an empty string. Use"" toturn off the interpretation of comments altogether.

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 seescan.

flush

logical: ifTRUE,scan will flush to theend of the line after reading the last of the fields requested.This allows putting comments after the last field.

stringsAsFactors

logical: should character vectors be convertedto factors? Note that this is overridden byas.is andcolClasses, both of which allow finer control.

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 forfile, the ‘R Data Import/Export’ manual and‘Note’.

encoding

encoding to be assumed for input strings. It isused to mark character strings as known to be inLatin-1 or UTF-8 (seeEncoding): it is not used tore-encode the input, but allowsR to handle encoded strings intheir native encoding (if one of those two). See ‘Value’and ‘Note’.

text

character string: iffile is not supplied and this is,then data are read from the value oftext via a text connection.Notice that a literal string can be used to include (small) data setswithin R code.

skipNul

logical: shouldNULs be skipped?

...

Further arguments to be passed toread.table.

Details

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.

Value

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".

CSV files

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.

Memory usage

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.

Note

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.

References

Chambers, J. M. (1992)Data for models.Chapter 3 ofStatistical Models in Seds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.

See Also

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).

Examples

## 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")

Read a Windows Registry Hive

Description

On Windows, read values of keys in the Windows Registry, andoptionally whole hives.

Usage

readRegistry(key, hive= c("HLM","HCR","HCU","HU","HCC","HPD"),             maxdepth=1, view= c("default","32-bit","64-bit"))

Arguments

key

character string, the path to the key in the Windows Registry.

hive

The ‘hive’ containing the key. The abbreviationsare forHKEY_LOCAL_MACHINE,HKEY_CLASSES_ROOT.HKEY_CURRENT_USER,HKEY_USERS,HKEY_CURRENT_CONFIG andHKEY_PERFORMANCE_DATA

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’.

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.

Value

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.

Note

This is only available on Windows.

Examples

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)

Browsing after an Error

Description

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.

Usage

recover()

Details

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).

Value

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.

Compatibility Note

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.

References

John M. Chambers (1998).Programming with Data; Springer.
See the compatibility note above, however.

See Also

browser for details about the interactive computations;options for setting the error option;dump.frames to save the current environments for laterdebugging.

Examples

## 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)

Allow Re-Listing an unlist()ed Object

Description

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".

Usage

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)

Arguments

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 ofx?

use.names

logical. Should names be preserved?

Details

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.

Value

an object of (S3) class"relistable" (and"list").

Author(s)

R Core, based on a code proposal by Andrew Clausen.

See Also

unlist

Examples

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))

Remove Add-on Packages

Description

Utility for removing add-on packages.

Usage

R CMD REMOVE[options][-l lib] pkgs

Arguments

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.

Details

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.

Note

Some binary distributions ofR haveREMOVE in a separatebundle, e.g. anR-devel RPM.

See Also

INSTALL,remove.packages


Remove Installed Packages

Description

Removes installed packages/bundles and updates index informationas necessary.

Usage

remove.packages(pkgs, lib)

Arguments

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.libPaths().

See Also

On Unix-alikes,REMOVE for a command line version;

install.packages for installing packages.


Remove Stored Source from a Function or Language Object

Description

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().

Usage

removeSource(fn)

Arguments

fn

afunction or another language object(fulfillingis.language) from which to remove thesource.

Details

This removes the"srcref" and related attributes, viarecursive cleaning ofbody(fn) in the case of a functionor the recursive language parts, otherwise.

Value

A copy of thefn object with the source removed.

See Also

is.language about language objects.

srcref for a description of source reference records,deparse for a description of how functions are deparsed.

Examples

## 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

R Home Directory

Description

Returns the location of theR home directory, which is the root ofthe installedR tree.

Usage

R RHOME

Roman Numerals

Description

Simple manipulation of (a small set of) integer numbers as roman numerals.

Usage

as.roman(x).romansr1+ r2r1<= r2max(r1)sum(r2)

Arguments

x

a numeric or character vector of arabic or roman numerals.

r1,r2

a roman number vector, i.e., ofclass"roman".

Details

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.

References

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.

Examples

## 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 Profiling of R's Execution

Description

Enable or disable profiling of the execution ofR expressions.

Usage

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"))

Arguments

filename

The file to be used for recording the profiling results.Set toNULL or"" to disable profiling.

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,"elapsed" for elapsed (real, wall-clock) time and"cpu"for CPU time, both measured in seconds."default" is the defaultevent on the platform, one of the two. See the ‘Details’.

Details

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.

Filtering Out Call Frames

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()

Note

On Unix-alikes:

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).

On Windows:

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.

See Also

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.

Examples

## 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 Profiling of R's Memory Use

Description

Enable or disable reporting of memory allocation in R.

Usage

Rprofmem(filename="Rprofmem.out", append=FALSE, threshold=0)

Arguments

filename

The file to be used for recording the memoryallocations. Set toNULL or"" to disable reporting.

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.

Details

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.

Value

None

Note

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.

See Also

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.

Examples

## 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)

Scripting Front-End for R

Description

This is an alternative front end for use in ‘⁠#!⁠’ scripts andother scripting applications.

Usage

Rscript[options] file[args]Rscript[options]-e expr[-e expr2...][args]

Arguments

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 infile orexpressions supplied via-e.

Details

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

--verbose

gives details of whatRscript isdoing.

--default-packages=list

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.

Note

Rscript is only supported on systems with theexecvsystem call.

Examples

## 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)

Show R Manuals and Other Documentation

Description

Utility function to find and displayR documentation.

Usage

RShowDoc(what, type= c("pdf","html","txt"), package)

Arguments

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.

Details

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).

Value

A invisible character string given the path to the file found.

See Also

For displaying regular help files,help (or?) andhelp.start.

Fortype = "txt",file.show is used.vignettes are nicely viewed viaRShowDoc(*, package= . ).

Examples

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 Documentation

Description

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.

Usage

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"))

Arguments

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:functions for help pages of CRAN packages,descriptions for extended descriptions of CRAN packages,news for package NEWS,Rfunctions for help pages of R base packages,Rmanuals for R manuals,READMEs for ‘README’ files of CRAN packages,views for task views,vignettes for package vignettes.

format

deprecated.

sortby

character string (can be abbreviated) indicating how tosort the search results:
(score,date:late for sorting by date with latest results first,date:early for earliest first,subject for captions in alphabetical order,subject:descending for reverse alphabetical order,size orsize:descending for size.)

matchesPerPage

How many items to show per page.

words

Show results matchingall words/phrases (default) orany of them.

Details

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.

Value

(Invisibly) the complete URL passed to the browser,including the query string.

Author(s)

Andy Liaw and Jonathan Baron and Gennadiy Starostin

See Also

help.search,help.start for local searches.

browseURL for how the help file is displayed.

Examples

# 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

An Etags-like Tagging Utility for R

Description

rtags provides etags-like indexing capabilities for R code,using R's own parser.

Usage

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"))

Arguments

path,pattern,recursive

Arguments passed on tolist.files to determine thefiles to be tagged. By default, these are all files with extension‘.R’, ‘.r’, ‘.S’, and ‘.s’ in the currentdirectory. These arguments are ignored ifsrc is specified.

src

A vector of file names to be indexed.

keep.re

A regular expression further restrictingsrc(the files to be indexed). For example, specifyingkeep.re = "/R/[^/]*\\.R$" will only retain files withextension ‘.R’ inside a directory named ‘R’.

ofile

Passed on tocat as thefileargument; typically the output file where the tags will be written("TAGS" or"tags" by convention). By default, theoutput is written to the R console (unless redirected).

append

Logical, indicating whether the output should overwritean existing file, or append to it.

verbose

Logical. IfTRUE, file names are echoed to theR console as they are processed.

type

Character string specifying whether emacs style("etags") or vi style ("ctags") tags are to begenerated.

Details

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.

Author(s)

Deepayan Sarkar

References

https://en.wikipedia.org/wiki/Ctags,https://www.gnu.org/software/emacs/manual/html_node/emacs/Tags-Tables.html

See Also

list.files,cat

Examples

## Not run:rtags("/path/to/src/repository",      pattern="[.]*\\.[RrSs]$",      keep.re="/R/",      verbose=TRUE,      ofile="TAGS",      append=FALSE,      recursive=TRUE)## End(Not run)

R Driver forStangle

Description

A driver forStangle that extractsR code chunks.Notably allRtangleSetup() arguments may be used as argumentsin theStangle() call.

Usage

Rtangle()RtangleSetup(file, syntax, output=NULL, annotate=TRUE,             split=FALSE, quiet=FALSE, drop.evalFALSE=FALSE,...)

Arguments

file

name of Sweave source file. See the description of thecorresponding argument ofSweave.

syntax

an object of classSweaveSyntax.

output

name of output file used unlesssplit = TRUE:see ‘Details’.

annotate

a logical orfunction. When true, as bydefault, code chunks are separated by comment lines specifying thenames and line numbers of the code chunks.IfFALSE the decorating comments are omitted. Alternatively,annotate may be a function, see section ‘Chunk annotation’.

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 withoptioneval = FALSE arecommented out in the output;otherwise (drop.evalFALSE = TRUE) they are omitted entirely.

...

additional named arguments setting defaults for furtheroptions listed in ‘Supported Options’.

Details

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.

Chunk annotation (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.

Supported Options

Rtangle supports the following options for code chunks (thevalues in parentheses show the default values):

engine:

character string ("R"). Only chunks withengine equal to"R" or"S" are processed.

keep.source:

logical (TRUE). Ifkeep.source == TRUE the original source is copied to thefile. Otherwise, deparsed source is output.

eval:

logical (TRUE). IfFALSE, the code chunkis copied across but commented out.

prefix

Used ifsplit = TRUE. Seeprefix.string.

prefix.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.

show.line.nos

logical (FALSE). Should the output beannotated with comments showing the line number of the first code lineof the chunk?

Author(s)

Friedrich Leisch and R-core.

See Also

Sweave User Manual’, a vignette intheutils package.

Sweave,RweaveLatex

Examples

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")

R/LaTeX Driver for Sweave

Description

A driver forSweave that translatesR code chunks inLaTeX files by “running them”, i.e.,parse() andeval() each.

Usage

RweaveLatex()RweaveLatexSetup(file, syntax, output=NULL, quiet=FALSE,                 debug=FALSE, stylepath,...)

Arguments

file

Name of Sweave source file. See the description of thecorresponding argument ofSweave.

syntax

An object of classSweaveSyntax.

output

Name of output file. The default is to remove extension‘.nw’, ‘.Rnw’ or ‘.Snw’ and to addextension ‘.tex’. Any directory paths infile are also removed such that the output iscreated in the current working directory.

quiet

IfTRUE all progress messages are suppressed.

debug

IfTRUE, input and output of all codechunks is copied to the console.

stylepath

See ‘Details’.

...

named values for the options listed in ‘SupportedOptions’.

Details

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.

Supported Options

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.

engine:

character string ("R"). Only chunks withengine equal to"R" or"S" are processed.

echo:

logical (TRUE). IncludeR code in the outputfile?

keep.source:

logical (TRUE). When echoing, ifTRUE the original source is copied to the file. Otherwise,deparsed source is echoed.

eval:

logical (TRUE). IfFALSE, the code chunkis not evaluated, and hence no text nor graphical outputproduced.

results:

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.

print:

logical (FALSE). IfTRUE, this forcesauto-printing of all expressions.

term:

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.

split:

logical (FALSE). IfTRUE, text outputis written to separate files for each code chunk.

strip.white:

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.

prefix:

logical (TRUE). IfTRUE generatedfilenames of figures and output all have the common prefix givenby theprefix.string option: otherwise only unlabelledchunks use the prefix.

prefix.string:

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.

include:

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).

fig:

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.

eps:

logical (FALSE), indicating whether EPS figuresshould be generated. Ignored iffig = FALSE.

pdf:

logical (TRUE), indicating whether PDF figuresshould be generated. Ignored iffig = FALSE.

pdf.version, pdf.encoding, pdf.compress:

passed topdf to set the version, encoding and compression (ornot). Defaults taken frompdf.options().

png:

logical (FALSE), indicating whether PNG figuresshould be generated. Ignored iffig = FALSE.Only available inR2.13.0\R \ge 2.13.0.

jpeg:

logical (FALSE), indicating whether JPEG figuresshould be generated. Ignored iffig = FALSE.Only available inR2.13.0\R \ge 2.13.0.

grdevice:

character (NULL): see section‘Custom Graphics Devices’. Ignored iffig = FALSE.Only available inR2.13.0\R \ge 2.13.0.

width:

numeric (6), width of figures in inches. See‘Details’.

height:

numeric (6), height of figures in inches. See‘Details’.

resolution:

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.

concordance:

logical (FALSE). Write a concordancefile to link the input line numbers to the output line numbers.

figs.only:

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.

Custom Graphics Devices

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.

Hook Functions

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.

Author(s)

Friedrich Leisch and R-core

See Also

Sweave User Manual’, a vignette intheutils package.

Sweave,Rtangle


R for Windows Configuration

Description

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).

Usage

loadRconsole(file)

Arguments

file

The file from which to load a new ‘Rconsole’ configuration.By default a file dialog is used to select a file.

Details

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’.

Value

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.

Chinese/Japanese/Korean

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.

Note

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.

Author(s)

Guido Masarotto and R-core members

See Also

windows

Examples

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

Description

Load or save or display the commands history.

Usage

loadhistory(file=".Rhistory")savehistory(file=".Rhistory")history(max.show=25, reverse=FALSE, pattern,...)timestamp(stamp= date(),          prefix="##------ ", suffix=" ------##",          quiet=FALSE)

Arguments

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.Inf willgive all of the currently available history.

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 togrep when doingthe matching.

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

IfTRUE, suppress printing timestamp to the console.

Details

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.

Windows:

The functions described here work inRgui and interactiveRterm but not in batch use ofRterm nor inembedded/DCOM versions.

Unix-alikes:

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.

Note

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.

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)

Select Items from a List

Description

Select item(s) from a character vector.

Usage

select.list(choices, preselect=NULL, multiple=FALSE,            title=NULL, graphics= getOption("menu.graphics"))

Arguments

choices

a character vector of items.

preselect

a character vector, orNULL. If non-null andif the string(s) appear in the list, the item(s) are selectedinitially.

multiple

logical: can more than one item be selected?

title

optional character string for window title, orNULL for no title.

graphics

logical: should a graphical widget be used?

Details

The normal default isgraphics = TRUE.

On Windows,

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.

Under the macOS GUI,

this brings up a modal dialog boxwith a (scrollable) list of items, which can be selected by the mouse.

On other Unix-like platforms

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.

Value

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.

See Also

menu,tk_select.list for a graphicalversion using Tcl/Tk.

Examples

## Not run:select.list(sort(.packages(all.available=TRUE)))## End(Not run)

Collect Information About the Current R Session

Description

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().

Usage

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),...)osVersion

Arguments

package

a character vector naming installed packages, orNULL(the default) meaning all attached packages.

x

an object of class"sessionInfo".

object

an object of class"sessionInfo".

locale

show locale, by defaulttzone, and (on Windows) code page information?

tzone

show time zone information?

RNG

show information onRNGkind()? Defaults to trueiff it differs from theR version's default, i.e.,RNGversion(*).

...

currently not used.

Value

sessionInfo() returns an object of class"sessionInfo"which hasprint andtoLatex methods. This is a list with components

R.version

a list, the result of callingR.Version().

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(32-bit) appended

running

a character string (or possiblyNULL), the sameasosVersion, see below.

RNGkind

a character vector, the result of callingRNGkind().

matprod

a character string, the result of callinggetOption("matprod").

BLAS

a character string, the result of callingextSoftVersion()["BLAS"].

LAPACK

a character string, the result of callingLa_library().

LA_version

a character string, the result of callingLa_version().

locale

a character string, the result of callingSys.getlocale().

tzone

a character string, the result of callingSys.timezone().

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 ofcallingpackageDescription on otherattached packages.

loadedOnly

(not always present): a named list of the results ofcallingpackageDescription on packages whosenamespaces are loaded but are not attached.

osVersion

osVersion 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⁠’, ....

Note

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.

See Also

R.version,R_compiled_by

Examples

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 report

Select Package Repositories

Description

Interact with the user to choose the package repositories to be used.

Usage

setRepositories(graphics= getOption("menu.graphics"),                ind=NULL, addURLs= character(), name=NULL)

Arguments

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 textmenu.

ind

NULL or a vector of integer indices, which have thesame effect as if they were entered at the prompt forgraphics = FALSE.

name

NULL or character vector of names of therepositories in the repository table which has the same effect aspassing the corresponding indices toind.

addURLs

A character vector of additional URLs: it is oftenhelpful to use a named vector.

Details

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.

Value

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.

Note

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.

See Also

chooseCRANmirror,chooseBioCmirror,install.packages.

Examples

## 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 the Window Title or the Status Bar of the RGui in Windows

Description

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).

Usage

setWindowTitle(suffix, title= paste(getIdentification(), suffix))getWindowTitle()getIdentification()setStatusBar(text)

Arguments

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.

Details

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.

Value

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.

Note

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.

Examples

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)})

Build Shared Object/DLL for Dynamic Loading

Description

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.

Usage

R CMD SHLIB[options][-o dllname] files

Arguments

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. UseR CMD SHLIB --help for a current list.

Details

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.

Note

Some binary distributions ofR haveSHLIB in a separatebundle, e.g., anR-devel RPM.

See Also

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.

Examples

## 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)

Express File Paths in Short Form on Windows

Description

Convert file paths to the short form. This is an interface to theWindows API callGetShortPathNameW.

Usage

shortPathName(path)

Arguments

path

character vector of file paths.

Details

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.

Value

A character vector. The path separator will be ‘⁠\⁠’. If afile path does not exist, the supplied path will be returned with slashesreplaced by backslashes.

Note

This is only available on Windows.

See Also

normalizePath.

Examples

if(.Platform$OS.type=="windows") withAutoprint({  cat(shortPathName(c(R.home(), tempdir())), sep="\n")})

Source Reference Utilities

Description

These functions extract information from source references.

Usage

getSrcFilename(x, full.names=FALSE, unique=TRUE)getSrcDirectory(x, unique=TRUE)getSrcref(x)getSrcLocation(x, which= c("line","column","byte","parse"),               first=TRUE)

Arguments

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.

Details

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:

line

The line number where the object starts or ends.

column

The column number where the object starts or ends.Horizontal tabs are converted to spaces.

byte

As for"column", but counting bytes, which maydiffer in case of multibyte characters (and horizontal tabs).

parse

As for"line", but this ignores#line directives.

Value

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.

See Also

srcref,getParseData

Examples

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")

Stack or Unstack Vectors from a Data Frame or List

Description

Stacking vectors concatenates multiple vectors into a single vectoralong with a factor indicating where each observation originated.Unstacking reverses this operation.

Usage

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,...)

Arguments

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 toformula(x)in the data frame method forunstack.

drop

Whether to drop the unused levels from the “ind”column of the return value.

...

further arguments passed to or from other methods.

Details

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.

Value

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 inx.

ind

a factor indicating from which vector inx theobservation originated.

Author(s)

Douglas Bates

See Also

lm,reshape

Examples

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 vector

Compactly Display the Structure of an Arbitrary R Object

Description

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.

Usage

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,...))

Arguments

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 tothevec.len component of option"str" (seeoptions) which defaults to 4.

digits.d

number of digits for numerical components (as forprint). Defaults to thedigits.d component ofoption"str" which defaults to 3.

nchar.max

maximal number of characters to show forcharacter strings. Longer strings are truncated, seelongch example below.

give.attr

logical; ifTRUE (default), show attributesas sub structures.

drop.deparse.attr

logical; ifTRUE (default),deparse(control =control) will not have"showAttributes" incontrol. Used to be hard coded toFALSE and hence can be set viastrOptions() for backcompatibility.

give.length

logical; ifTRUE (default), indicatelength (as[1:...]).

give.head

logical; ifTRUE (default), give (possiblyabbreviated) mode/class and length (astype[1:...]).

width

the page width to be used. The default is the currentlyactiveoptions("width"); note that this has only aweak effect, unlessstrict.width is not"no".

nest.lev

current nesting level in the recursive calls tostr.

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 (seedelayedAssign) objects only.

strict.width

string indicating if thewidth argument'sspecification should be followed strictly, one of the valuesc("no", "cut", "wrap"), which can be abbreviated.Defaults to thestrict.width component of option"str"(seeoptions) which defaults to"no" for backcompatibility reasons;"wrap" usesstrwrap(*, width = width) whereas"cut" cutsdirectly towidth. Note that a smallvec.lengthmay be better than settingstrict.width = "wrap".

formatNum

a function such asformat forformatting numeric vectors. It defaults to theformatNumcomponent of option"str", see “Usage” ofstrOptions() above, which is almost back compatible toR<= 2.7.x, however, usingformatC may be slightly better.

list.len

numeric; maximum number of list elements to displaywithin a level.

deparse.lines

numeric orNULL as by default, determining thenlines argument todeparse() whenobject isacall. WhenNULL, thenchar.max andwidth arguments are used to determine a smart default.

...

potential further arguments (required for Method/Genericreasons).

Value

str does not return anything, for efficiency reasons.The obvious side effect is output to the terminal.

Note

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”.

Author(s)

Martin Maechler[email protected] since 1990.

See Also

ls.str forlisting objects with their structure;summary,args.

Examples

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)

Capture String Tokens into a data.frame

Description

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.

Usage

strcapture(pattern, x, proto, perl=FALSE, useBytes=FALSE)

Arguments

pattern

The regular expression with the capture expressions.

x

A character vector in which to capture the tokens.

proto

Adata.frame or S4 object that behaves like one. See details.

perl,useBytes

Arguments passed toregexec.

Details

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.

Value

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.

See Also

regexec andregmatches for relatedlow-level utilities.

Examples

x<-"chr1:1-1000"pattern<-"(.*?):([[:digit:]]+)-([[:digit:]]+)"proto<- data.frame(chr=character(), start=integer(), end=integer())strcapture(pattern, x, proto)

Summarise Output of R Sampling Profiler

Description

Summarise the output of theRprof function to show theamount of time used by differentR functions.

Usage

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)

Arguments

filename

Name of a file produced byRprof().

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

IfTRUE memory summaries use change in memoryrather than current memory.

exclude

Functions to exclude when summarizing the stack tracefor memory summaries.

basenames

Number of components of the path to filenames to display.

Details

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.

Value

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.

Memory profiling

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.

Line profiling

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.

See Also

The chapter on ‘Tidying and profiling R code’ in‘Writing R Extensions’:RShowDoc("R-exts").

Rprof

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

Examples

## Not run:## Rprof() is not available on all platformsRprof(tmp<- tempfile())example(glm)Rprof()summaryRprof(tmp)unlink(tmp)## End(Not run)

Automatic Generation of Reports

Description

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.

Usage

Sweave(file, driver= RweaveLatex(),       syntax= getOption("SweaveSyntax"), encoding="",...)Stangle(file, driver= Rtangle(),        syntax= getOption("SweaveSyntax"), encoding="",...)

Arguments

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 matchesfile.

driver

the actual workhorse, (a function returning) a namedlist of five functions; for details, see Section 5 of the‘Sweave User Manual’ available asvignette("Sweave").

syntax

NULL or an object of class"SweaveSyntax" ora character string with its name.See the section ‘Syntax Definition’.

encoding

The default encoding to assume forfile.

...

further arguments passed to the driver's setup function.SeeRweaveLatexSetup andRtangleSetup,respectively, for the arguments of the default drivers.

Details

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".

Syntax Definition

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.*.

Author(s)

Friedrich Leisch and R-core.

References

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.

See Also

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.

Examples

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")

Convert Sweave Syntax

Description

This function converts the syntax of files inSweaveformat to another Sweave syntax definition.

Usage

SweaveSyntConv(file, syntax, output=NULL)

Arguments

file

Name of Sweave source file.

syntax

An object of classSweaveSyntax or a characterstring with its name giving the target syntax to which the file isconverted.

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 infile are also removed suchthat the output is created in the current working directory.

Author(s)

Friedrich Leisch

See Also

Sweave User Manual’, a vignette intheutils package.

RweaveLatex,Rtangle

Examples

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

Description

Create a tar archive.

Usage

tar(tarfile, files=NULL,    compression= c("none","gzip","bzip2","xz","zstd"),    compression_level=6, tar= Sys.getenv("tar"),    extra_flags="")

Arguments

tarfile

The pathname of the tar file: tilde expansion (seepath.expand) will be performed. Alternatively, aconnection that can be used for binary writes.

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 forgzfile forpossible values.

tar

character string: the path to the command to be used. Ifthe command itself contains spaces it needs to be quoted (e.g., byshQuote) – but argumenttar may also containflags separated from the command by spaces.

extra_flags

Any extra flags for an externaltar.

Details

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.)

Value

The return code fromsystem or0 for the internalversion, invisibly.

Portability

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.

Compression

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.

Note

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.

See Also

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.

untar.


Converting R Objects to BibTeX or LaTeX

Description

These methods convertR objects to character vectors withBibTeX or LaTeX markup.

Usage

toBibtex(object,...)toLatex(object,...)## S3 method for class 'Bibtex'print(x, prefix="",...)## S3 method for class 'Latex'print(x, prefix="",...)

Arguments

object

object of a class for which atoBibtex ortoLatex method exists.

x

object of class"Bibtex" or"Latex".

prefix

a character string which is printed at the beginning ofeach line, mostly used to insert whitespace for indentation.

...

in the print methods, passed towriteLines.

Details

Objects of class"Bibtex" or"Latex" are simplycharacter vectors where each element holds one line of thecorresponding BibTeX or LaTeX file.

See Also

citEntry andsessionInfo for examples


Text Progress Bar

Description

Text progress bar in theR console.

Usage

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,...)

Arguments

min,max

(finite) numeric values for the extremes of theprogress bar. Must havemin < max.

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 widthofchar. IfNA, the default, the number of charactersis that which fits intogetOption("width").

style

the ‘style’ of the bar – see ‘Details’.

file

an open connection object or"" which indicatesthe console:stderr() might be useful here.

pb,con

an object of class"txtProgressBar".

title,label

ignored, for compatibility with other progress bars.

...

for consistency with the generic.

Details

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.

Value

FortxtProgressBar an object of class"txtProgressBar".

ForgetTxtProgressBar andsetTxtProgressBar, alength-one numeric vector giving the previous value (invisibly forsetTxtProgressBar).

Note

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.

See Also

winProgressBar (Windows only),tkProgressBar (Unix-alike platforms).

Examples

# 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 Data to Appropriate Type

Description

Convert a data object to logical, integer, numeric, complex, characteror factor as appropriate.

Usage

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,...)

Arguments

x

a vector, matrix, array, data frame, or list.

na.strings

a vector of strings which are to be interpreted asNA values. Blank fields are also considered to bemissing values in logical, integer, numeric or complex vectors.

as.is

whether to store strings as plaincharacter. Whenfalse, convert character vectors tofactors. See ‘Details’.

dec

the character to be assumed for decimal points.

numerals

string indicating how to convert numbers whoseconversion to double precision would lose accuracy, typically whenx has more digits than can be stored in adouble.Can be abbreviated. Possible values are

numerals = "allow.loss", default:

the conversionhappens with some accuracy loss.

numerals = "warn.loss":

awarningabout accuracy loss is signalled and the conversion happens aswithnumerals = "allow.loss".

numerals = "no.loss":

x isnotconverted to a number, but to afactor orcharacter, depending onas.is.

tryLogical

alogical determining if vectorsconsisting entirely ofF,T,FALSE,TRUEandna.strings should be converted tological;true, by default.

...

arguments to be passed to or from methods.

Details

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.

Value

An object likex but using another storage mode whenappropriate.

Author(s)

R Core, with a contribution by Arni Magnusson

See Also

read.table,class,storage.mode.

Examples

## 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 or List Tar Archives

Description

Extract files from or list the contents of a tar archive.

Usage

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"))

Arguments

tarfile

The pathname of the tar file: tilde expansion (seepath.expand) will be performed. Alternatively, aconnection that can be used for binary reads. For acompressedtarfile, and if a connection is to be used,that should be created bygzfile(.) (orgzcon(.) which currently only works for"gzip",whereasgzfile() works for all compressions available intar()).

files

A character vector of recorded filepaths to be extracted:the default is to extract all files.

list

IfTRUE, list the files (the equivalent oftar -tf). Otherwise extract the files (the equivalent oftar -xf).

exdir

The directory to extract files to (the equivalent oftar -C). It will be created if necessary.

compressed

(Deprecated in favour of auto-detection, used onlyfor an externaltar command.) Logical or characterstring. Values"gzip","bzip2","xz" and"zstd" select that form of compression (and may beabbreviated to the first letter).TRUE indicatesgzip compression,FALSE no known compression, andNA (the default) indicates that the type is to be inferredfrom the file header.

The external command may ignore the selected compression type anddetect a type automagically.

extras

NULL or a character string: further command-lineflags such as-p to be passed to an externaltarprogram.

verbose

logical: if true echo the command used for an externaltar program.

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 externaltar command is assumed to be able handle compressedtarfiles and ifcompressed does not specify it, toautomagically detect the type of compression. (The majorimplementations have done so since 2009; for GNUtar sinceversion 1.22.)

If true, theR code calls an appropriate decompressor and pipesthe output totar, forcompressed = NA examiningthe tarfile header to determine the type of compression.

tar

character string: the path to the command to be used or"internal" or"". If the command itself containsspaces it needs to be quoted – buttar can also containflags separated from the command by spaces.

Details

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.

GNU tar:

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).

NetBSD:

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:

OpenBSD'star does not detect compressionautomagically. It has no support forxz beyond reportingthat the file isxz-compressed. Sosupport_old_tars= TRUE is recommended.

Heirloom Toolchest:

Thistar does automagicallydetectgzip andbzip2 compression (undocumented)but had no support forxz norzstd compression.

Older support:

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 ‘._’.

Value

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.

See Also

tar,unzip.


Extract or List Zip Archives

Description

Extract files from or list a zip archive.

Usage

unzip(zipfile, files=NULL, list=FALSE, overwrite=TRUE,      junkpaths=FALSE, exdir=".", unzip="internal",      setTimes=FALSE)

Arguments

zipfile

The pathname of the zip file: tilde expansion (seepath.expand) will be performed.

files

A character vector of recorded filepaths to be extracted:the default is to extract all files.

list

IfTRUE, list the files and extract none. Theequivalent ofunzip -l.

overwrite

IfTRUE, overwrite existing files (the equivalentofunzip -o), otherwise ignore such files (the equivalent ofunzip -n).

junkpaths

IfTRUE, use only the basename of the storedfilepath when extracting. The equivalent ofunzip -j.

exdir

The directory to extract files to (the equivalent ofunzip -d). It will be created if necessary.

unzip

The method to be used. An alternative is to usegetOption("unzip"), which on a Unix-alike may be set to thepath to aunzip program.

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.)

Value

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.

Note

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.

Source

The internal C code useszlib and is in particular based on thecontributed ‘⁠minizip⁠’ application in thezlib sources(fromhttps://zlib.net/) by Gilles Vollant.

See Also

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.


Compare Installed Packages with CRAN-like Repositories

Description

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.

Usage

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"))

Arguments

lib.loc

character vector describing the location of Rlibrary trees to search through (and update packages therein), orNULL for all known trees (see.libPaths).

repos

character vector, the base URL(s) of the repositoriesto use, e.g., the URL of a CRAN mirror such as"https://cloud.r-project.org".

contriburl

URL(s) of the contrib sections of therepositories. Use this argument if your repository isincomplete. Overrides argumentrepos.Incompatible withtype = "both".

method

Download method, seedownload.file.Unused byold.packages if a non-NULLavailableis supplied.

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"graphics", which brings up a widget to allow the userto (de)select from the list of packages which could be updated.The latter value only works on systems with a GUI version ofselect.list, and is otherwise equivalent toask = TRUE.ask does not control questions asked beforeinstalling packages from source viatype = "both" (see option"install.packages.compile.from.source").

available

an object as returned byavailable.packageslisting packages available at the repositories, orNULL whichmakes an internal call toavailable.packages.Incompatible withtype = "both".

checkBuilt

IfTRUE, a package built under an earliermajor.minor version ofR (e.g.,3.4) is considered to be‘old’.

oldPkgs

if specified as non-NULL,update.packages() only considersthese packages for updating. This may be a character vectorof package names or a matrix as returned byold.packages.

instPkgs

by default all installed packages,installed.packages(lib.loc = lib.loc). A subset can bespecified; currently this must be in the same (character matrix)format as returned byinstalled.packages().

...

Arguments such asdestdir anddependencies to bepassed toinstall.packages andignore_repo_cache,max_repo_cache_age andnoCache toavailable.packages orinstalled.packages.

type

character, indicating the type of package to download andinstall. Seeinstall.packages.

Details

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.

Value

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.

Warning

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.

See Also

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

Description

Upgrade objects.

Usage

upgrade(object,...)

Arguments

object

anR object.

...

further arguments passed to or from other methods.

Details

This is a generic function, with a method for"packageStatus" objects.


Display a Text URL

Description

Extension offile.show to display text files from a remoteserver.

Usage

url.show(url, title= url, file= tempfile(),         delete.file=TRUE, method,...)

Arguments

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: seedownload.file

...

Arguments to pass tofile.show.

Note

Since this is for text files, it will convert to CRLF line endings onWindows.

See Also

url,file.show,download.file

Examples

## Not run: url.show("https://www.stats.ox.ac.uk/pub/datasets/csb/ch3a.txt")

Encode or Decode (partial) URLs

Description

Functions to percent-encode or decode characters in URLs.

Usage

URLencode(URL, reserved=FALSE, repeated=FALSE)URLdecode(URL)

Arguments

URL

a character vector.

reserved

logical: should ‘reserved’ characters beencoded? See ‘Details’.

repeated

logical: should apparently already-encoded URLs beencoded again?

Details

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.

Value

A character vector.

References

Internet STD 66 (formerly RFC 3986),https://www.rfc-editor.org/info/std66

Examples

(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")))

Deprecated Functions in Packageutils

Description

(Currently none)

These functions are provided for compatibility with older versions ofR only, and may be defunct as soon as of the next release.

See Also

Deprecated,Defunct


Invoke a Data Viewer

Description

Invoke a spreadsheet-style data viewer on a matrix-likeR object.

Usage

View(x, title)

Arguments

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 ofxprefixed byData:.

Details

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.

On Unix-alikes,

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.

On Windows,

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.

Value

InvisibleNULL. The functions puts up a window and returnsimmediately: the window can be closed via its controls or menus.

See Also

edit.data.frame,data.entry.


View, List or Get R Source of Package Vignettes

Description

View a specified package vignette, or list the available ones;display it rendered in a viewer, and get or edit itsR source file.

Usage

vignette(topic, package=NULL, lib.loc=NULL, all=TRUE)## S3 method for class 'vignette'print(x,...)## S3 method for class 'vignette'edit(name,...)

Arguments

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, orNULL in which ‘all’ packages (asdefined by argumentall) are searched.

lib.loc

a character vector of directory names ofR libraries,orNULL. The default value ofNULL corresponds to alllibraries currently known.

all

logical; ifTRUE search all available packages inthe library trees specified bylib.loc, and ifFALSE,search only attached packages.

x,name

object of classvignette.

...

ignored by theprint method, passed on tofile.edit by theedit method.

Details

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".

See Also

browseVignettes for an HTML-based vignette browser;RShowDoc("basename", package = "pkgname") displays a“rendered” vignette (pdf or html).

Examples

## 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 and Summarize Errors From List

Description

Collect errors (class"error", typically fromtryCatch)from a listx into a “summary warning”, by defaultproduce awarning and keep that message as"warningMsg" attribute.

Usage

warnErrList(x, warn=TRUE, errValue=NULL)

Arguments

x

alist, typically from applying models to alist of data (sub)sets, e.g., usingtryCatch(*, error = identity).

warn

logical indicating ifwarning() should becalled.

errValue

the value with which errors should be replaced.

Value

alist of the same length and names as thexargument, with the error components replaced byerrValue,NULL by default, and summarized in the"warningMsg" attribute.

See Also

ThewarnErrList() utility has been used inlmList() andnlsList() inrecommended packagenlme forever.

Examples

## 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>

Dialog Boxes under Windows

Description

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.

Usage

winDialog(type= c("ok","okcancel","yesno","yesnocancel"),          message)winDialogString(message, default)

Arguments

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.

Value

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.

Note

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.

See Also

winMenuAdd
file.choose to select a file
packagewindlgs in the package source distribution for ways toprogram dialogs in C in theGraphApp toolkit.

Examples

## Not run: winDialog("yesno", "Is it OK to delete file blah")

Get Windows Version

Description

Get the self-reported Microsoft Windows version number.

Usage

win.version()

Details

win.version is an auxiliary function forsessionInfo andbug.report.

Value

A character string describing the version of Windows reported to be in use.

Note

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.

Examples

if(.Platform$OS.type=="windows")   print(win.version())

User Menus under MS Windows (RGui)

Description

Enables users to add, delete and program menus for theRgui in MS Windows.

Usage

winMenuAdd(menuname)winMenuAddItem(menuname, itemname, action)winMenuDel(menuname)winMenuDelItem(menuname, itemname)winMenuNames()winMenuItems(menuname)

Arguments

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"enable" or"disable".

Details

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:

$ConsoleMain

The console menu (the default)

$ConsolePopup

The console popup menu

$Graph<n>Main

The menu for graphics window<n>

$Graph<n>Popup

The 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.

Value

NULL, invisibly. If an error occurs, an informative errormessage will be given.

Note

These functions are only available on Windows and only when usingtheRgui, hence not inESS norRStudio.

See Also

winDialog

Examples

## 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)

Progress Bars under MS Windows

Description

Put up a Windows progress bar widget, update and access it.

Usage

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,...)

Arguments

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"winProgressBar".

...

for consistency with the generic.

Details

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.

Value

ForwinProgressBar an object of class"winProgressBar".

ForgetWinProgressBar andsetWinProgressBar, alength-one numeric vector giving the previous value (invisibly forsetWinProgressBar).

Note

These functions are only available on Windows.

See Also

On all platforms,txtProgressBar,tkProgressBar


Data Output

Description

write.table prints its required argumentx (afterconverting it to a data frame if it is not one nor a matrix) toa file orconnection.

Usage

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(...)

Arguments

x

the object to be written, preferably a matrix or data frame.If not, it is attempted to coercex to a data frame.

file

either a character string naming a file or aconnectionopen for writing."" indicates output to the console.

append

logical. Only relevant iffile is a characterstring. IfTRUE, the output is appended to thefile. IfFALSE, any existing file of the name is destroyed.

quote

a logical value (TRUE orFALSE) or anumeric vector. IfTRUE, any character or factor columnswill be surrounded by double quotes. If a numeric vector, itselements are taken as the indices of columns to quote. In bothcases, row and column names are quoted if they are written. IfFALSE, nothing is quoted.

sep

the field separator string. Values within each row ofx are separated by this string.

eol

the character(s) to print at the end of each line (row).For example,eol = "\r\n" will produce Windows' line endings ona Unix-alike OS, andeol = "\r" will produce files as expected byExcel:mac 2004.

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 ofx are to be written along withx, or acharacter vector of row names to be written.

col.names

either a logical value indicating whether the columnnames ofx are to be written along withx, or acharacter vector of column names to be written. See the section on‘CSV files’ for the meaning ofcol.names = NA.

qmethod

a character string specifying how to deal with embeddeddouble quote characters when quoting strings. Must be one of"escape" (default forwrite.table), in which case thequote character is escaped in C style by a backslash, or"double" (default forwrite.csv andwrite.csv2), in which case it is doubled. You can specifyjust the initial letter.

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. Seefile.

...

arguments towrite.table:append,col.names,sep,dec andqmethodcannot be altered.

Details

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").

CSV files

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.

Note

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.

See Also

The ‘R Data Import/Export’ manual.

read.table,write.

write.matrix in packageMASS.

Examples

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)

Create Zip Archives

Description

A wrapper for an externalzip command to create zip archives.

Usage

zip(zipfile, files, flags="-r9X", extras="",    zip= Sys.getenv("R_ZIPCMD","zip"))

Arguments

zipfile

The pathname of the zip file: tilde expansion (seepath.expand) will be performed.

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.

Details

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.

Value

The status value returned by the external command, invisibly.

See Also

unzip,unz; further,tar anduntar for (un)packing tar archives.



[8]ページ先頭

©2009-2025 Movatter.jp