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

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") ) 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: Factor w/ 3 levels "Peter","Paul",..: 1 2 3
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"))