Skip to content
Prev 2235 / 12125 Next

[R-pkg-devel] Debian: example file is no longer read correctly

Dear all,

Thanks a lot for all your suggestions, which I was happy to include. All my coding skills are self-taught so I am especially thankful for suggestions on how to improve the code regarding defensiveness etc..

I have also followed I?aki's excellent suggestion and checked the package with the latest changes to the example (to circumvent the path-setting problems) using rhub using different platforms. 

I have submitted the version with the corrections again earlier and it is on its way to CRAN now.

Thanks again,
Christine



-----Urspr?ngliche Nachricht-----
Von: Georgi Boshnakov [mailto:georgi.boshnakov at manchester.ac.uk] 
Gesendet: Mittwoch, 20. Dezember 2017 14:49
An: G?ran Brostr?m; r-package-devel at r-project.org; Blume Christine
Betreff: RE: [R-pkg-devel] Debian: example file is no longer read correctly

The package on CRAN doesn't use tempdir()  to change directories. 
Given that the error occurs on some linux-es only, it may be worth a try to submit the package and see if these errors will resurface. 

It is difficult to debug errors on systems you don't have but some cleanup of your code might help your users and you.
In particular, write more defensively. For example, in your function (see a copy further bbelow) you may consider if it is fine to convert each file to a data.frame. Even if this is successful,  the data frame may not be what you expect.
 One good idea is to write exhaustive checks.

The check for nofiles > 0 was already suggested, another suggestion is after the signature below. 

Kind regards,
Georgi Boshnakov

The chunk:

=========
        if (ncol(data) == 2) {
            ...
        }
        if (ncol(data) == 3) {
              ... 
        }
==========

can be made more robust by simply adding an 'else' (note also the else if) to cover for a condition that "cannot" happen.

=========
        if (ncol(data) == 2) {
            ...
        }  else if (ncol(data) == 3) {
              ... 
        } else {
             stop(""I thought that this can never happen!")
       }
==========


=================

nparACT_flex_loop()
function (path, SR, cutoff = 1, minutes, plot = T, fulldays = T) {
    files <- list.files(path)
    fileext <- file_ext(files[1])
    nofiles <- length(files)
    bin_hr <- 60
    nparACT_result <- matrix(NA, nofiles, 9)
    nparACT_result <- as.data.frame(nparACT_result)
    colnames(nparACT_result) <- c("IS", "IV", "RA", "L5", "L5_starttime", 
        "M10", "M10_starttime", "Lflex", "Lflex_starttime")
    matrix_hraverage <- matrix(NA, nofiles, 24)
    for (zz in 1:nofiles) {
        name <- files[zz]
        if (fileext == "txt") {
            data <- read.table(paste(path, name, sep = "/"), 
                header = F)
        }
        else {
            data <- read.csv(paste(path, name, sep = "/"), header = F)
        }
        if (is.data.frame(data) == F) {
            data = as.data.frame(data)
        }
        if (ncol(data) == 2) {
            data[, 1] <- as.POSIXct(data[, 1])
            data[, 2] <- as.numeric(as.character(data[, 2]))
            names(data)[1] <- "time"
            names(data)[2] <- "activity"
        }
        if (ncol(data) == 3) {
            names(data)[1] <- "date"
            names(data)[2] <- "time"
            names(data)[3] <- "activity"
            data$date <- NULL
            data$time <- as.POSIXct(data$time, format = "%H:%M:%S")
            data$activity <- as.numeric(as.character(data$activity))
        }
        if (any(is.na(data$activity)) == TRUE) 
            stop("Please check your data! It must not contain NAs")
        a <- nrow(data)
        e <- SR * 60
        m <- bin_hr * SR * 60
        full_days <- floor(a/(e * bin_hr * 24))
        if (fulldays == T) {
            data <- data[1:(e * bin_hr * 24 * full_days), ]
        }
        a <- nrow(data)
        b <- floor(a/(SR * 60))
        nparACT_auxfunctions1$nparACT_filt(data, a, cutoff)
        if (SR != 1/60) {
            data_min <- nparACT_auxfunctions1$nparACT_data_min(b, 
                SR, data)
        }
        else {
            data_min <- data$activity
        }
        data_hrs <- nparACT_auxfunctions1$nparACT_data_hrs(data, 
            a, m)
        result_ISIV <- nparACT_ISIVfunctions$nparACT_ISIV(data_hrs, 
            bin_hr)
        IS <- result_ISIV[1]
        IV <- result_ISIV[2]
        nparACT_result[zz, 1] <- IS
        nparACT_result[zz, 2] <- IV
        minaverage <- nparACT_auxfunctions1$nparACT_minaverage(a, 
            data_min)
        if (plot == T) {
            hraverage_sorted <- nparACT_auxfunctions1$nparACT_hraverage_GA_loop(minaverage, 
                data, a, SR)
            matrix_hraverage[zz, ] <- hraverage_sorted
        }
        result_RA <- nparACT_RAfunctions$nparACT_L5M10Lflex(data, 
            minaverage, a, SR, minutes)
        result_RA <- as.data.frame(result_RA)
        nparACT_result[zz, 3] <- result_RA$RA
        nparACT_result[zz, 4] <- result_RA$L5
        nparACT_result[zz, 5] <- as.character(result_RA$L5_starttime)
        nparACT_result[zz, 6] <- result_RA$M10
        nparACT_result[zz, 7] <- as.character(result_RA$M10_starttime)
        nparACT_result[zz, 8] <- result_RA$Lflex
        nparACT_result[zz, 9] <- as.character(result_RA$Lflex_starttime)
    }
    nparACT_result <- nparACT_result
    if (plot == T) {
        nparACT_auxfunctions2$nparACT_plot_hraverage_GA_loop(matrix_hraverage)
    }
    return(nparACT_result)
}
<environment: namespace:nparACT>

-----Original Message-----
From: R-package-devel [mailto:r-package-devel-bounces at r-project.org] On Behalf Of G?ran Brostr?m
Sent: 20 December 2017 13:03
To: r-package-devel at r-project.org; Blume Christine
Subject: Re: [R-pkg-devel] Debian: example file is no longer read correctly
On 2017-12-20 12:14, G?ran Brostr?m wrote:
No, it is not. But when I ran R CMD check --as-cran ..., a file 'nparACT-Ex.timings' was created in 'nparACT.Rcheck/sleepstudy_example/', which your example reads, and disaster follows.

R CMD check (without --as-cran) avoids this problem, but CRAN will not accept it. Seems to be the writing to and reading from disc that is problematic and in need of some structure? Others may help with that.

G?ran
______________________________________________
R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel