Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dplyr negative select if df not null

I run an api extract each morning. There is a variable bla that may or may not be null. If it’s not null it is a dataframe that contains a field sessions, in which case I would like to deselect sessions.

Normally, bla is indeed a dataframe and not null and the following block runs fine:

bla |> ... dplyr chain here ... |> select(-sessions)

But in those cases where bla is just null, I need this code to still run and not buckle my workflow.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Tried:

bla |> ... dplyr chain here ... |> select(!any_of('sessions'))

But this errors with:

Error in UseMethod("select") : 
  no applicable method for 'select' applied to an object of class "NULL"

How can I tell dply to run the select command only if bla is not null?

>Solution :

There are couple of ways to deal with this. Use an if condition on exists (if we have created an object earlier in the step)

library(dplyr)
if(exists('bla')) {
      bla |> 
       select(!any_of('sessions'))
  }

Or wrap with tryCatch

tryCatch(bla |> 
      select(!any_of('sessions')), error = function(e) NULL)

Based on the OP’s update in comments, if this is part of a chain, we can check if the number of rows are greater than 0 before we select

ab_tests <- get_ga_df(viewId, c(start_date, end_date), ab_metrics, ab_dims) %>%  
     index_zen_name(dimension_lookup_marketing) %>%    
    dedup_key(c('session_id', 'date')) %>%  {  
      if(!is.null(.)) {          
        select(., !any_of('sessions'))
       } else .
   }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading