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

Georgi Boshnakov georgi.boshnakov at manchester.ac.uk
Wed Dec 20 14:49:10 CET 2017


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:
> Christine,
> 
> the error message from CRAN/r-devel-linux suggests that the error 
> comes from your function nparACT_flex_loop . There your code below is 
> embedded in a loop:
> 
> for (zz in 1:nofiles) ....
> 
> If nofiles == 0 (no found files), this loop is still executed (R is 
> not Fortran). Maybe this is the cause of the failure?

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

> 
> You should anyway use seq_len(nofiles) or seq_along(files) instead of 
> 1:nofiles, or check the  number of files found.
> 
> Göran
> 
> On 2017-12-20 11:11, Blume Christine wrote:
>> Dear all,
>>
>> I guess I need your help again. With debian my package (nparACT) 
>> gives an error message that seems to be related to loading the data:
>>
>>   Warning in is.na(data$activity) :
>>       is.na() applied to non-(list or vector) of type 'NULL'
>>      Error in 1:a : argument of length 0
>>
>> Although I have set up a virtual machine to test the package using 
>> the R devel version with Linux (Debian), it gives me a hard time to 
>> find out what is wrong since I do not manage to install packages 
>> using the devel version...
>>
>> Does anyone know what has changed so this text file is not read in 
>> any longer? The code for reading in the text file is:
>>
>> data <- read.table(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))
>>      }
>>
>> Or can someone tell me how to install the package using R devel on Linux?
>>
>> Thanks a lot!
>> Christine
>>
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-package-devel at r-project.org mailing list 
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>>
> 
> ______________________________________________
> R-package-devel at r-project.org mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-package-devel

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


More information about the R-package-devel mailing list