Package zeallot. R topics documented: September 28, Type Package

Similar documents
Package cattonum. R topics documented: May 2, Type Package Version Title Encode Categorical Features

Package fastdummies. January 8, 2018

Package messaging. May 27, 2018

Package knitrprogressbar

Package ECctmc. May 1, 2018

Package robotstxt. November 12, 2017

Package modules. July 22, 2017

Package jdx. R topics documented: January 9, Type Package Title 'Java' Data Exchange for 'R' and 'rjava'

Package labelvector. July 28, 2018

Package canvasxpress

Package eply. April 6, 2018

Package pinyin. October 17, 2018

Package githubinstall

Package facerec. May 14, 2018

Package BiocManager. November 13, 2018

Package validara. October 19, 2017

Package purrrlyr. R topics documented: May 13, Title Tools at the Intersection of 'purrr' and 'dplyr' Version 0.0.2

Package docxtools. July 6, 2018

Package dkanr. July 12, 2018

Package glue. March 12, 2019

Package barcoder. October 26, 2018

Package SEMrushR. November 3, 2018

Package colf. October 9, 2017

Package reval. May 26, 2015

Package lumberjack. R topics documented: July 20, 2018

Package snakecase. R topics documented: March 25, Version Date Title Convert Strings into any Case

Package humanize. R topics documented: April 4, Version Title Create Values for Human Consumption

Package keyholder. May 19, 2018

Package tidyimpute. March 5, 2018

Package rsppfp. November 20, 2018

Package tibble. August 22, 2017

Package fingertipsr. May 25, Type Package Version Title Fingertips Data for Public Health

Package geojsonsf. R topics documented: January 11, Type Package Title GeoJSON to Simple Feature Converter Version 1.3.

Package WordR. September 7, 2017

Package ezsummary. August 29, 2016

Package rprojroot. January 3, Title Finding Files in Project Subdirectories Version 1.3-2

Package triebeard. August 29, 2016

Package ecoseries. R topics documented: September 27, 2017

Package semver. January 6, 2017

Package ggrepel. September 30, 2017

Package postal. July 27, 2018

Package available. November 17, 2017

Package pdfsearch. July 10, 2018

Package gtrendsr. October 19, 2017

Package textrecipes. December 18, 2018

Package ggimage. R topics documented: November 1, Title Use Image in 'ggplot2' Version 0.0.7

Package bigreadr. R topics documented: August 13, Version Date Title Read Large Text Files

Package bisect. April 16, 2018

Package modmarg. R topics documented:

Package spark. July 21, 2017

Package liftr. R topics documented: May 14, Type Package

Package assertr. R topics documented: February 23, Type Package

Package nngeo. September 29, 2018

Package condusco. November 8, 2017

Package internetarchive

Package catchr. R topics documented: December 30, Type Package. Title Catch, Collect, and Raise Conditions. Version 0.1.0

Package ggloop. October 20, 2016

Package postgistools

Package dbx. July 5, 2018

Package readxl. April 18, 2017

Package gtrendsr. August 4, 2018

Package essurvey. August 23, 2018

Package ezknitr. September 16, 2016

Package clipr. June 23, 2018

Package goodpractice

Package gggenes. R topics documented: November 7, Title Draw Gene Arrow Maps in 'ggplot2' Version 0.3.2

Package vinereg. August 10, 2018

Package ggimage. R topics documented: December 5, Title Use Image in 'ggplot2' Version 0.1.0

Package kdtools. April 26, 2018

Package repec. August 31, 2018

Package RODBCext. July 31, 2017

Package guardianapi. February 3, 2019

Package calpassapi. August 25, 2018

Package oec. R topics documented: May 11, Type Package

Package loggit. April 9, 2018

Package datapasta. January 24, 2018

Package customlayout

Package widyr. August 14, 2017

Package data.world. April 5, 2018

Package slickr. March 6, 2018

Package spelling. December 18, 2017

Package styler. December 11, Title Non-Invasive Pretty Printing of R Code Version 1.0.0

Package ruler. February 23, 2018

Package opencage. January 16, 2018

