Developer hint: Missing values

If while you are developing or modifying a function (server-side or client-side) the function appears to run OK but there are unexpected missings/NAs in the output make sure you that the function is not configured correctly to deal with missing values.

Example of problem
#If data (e.g. X) contain missing values (typically, NAs)
mean(X) #will return NA
mean(X,na.rm=TRUE) #will omit NAs and then do calculation correctly
#It is important to remember that if you are developing a function like tapply or lapply that
#itself calls functions like mean(), those will also return NAs if you don't make sure that
#that tapply/lapply calls the function with na.rm included.
#For example, see meanByGroupDS. The line:
FUN <- function (x) {UseMethod("mean")}
#returns NAs when lapply calls FUN
#So you must use:
FUN <- function(x) {mean(x,na.rm=TRUE)}

Paul Burton, 11th June 2015

DataSHIELD Wiki by DataSHIELD is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at