opensafely-core / r-docker

Docker image for running R scripts in OpenSAFELY
1 stars 3 forks source link

dplyr::mutate(dplyr::case_when()) erroring on server by not locally #77

Closed ciaranmci closed 2 years ago

ciaranmci commented 2 years ago

My error is with the dplyr::mutate( dplyr::case_when() ) call. The error from the job server says that "x must be a logical vector, not a Date object". This error doesn't arise when I run on my local machine or locally on my OpenSAFELY docker container.

My R syntax is:

mutate(
      date_death = case_when(
                             is.na(.$date_death_ons) & !is.na(.$date_death_cpns) ~ .$date_death_cpns,
                             is.na(.$date_death_cpns) & !is.na(.$date_death_ons) ~ .$date_death_ons,
                             is.na(.$date_death_ons) & is.na(.$date_death_cpns) ~ NA_Date_
                             )
      )

The error occured in a recent job run (ID: rkbgjwd6a3lczzwt)

Any ideas what might be causing the error? I'm really keen to have this output ready for the showcase on April 7th. Thanks for any help you can offer.

bloodearnest commented 2 years ago

@wjchulme @milanwiedemann perhaps you can help Ciaran out here?

ciaranmci commented 2 years ago

I think I fixed it with some help from @LFISHER7. I think @wjchulme is the "Will" referred to in the help. Essentially, "date_death_cpns is all NA. Unless you tell it otherwise, readr() (or similar) will import a column of NAs as a logical vector, so date_death_cpns will be logical. case_when() is strict about mixed vector types, so will give an error when you try to combine a logical with a date".

So, I explicitly declared date_death_cpns as a date and now everything works. Thanks, all