0

I have a dynamic automatic process where I pick up files, process them and left_join them together.

I don't always get all the files so sometimes my RHS left_join table is empty which consequently throws the error

Error: Not compatible with STRSXP: [type=NULL].

Consider following example

library(dplyr)
df <-structure(list(id = c(1, 2), var = c("a", "b")), class = "data.frame", row.names = c(NA, 
-2L))

df2 <- structure(list(), class = "data.frame", row.names = integer(0))

df %>% 
  left_join(df2, by=('id'))

Is there a simple way to ignore/suppress the error for the join if the RHS table is empty?

Thanks

CER
  • 854
  • 10
  • 22

2 Answers2

1

Thanks to the comments I came up with following simple approach which might help someone else in the future having a similar problem.


df %>% {
  tryCatch(left_join(.,df2, by = ('id')),
           error=function(e) .)}



CER
  • 854
  • 10
  • 22
0

Since if..else doesn't play well in piped statements, I would make a custom function to just return the (presumed) non-blank data frame. You may want to add a third condition if both are blank, but this should get you started:

left_join_if_exists = function(d1, d2, by) {
  if (0 %in% dim(d1)) {
    message("Warning: First data frame is empty")
    return(d2)
  } else if (0 %in% dim(d2)) {
    message("Warning: Second data frame is empty")
    return(d1)
  } else{
    return( left_join(d1,d2,by = by) )
  }
}

df %>% 
  left_join_if_exists( ., df2, by=c('id'))
GenesRus
  • 1,057
  • 6
  • 16