ropensci-archive / cleanEHR

:warning: ARCHIVED :warning: Essential tools and utility functions to facilitate the data processing pipeline, data cleaning and data analysing of clinical data from CC-HIC
GNU General Public License v3.0
54 stars 23 forks source link

Mistery with `xmlTime2POSIX` #159

Closed dpshelio closed 2 years ago

dpshelio commented 6 years ago

When called from a different package it fails in Windows but it works in Linux. However, if the input to the function is saved and then used independently then it fails in Linux too. Maybe I've done something wrong saving the data to use it later... but this is what I've done from https://github.com/ropensci/cleanEHR/blob/master/R/utilities.R#L144:

    if(!file.exists('times.Rdata')) {
        xmltime_group <- list(xml.time)
    } else {
        load(file='times.Rdata')
        xmltime_group[[ length(xmltime_group) + 1 ]] <- xml.time
   }
   save(xmltime_group, file='times.Rdata')

and then on https://github.com/ropensci/cleanEHR/blob/master/R/utilities.R#L150:

    if(!file.exists('times_r.Rdata')) {
        xmltime_res <- list(tp)
    } else {
        load(file='times_r.Rdata')
        xmltime_res[[ length(xmltime_res) + 1 ]] <- tp
   }
   save(xmltime_res, file='times_r.Rdata')

Run the code that passes times in, some of them of type "POSIXct" "POSIXct", whereas others as "numeric". And all seems all right. If then I open these files the input an run:

as.POSIXct(xmltime_res[[1]], "GMT", format="%Y-%m-%dT%H:%M:%S")

where the first element sometimes is a "POSIXct" where others is a "numeric" vector with values like: 1494576000 the numeric ones fail (as I would expect) because it doesn't know the "origin".

Error in as.POSIXct.numeric(input, "GMT",  format="%Y-%m-%dT%H:%M:%S") : 'origin' must be supplied

So could it be that the origin is somehow set somewhere and R picks it up? I've tried to name a variable origin and run it again and unless I explicitly use it in the function arguments it doesn't change the behaviour.

I've also try making origin as global using:

origin <<- as.POSIXct("1970-01-01", "GMT", format="%Y-%m-%d")

and with `assign(..., envir=.GlobalEnv), both got the same results.