Skip to contents

Set the option "stringsAsFactors", run a function and reset the option.

Usage

callWithStringsAsFactors(stringsAsFactors, FUN, ...)

Arguments

stringsAsFactors

TRUE or FALSE. Before calling FUN the option "stringsAsFactors" is set to the value given here. After the function call the option is reset to what it was before.

FUN

function to be called

...

arguments passed to FUN

Value

This function returns what FUN returns when called with the arguments given in ...

Examples

option.bak <- getOption("stringsAsFactors")
  
d1 <- callWithStringsAsFactors(
  TRUE,
  rbind,
  data.frame(id = 1, name = "Peter"),
  data.frame(id = 2, name = "Paul"),
  data.frame(id = 3, name = "Mary")
)
#> Warning: 'options(stringsAsFactors = TRUE)' is deprecated and will be disabled
  
d2 <- callWithStringsAsFactors(
  FALSE,
  rbind,
  data.frame(id = 1, name = "Peter"),
  data.frame(id = 2, name = "Paul"),
  data.frame(id = 3, name = "Mary")
)
  
str(d1)
#> 'data.frame':	3 obs. of  2 variables:
#>  $ id  : num  1 2 3
#>  $ name: chr  "Peter" "Paul" "Mary"
str(d2)
#> 'data.frame':	3 obs. of  2 variables:
#>  $ id  : num  1 2 3
#>  $ name: chr  "Peter" "Paul" "Mary"
  
# The option "stringsAsFactors" has not changed!
stopifnot(option.bak == getOption("stringsAsFactors"))