Package infer. July 11, Type Package Title Tidy Statistical Inference Version 0.3.0

Package ensurer. August 29, 2016

Package sfdct. August 29, 2017

Package promises. April 13, 2018

Package qualmap. R topics documented: September 12, Type Package

Package TrafficBDE. March 1, 2018

Package genesysr. June 14, 2018

Package pmatch. October 19, 2018

Package tidylpa. March 28, 2018

Package fst. December 18, 2017

Package processanimater

Package censusr. R topics documented: June 14, Type Package Title Collect Data from the Census API Version 0.0.

Package fst. June 7, 2018

Package pwrab. R topics documented: June 6, Type Package Title Power Analysis for AB Testing Version 0.1.0

Package mason. July 5, 2018

Package httpcache. October 17, 2017

Transcription:

Type Package Package zeallot September 28, 2017 Title Multiple, Unpacking, and Destructuring Assignment Version 0.0.6 Description Provides a %<-% operator to perform multiple, unpacking, and destructuring assignment in R. The operator unpacks the right-hand side of an assignment into multiple values and assigns these values to variables on the left-hand side of the assignment. URL https://github.com/nteetor/zeallot BugReports https://github.com/nteetor/zeallot/issues License MIT + file LICENSE Encoding UTF-8 RoxygenNote 6.0.1 VignetteBuilder knitr Suggests testthat, knitr, rmarkdown, purrr, magrittr NeedsCompilation no Author Nathan Teetor [aut, cre], Paul Teetor [ctb] Maintainer Nathan Teetor <nathanteetor@gmail.com> Repository CRAN Date/Publication 2017-09-28 18:39:40 UTC R topics documented: destructure.......................................... 2 operator........................................... 3 zeallot............................................ 7 Index 8 1

2 destructure destructure Destructure an object Description Usage destructure is used during unpacking assignment to coerce an object into a list. Individual elements of the list are assigned to names on the left-hand side of the unpacking assignment expression. destructure(x) Arguments x An R object. Details If x is atomic destructure expects length(x) to be 1. If a vector with length greater than 1 is passed to destructure an error is raised. New implementations of destructure can be very simple. A new destructure implementation might only strip away the class of a custom object and return the underlying list structure. Alternatively, an object might destructure into a nested set of values and may require a more complicated implementation. In either case, new implementations must return a list object so %<-% can handle the returned value(s). See Also %<-% Examples # data frames become a list of columns destructure( data.frame(x = 0:4, y = 5:9) ) # strings are split into list of characters destructure("abcdef") # dates become list of year, month, and day destructure(sys.date()) # create a new destructure implementation shape <- function(sides = 4, color = "red") { structure( list(sides = sides, color = color), class = "shape" )

operator 3 } ## Not run: # cannot destructure the shape object yet c(sides, color) %<-% shape() ## End(Not run) # implement destructure for shapes destructure.shape <- function(x) { list(x$sides, x$color) } # now we can destructure shape objects c(sides, color) %<-% destructure(shape()) sides # 4 color # "red" c(sides, color) %<-% destructure(shape(3, "green")) sides # 3 color # 'green' operator Multiple assignment operators Description Assign values to name(s). Usage x %<-% value value %->% x Arguments x value A name structure, see section below. A list of values, vector of values, or R object to assign. Value %<-% and %->% invisibly return value. These operators are used primarily for their assignment side-effect. %<-% and %->% assign into the environment in which they are evaluated.

