wildcard: Function 'wildcard'

Description Usage Arguments Examples

View source: R/ui.R

Description

Main function of the package. Evaluate a wildcard to fill in or expand a data frame. Copied and modified from remakeGenerator::evaluate() under GPL-3: https://github.com/wlandau/remakeGenerator

Usage

1
2
wildcard(df, rules = NULL, wildcard = NULL, values = NULL,
  expand = TRUE, include = NULL, exclude = NULL)

Arguments

df

data frame

rules

list with names a wildcards and elements as vectors of values to substitute in place of the wildcards.

wildcard

character scalar, a wildcard found in a data frame

values

vector of values to substitute in place of a wildcard

expand

logical, whether to expand the rows of the data frame to substitute each value for each wildcard in turn. If FALSE, no new rows will be added to df when the values are substituted in place of wildcards. Can be a vector of length length(rules) if using the rules argument.

include

character vector of columns of df to be included in the wildcard evaluation. The values will replace the wildcards in these columns but not in any of the other colums. All columns are included by default. You may use include or exclude (or neither), but not both.

exclude

character vector of columns of df to be EXCLUDED from the wildcard evaluation. The values will NOT replace the wildcards in any of these columns, but wildcard evaluation will occur in all the other columns. By default, no columns are excluded (all columns are used for wildcard evaluation). You may use include or exclude (or neither), but not both.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
myths <- data.frame(
  myth = c('Bigfoot', 'UFO', 'Loch Ness Monster'),
  claim = c('various', 'day', 'day'),
 note = c('various', 'pictures', 'reported day'))
wildcard(myths, wildcard = 'day', values = c('today', 'yesterday'))
wildcard(myths, wildcard = 'day', values = c('today', 'yesterday'),
  expand = FALSE)
locations <- data.frame(
  myth = c('Bigfoot', 'UFO', 'Loch Ness Monster'),
  origin = 'where')
rules <- list(
  where = c('North America', 'various', 'Scotland'),
  UFO = c('spaceship', 'saucer'))
wildcard(locations, rules = rules, expand = c(FALSE, TRUE))
numbers <- data.frame(x = 4, y = 3, z = 4444, w = 4.434)
wildcard(numbers, wildcard = 4, values = 7)
# Inclusion and exclusion
wildcard(myths, wildcard = "day", values = c("today", "yesterday"),
  include = "claim")
wildcard(myths, wildcard = "day", values = c("today", "yesterday"),
  exclude = c("claim", "note"))
# Wildcards should not also be replacement values.
# Otherwise, the output will be strange
# and will depend on the order of the wildcards.
## Not run: 
df <- data.frame(x = "a", y = "b")
rules <- list(a = letters[1:3], b = LETTERS[1:3])
wildcard(df, rules = rules)

## End(Not run)

Example output

               myth     claim               note
1           Bigfoot   various            various
2               UFO     today           pictures
3               UFO yesterday           pictures
4 Loch Ness Monster     today     reported today
5 Loch Ness Monster yesterday reported yesterday
               myth     claim           note
1           Bigfoot   various        various
2               UFO     today       pictures
3 Loch Ness Monster yesterday reported today
               myth        origin
1           Bigfoot North America
2         spaceship       various
3            saucer       various
4 Loch Ness Monster      Scotland
  x y    z     w
1 7 3 7777 7.737
               myth     claim         note
1           Bigfoot   various      various
2               UFO     today     pictures
3               UFO yesterday     pictures
4 Loch Ness Monster     today reported day
5 Loch Ness Monster yesterday reported day
               myth   claim         note
1           Bigfoot various      various
2               UFO     day     pictures
3 Loch Ness Monster     day reported day
  x y
1 a A
2 a B
3 a C
4 A A
5 B B
6 C C
7 c A
8 c B
9 c C
Warning message:
In check_rules(rules) :
  In `rules`, some wildcards are also replacement values.
The returned data frame may be different than you expect,
and it may depend on the order of the wildcards in `rules`.

wildcard documentation built on May 2, 2019, 8:25 a.m.