I have create a function that builds a tibble from a few basic inputs
library(tidyverse)
fun <- function(x, y) {
tibble(
start = x) %>%
mutate(k = x * y)
}
What I would like to do is set the class
of each variable within this function.
fun <- function(x, y) {
tibble(
start = x) %>%
mutate(k = x * y) %>%
mutate_at(vars(x, k), "currency")
}
I've tried
fun <- function(x, y) {
tibble(
start = x) %>%
mutate(k = x * y) %>%
class(k) <- "currency"
}
My goal is to create a tibble that includes classes in them for later export using the openxlsx
package. I've found lots of ways to convert from factors to numeric, etc. using the mutate_at function, but cannot find a way to define the class of a tibble (or data frame) variable in the same function it is create. I know I can do this in a script easy enough, but it seems like something I should be able to do within a function.