callWithStringsAsFactors: Call a Function with Option "stringsAsFactors" set...

View source: R/main.R

callWithStringsAsFactorsR Documentation

Call a Function with Option "stringsAsFactors" set temporarily

Description

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")
)
  
d2 <- callWithStringsAsFactors(
  FALSE,
  rbind,
  data.frame(id = 1, name = "Peter"),
  data.frame(id = 2, name = "Paul"),
  data.frame(id = 3, name = "Mary")
)
  
str(d1)
str(d2)
  
# The option "stringsAsFactors" has not changed!
stopifnot(option.bak == getOption("stringsAsFactors"))
  

KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.