getVarValues: [~] Parse arguments of a function and return approriate...

Description Usage Arguments Value Author(s) Examples

View source: R/getVarValues.R

Description

Parse dataframe DATA, variable VAR and a call CALL of a function and return approriate values of the VAR.
If possible, return DATA[,"VAR"]
Otherwise return VAR.

Usage

1
2
3
4
5
6
7
getVarValues(
  VAR,
  DATA,
  CALL = match.call(definition = sys.function(sys.parent()), call =
    sys.call(sys.parent())),
  env = parent.frame(2L)
)

Arguments

VAR

A name of a variable (with or without quotes).

DATA

A name of a data frame (with or without quotes).

CALL

(Call) to be parsed. Default is call to parent function.

env

The environment in which expressions must be evaluated.

Value

A vector. If possible, return DATA[,VAR]. Otherwise return VAR.

Author(s)

Vilmantas Gegzna

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
library(spMisc)
# --------------------------------------------------------------------
# [Behaviour of function has changed] EXAMPLES  are out of date!!!
# --------------------------------------------------------------------


# EXAMPLE 1 *****************************************************************

# Clear current environment
clear()

# Data
df  <- mtcars[,c("cyl","gear")]

#  Function, that uses `getVarValues`:
f1 <- function(data, v1, v2) { getVarValues(v1, data) }

# Returns values of `df$cyl`:
f1(df, "cyl")
##  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

# Values of `df$gear` not of `df$cyl`:
cyl <- "gear"
f1(df, cyl)
##  [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4

# Returns values of `df$gear`:
f1(df, "gear")
##  [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4

var <- c("My", "variable", "var")
f1(df, var)
## [1] "My"   "variable"   "var"
## Warning message:
## In getVarValues(v1, data) : Arguments 'DATA' and 'VAR' do not match!!!

# EXAMPLE 2 *****************************************************************
# A Data frame
   df <- data.frame(A = "Values_A_(DATA.FRAME)",
                    E = "Values_E_(DATA.FRAME)", stringsAsFactors = FALSE)

# Vectors
   A <- "Values of the vector 'A'"
   B <- "Values of the vector 'B'"


# A call object `CALL`:

fun  <- function(data, gr, ID) match.call()
CALL <- fun(df, A, B)
CALL
## fun(data = df, gr = A, ID = B)


# Outputs of `getVarValues` -------------------------------------------------


getVarValues("A", df, CALL)
## [1] "Values_A_(DATA.FRAME)"

getVarValues(A, df)
## [1] "Values of the vector 'A'"

getVarValues(B, df)
## [1] "Values of the vector 'B'"

# UNEXPECTED results -----------------------------------------------------------------------


## Not run: 

 getVarValues(ID, df) # ??? `ID` found only in function's `fun` definition.
 ## NULL

 getVarValues(G, df) # ERROR, as variable G does not exist.
 ##  Error in eval(expr, envir, enclos) : object 'G' not found

 getVarValues(F, df) # F is a special variable: F = FALSE
 ##  FALSE

 getVarValues(c, df) # c() is a function.
 ## function (..., recursive = FALSE)  .Primitive("c")


 getVarValues(NULL, df) # NULL
 getVarValues(NA,   df) # NA
 getVarValues(NaN,  df) # NaN

## End(Not run)

GegznaV/spMisc documentation built on April 26, 2020, 5:59 p.m.