4 operator Name Structure the basics At its simplest, the name structure may be a single variable name, in which case %<-% and %->% perform regular assignment, x %<-% list(1, 2, 3) or list(1, 2, 3) %->% x. To specify multiple variable names use a call to c(), for example c(x, y, z) %<-% c(1, 2, 3). When value is neither an atomic vector nor a list, %<-% and %->% will try to destructure value into a list before assigning variables, see destructure(). object parts Like assigning a variable, one may also assign part of an object, c(x,x[[1]]) %<-% list(list(), 1). nested names One can also nest calls to c() when needed, c(x, c(y, z)). This nested structure is used to unpack nested values, c(x, c(y, z)) %<-% list(1, list(2, 3)). collector variables To gather extra values from the beginning, middle, or end of value use a collector variable. Collector variables are indicated with a... prefix, c(...start, z) %<-% list(1, 2, 3, 4). skipping values Use. in place of a variable name to skip a value without raising an error or assigning the value, c(x,., z) %<-% list(1, 2, 3). Use... to skip multiple values without raising an error or assigning the values, c(w,..., z) %<-% list(1, NA, NA, 4). default values Use = to specify a default value for a variable, c(x, y = NULL) %<-% tail(1, 2). When assigning part of an object a default value may not be specified because of the syntax enforced by R. The following would raise an "unexpected '='..." error, c(x, x[[1]] = 1) %<-% list(list()). See Also For more on unpacking custom objects please refer to destructure(). Examples # basic usage c(a, b) %<-% list(0, 1) a # 0 b # 1 # unpack and assign nested values c(c(e, f), c(g, h)) %<-% list(list(2, 3), list(3, 4)) e # 2 f # 3 g # 4 h # 5 # can assign more than 2 values at once

operator 5 c(j, k, l) %<-% list(6, 7, 8) # assign columns of data frame c(erupts, wait) %<-% faithful erupts # 3.600 1.800 3.333.. wait # 79 54 74.. # assign only specific columns, skip # other columns c(mpg, cyl, disp,...) %<-% mtcars mpg # 21.0 21.0 22.8.. cyl # 6 6 4.. disp # 160.0 160.0 108.0.. # skip initial values, assign final value TODOs <- list("make food", "pack lunch", "save world") c(..., task) %<-% TODOs task # "save world" # assign first name, skip middle initial, # assign last name c(first,., last) %<-% c("ursula", "K", "Le Guin") first last # "Ursula" # "Le Guin" # simple model and summary mod <- lm(hp ~ gear, data = mtcars) # extract call and fstatistic from # the summary c(modcall,..., modstat,.) %<-% summary(mod) modcall modstat # unpack nested values w/ nested names fibs <- list(1, list(2, list(3, list(5)))) c(f2, c(f3, c(f4, c(f5)))) %<-% fibs f2 # 1 f3 # 2 f4 # 3 f5 # 5 # unpack first numeric, leave rest c(f2, fibcdr) %<-% fibs

6 operator f2 # 1 fibcdr # list(2, list(3, list(5))) # swap values without using temporary variables c(a, b) %<-% c("eh", "bee") a b # "eh" # "bee" c(a, b) %<-% c(b, a) a b # "bee" # "eh" # unpack strsplit return value names <- c("nathan,maria,matt,polly", "Smith,Peterson,Williams,Jones") c(firsts, lasts) %<-% strsplit(names, ",") firsts # c("nathan", "Maria",.. lasts # c("smith", "Peterson",.. # handle missing values with default values parse_time <- function(x) { strsplit(x, " ")[[1]] } c(hour, period = NA) %<-% parse_time("10:00 AM") hour # "10:00" period # "AM" c(hour, period = NA) %<-% parse_time("15:00") hour # "15:00" period # NA # right operator list(1, 2, "a", "b", "c") %->% c(x, y,...chars) x # 1 y # 2 chars # list("a", "b", "c") # magrittr chains, install.packages("magrittr") for this example if (requirenamespace("magrittr", quietly = TRUE)) { library(magrittr) c("hello", "world!") %>% paste0("\n") %>% lapply(toupper) %->% c(greeting, subject)

zeallot 7 } greeting subject # "HELLO\n" # "WORLD!\n" zeallot Multiple, unpacking, and destructuring assignment in R Description zeallot provides a %<-% operator to perform multiple assignment in R. To get started with zeallot be sure to read over the introductory vignette on unpacking assignment, vignette('unpacking-assignment'). Author(s) See Also Maintainer: Nathan Teetor <nathanteetor@gmail.com> Other contributors: %<-% Paul Teetor [contributor]

Index %->% (operator), 3 %<-% (operator), 3 destructure, 2 destructure(), 4 operator, 3 zeallot, 7 zeallot-package (zeallot), 7 8