Error in Get(As.Character(Fun), Mode = "Function", Envir = Envir)

R: Error in get(as.character(FUN), mode = function, envir = envir) :

outer takes only two arguments. We may need pmap

library(purrr)
pmap_dbl(list(input_1, input_2, input_3, input_4), my_function_b)

or Map/mapply

mapply(my_function_b, input_1, input_2, input_3, input_4)

If we need all combinations, create the combinations with expand.grid and apply over the rows

tmp <- expand.grid(input_1 = input_1, input_2 = input_2, 
input_3 = input_3, input_4 = input_4)
out <- apply(tmp, 1,
FUN = function(x) do.call(my_function_b, as.list(x)))

Or may speed up with dapply from collapse

library(collapse)

out1 <- dapply(tmp, MARGIN = 1, FUN = function(x)
my_function_b(x[1], x[2], x[3], x[4]))

Perhaps, we create the combinations on two vectors, and then add ?

my_function_b <- function(input_1, input_2) sin(input_1) + cos(input_2)
tmp1 <- outer(input_1, input_2, my_function_b)
z <- tmp1 + input_3[col(tmp1)] + input_4[col(tmp1)]
library(lattice)
wireframe(z, drape=TRUE, col.regions=rainbow(100))

-output

Sample Image

Error in get(as.character(FUN), mode = function, envir = envir) : object 'guide_legend' of mode 'function'

The problem seems to be in the ggnewscale package, at this line, where it tries to create a call to ggplot::guide_legend. This is done via match.fun, which then throws an error at this line. It may be because match.fun only looks back through 2 layers of parent environments for the named function. You could try adding a non-exported, local, alias function to your own package prior to the problematic function:

guide_legend <- function(...) ggplot2::guide_legend(...)

Error in get(as.character(FUN), mode = function, envir = envir) :

It looks like a ifelse() case. if() ... else ... can only treat a single boolean. To make your function work, you need to vectorize it by mapply() or Vectorize().

Example Data

set.seed(1)
df <- data.frame(ONEOFF_PURCHASES = sample(0:1, 5, T), INSTALLMENTS_PURCHASES = sample(0:1, 5, T))

# ONEOFF_PURCHASES INSTALLMENTS_PURCHASES
# 1 0 0
# 2 1 0
# 3 0 0
# 4 0 1
# 5 1 1

Function

purchase <- function(x, y){
if((x == 0) && (y == 0))
return('NONE')
else if((x > 0) && (y > 0))
return('BOTH_ONEOFF_INSTALLMENT')
else if((x > 0) && (y == 0))
return('ONE_OFF')
else if((x == 0) && (y > 0))
return('INSTALLMENT')
else
return('OTHERS')
}

Vectorization

mapply(purchase, df$ONEOFF_PURCHASES, df$INSTALLMENTS_PURCHASES)
# [1] "NONE" "ONE_OFF" "NONE" "INSTALLMENT" "BOTH_ONEOFF_INSTALLMENT"

Vectorize(purchase)(df$ONEOFF_PURCHASES, df$INSTALLMENTS_PURCHASES)
# [1] "NONE" "ONE_OFF" "NONE" "INSTALLMENT" "BOTH_ONEOFF_INSTALLMENT"

Actually, we don't use the approach above in this issue. We'll use ifelse() or dplyr::case_when().

library(dplyr)

df %>%
mutate(PURCHASE_TYPE = case_when(
(ONEOFF_PURCHASES == 0) & (INSTALLMENTS_PURCHASES == 0) ~ 'NONE',
(ONEOFF_PURCHASES > 0) & (INSTALLMENTS_PURCHASES > 0) ~ 'BOTH_ONEOFF_INSTALLMENT',
(ONEOFF_PURCHASES > 0) & (INSTALLMENTS_PURCHASES == 0) ~ 'ONE_OFF',
(ONEOFF_PURCHASES == 0) & (INSTALLMENTS_PURCHASES > 0) ~ 'INSTALLMENT',
TRUE ~ 'OTHERS'
))

# ONEOFF_PURCHASES INSTALLMENTS_PURCHASES PURCHASE_TYPE
# 1 0 0 NONE
# 2 1 0 ONE_OFF
# 3 0 0 NONE
# 4 0 1 INSTALLMENT
# 5 1 1 BOTH_ONEOFF_INSTALLMENT

Aggregate function not allowing to aggregate using maximum function

It would be an issue if the function name is already assigned to another object

max <- 25
aggregate(loss~year, data = claim.data, FUN = max)

Error in get(as.character(FUN), mode = "function", envir = envir) :

object 'FUN' of mode 'function' was not found

Either use anonymous function call

aggregate(loss~year, data = claim.data, FUN = function(x) max(x))
# year loss
#1 2000 143866
#2 2001 316487
#3 2002 186172
#4 2003 465357
#5 2004 233817
#6 2005 847532
#7 2006 294599
#8 2007 382048
#9 2008 1768795
#10 2009 686856
#11 2010 1356117
#12 2011 2093160
#13 2012 411778
#14 2013 2002641
#15 2014 289696
#16 2015 220853
#17 2016 428111
#18 2017 362979
#19 2018 335988
#20 2019 178828

Or apply on a fresh R session



Related Topics



Leave a reply



Submit