Skip to contents

Create a variable that supersedes other variables and has various functionalities

Usage

newSuperVar(variable, value = 0L, lock = FALSE, editn = NULL)

Arguments

variable

variable name for super variable

value

value of the variable

lock

lock variable to change

editn

number of times the super variable may be set to a new value using .set().
- Set to NULL to allow unlimited value change
- Set to 0 to prevent editing the super variable

Value

no visible return, but variable is created and stored with various functionalities

Note

What you should know about the functionality:

This function ensures that a variable is created and may not easily be altered. It helps preserve the original variable by providing only limited access to the variable.

Creation of this super variable automatically attached some key functions to it, such that the user is able to call the function like .set(), .rm().

Super variable value may be set from any scope using the .set() function, which means that it is granted global variable features without being present within the global environment of the current section.

The variable name of the super variable may be overwritten in the local environment, but this would not alter the super variable. It means that once the local variable is removed, the super variable remains available for use.

Use cases:

- Preserve originality of variable within an R session. Avoid inadvertent deletion.

- Widely accessible from any scope e.g functions, lapply, loops, local environment etc

- Restricted mutability of variable using set function e.g varname.set()

- Variable with easy function calls by attaching '.'

- Variable with un-mutable class when changing its value

- Variable with restricted number of times it can be changed

Examples

# Task: create a super variable to
# store dataset that should not be altered
newSuperVar(mtdf, value = austres) # create a super variable
head(mtdf) # view it
#> [1] 13067.3 13130.5 13198.4 13254.2 13303.7 13353.9
mtdf.class # view the store class of the variable, it cannot be changed
#> [1] "ts"
# it means that when the super variable is edited, the new value MUST have the same class "ts"

# create and lock super variable by default
# extra security to prevent changing
newSuperVar(mtdf3, value = beaver1, lock = TRUE)
head(mtdf3) # view
#>   day time  temp activ
#> 1 346  840 36.33     0
#> 2 346  850 36.34     0
#> 3 346  900 36.35     0
#> 4 346  910 36.42     0
#> 5 346  920 36.55     0
#> 6 346  930 36.69     0
mtdf3.round(1) # round to 1 decimal places
head(mtdf3) # view
#>   day time temp activ
#> 1 346  840 36.3     0
#> 2 346  850 36.3     0
#> 3 346  900 36.4     0
#> 4 346  910 36.4     0
#> 5 346  920 36.5     0
#> 6 346  930 36.7     0
mtdf3.signif(2) # round to 2 significant digits
head(mtdf3) # view
#>   day time temp activ
#> 1 350  840   36     0
#> 2 350  850   36     0
#> 3 350  900   36     0
#> 4 350  910   36     0
#> 5 350  920   36     0
#> 6 350  930   37     0

# Task: create a new super variable to store numbers
# edit the numbers from various scopes
newSuperVar(edtvec, value = number(5))
edtvec # view content of the vector
#> [1] 989796930 920810920 699060670 501683940 651211721

# edtvec.set(letters) #ERROR: Cannot set to value with different class than initial value

edtvec.set(number(20)) # set to new numbers
edtvec # view output
#>  [1] 122456571 877662456 242745245 268460318  57187700 483436664 879106300
#>  [8] 185263354 729098265  57820740 317418108 991231294 163001476 431725693
#> [15]  55356427 890909582 511030826 586039434 764088705 265678445

for (pu in 1:8) {
  print(edtvec) # view output within loop
  edtvec.set(number(pu)) # set to new numbers within for loop
}
#>  [1] 122456571 877662456 242745245 268460318  57187700 483436664 879106300
#>  [8] 185263354 729098265  57820740 317418108 991231294 163001476 431725693
#> [15]  55356427 890909582 511030826 586039434 764088705 265678445
#> [1] 922044610
#> [1] 542750453 684796673
#> [1] 985971426  21730612 547744612
#> [1] 113177268 618165892 311652864 847668387
#> [1] 720177668  45533169  35399543 403824668 402159537
#> [1] 967187903 446602062 492371572 849516133 972632371 339960427
#> [1] 207482094  91025226 449917866 392054235 225694087  63416129 658798843

lc <- lapply(1:8, function(pu) {
  print(edtvec) # view output within loop
  edtvec.set(number(pu)) # set to new numbers within lapply loop
})
#> [1] 465200626 937148200 251387636 962507853 989139661 238239587 205783011
#> [8] 486979911
#> [1] 433634519
#> [1]  23538982 866274810
#> [1] 428422168 801913291 617613581
#> [1] 180622873 162176338 134378123 851868852
#> [1] 919463090 184563029  69547758 691353684 938521527
#> [1] 561368817  33680892  62541986 944709481 861278716 255574511
#> [1] 166432913 287912323 909114178 487720854 562149077 563715948 401457498

# see that the above changed the super variable easily.
# local variable will not be altered by the loop
# example
bim <- 198
lc <- lapply(1:8, function(j) {
  print(bim)
  bim <- j # will not alter the value of bim in next round
})
#> [1] 198
#> [1] 198
#> [1] 198
#> [1] 198
#> [1] 198
#> [1] 198
#> [1] 198
#> [1] 198


# Task: create and search data.frame
# create a new super variable with value as mtcars
# search if it contains the numeric value 21
newSuperVar(lon2, value = mtcars) # declares lon2
lon2 # view content of lon2
#>                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#> Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#> Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#> Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#> Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#> Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#> Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#> AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#> Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#> Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
#> Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
#> Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
#> Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
#> Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
lon2.contains("21.0") # WRONG - since df.col is not specific,
#> [1] FALSE
# only the first column is search for the character "21.0"
lon2.contains("21.0", df.col = "mpg") # WRONG - searches mpg column
#> [1] FALSE
# for the character "21.0"
lon2.contains(21.0, df.col = "mpg") # CORRECT - search mpg column for the
#> [1] TRUE
# numeric value 21.0

# remove lon2 as a super variable
exists("lon2") # before removal
#> [1] TRUE
lon2.rm()
exists("lon2") # after removal
#> [1] FALSE

# Task: create and search vector
# create a new super variable with value as 10 random numbers
# search if it contains the numeric value 72
newSuperVar(lon3, value = number(10, seed = 12)) # declares lon3
lon3 # view content of lon3
#>  [1] 297914714 827278583 727320750 767861829  98239010 612880475 542204285
#>  [8]  64057465 891652716 711531738
lon3.contains(72) # should give TRUE or false if the vector contains the value 45
#> [1] TRUE
lon3.contains(72, fixed = TRUE) # should give TRUE or false if the vector contains the value 45
#> [1] TRUE

# remove lon3 as a super variable
lon3.rm()


#Task: create a super variable that can only be edited 3 times
newSuperVar(man1, value = number(5), editn = 3)
man1 # view value
#> [1] 937871792 420263352 935543960  20459592 813607802

man1.set(number(10)) # change value first time
man1 # view value
#>  [1] 570216551 572387564 832886781 578315298 380012494 977679002 404691028
#>  [8] 563284928 655541844 620977293

man1.set(number(2)) # change value second time
man1 # view value
#> [1] 734810736 224065701

man1.set(number(1)) # change value third time
man1 # view value
#> [1] 248943356

man1.set(number(5)) # change value forth time,
# should not change because max change times exceeded
man1 # view value
#> [1] 248943356