Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stringsAsFactors = TRUE needs to happen before R4.0.0 happens. See Hornik email. #8

Closed
swihart opened this issue Apr 24, 2020 · 15 comments

Comments

@swihart
Copy link
Owner

swihart commented Apr 24, 2020

Ok. Go to https://cran.r-project.org/web/checks/check_results_rmutil.html and click on devel-windows. Examples are bombing:

Looks like tvctomat when interaction = TRUE is the culprit because of the four lines below the 1st three are non-problematic but the 4th one errors:

print(tvc <- tvctomat(z, units="days"))
print(tvc2 <- tvctomat(v, oldtvc=tvc, units=NA))
print(tvc3 <- tvctomat(v, oldtvc=tvc, dataframe=FALSE, units=NA))
print(tvc4 <- tvctomat(tvc2, interaction=c("z","v")))
checking examples ...
running examples for arch 'i386' ... ERROR
Running examples in 'rmutil-Ex.R' failed
The error most likely occurred in:

> ### Name: tvctomat
> ### Title: Create a Time-varying, Intra-individual Covariate (tvcov) Object
> ### Aliases: tvctomat
> ### Keywords: manip
>
> ### ** Examples
>
> z <- matrix(rpois(20,5),ncol=5)
> print(tvc <- tvctomat(z, units="days"))
           
Names: z
Units: days
Number of individuals: 4
Number of observations: 20
Number of observations per individual:
 5 5 5 5
> covariates(tvc)
 [1] 4 3 6 6 6 4 8 2 4 11 5 9 3 7 4 8 6 3 5 7
> names(tvc)
[1] "z"
> v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5))
> print(tvc2 <- tvctomat(v, oldtvc=tvc, units=NA))
                
Names: z v
Units: days <NA>
Number of individuals: 4
Number of observations: 20
Number of observations per individual:
 5 5 5 5
> covariates(tvc2)
    z v
1 4 a
2 3 e
3 6 d
4 6 c
5 6 b
6 4 b
7 8 a
8 2 e
9 4 d
10 11 c
11 5 c
12 9 b
13 3 a
14 7 e
15 4 d
16 8 d
17 6 c
18 3 b
19 5 a
20 7 e
> print(tvc3 <- tvctomat(v, oldtvc=tvc, dataframe=FALSE, units=NA))
                               
Names: z vb vc vd ve
Units: days <NA> <NA> <NA> <NA>
Number of individuals: 4
Number of observations: 20
Number of observations per individual:
 5 5 5 5
> covariates(tvc3)
    z vb vc vd ve
1 4 0 0 0 0
2 3 0 0 0 1
3 6 0 0 1 0
4 6 0 1 0 0
5 6 1 0 0 0
6 4 1 0 0 0
7 8 0 0 0 0
8 2 0 0 0 1
9 4 0 0 1 0
10 11 0 1 0 0
11 5 0 1 0 0
12 9 1 0 0 0
13 3 0 0 0 0
14 7 0 0 0 1
15 4 0 0 1 0
16 8 0 0 1 0
17 6 0 1 0 0
18 3 1 0 0 0
19 5 0 0 0 0
20 7 0 0 0 1
> print(tvc4 <- tvctomat(tvc2, interaction=c("z","v")))
Error in dimnames(x) <- dn :
  length of 'dimnames' [2] not equal to array extent
Calls: print -> tvctomat -> colnames<-
Execution halted
running examples for arch 'x64' ... ERROR
Running examples in 'rmutil-Ex.R' failed
The error most likely occurred in:

> ### Name: tvctomat
> ### Title: Create a Time-varying, Intra-individual Covariate (tvcov) Object
> ### Aliases: tvctomat
> ### Keywords: manip
>
> ### ** Examples
>
> z <- matrix(rpois(20,5),ncol=5)
> print(tvc <- tvctomat(z, units="days"))
           
Names: z
Units: days
Number of individuals: 4
Number of observations: 20
Number of observations per individual:
 5 5 5 5
> covariates(tvc)
 [1] 4 3 6 6 6 4 8 2 4 11 5 9 3 7 4 8 6 3 5 7
> names(tvc)
[1] "z"
> v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5))
> print(tvc2 <- tvctomat(v, oldtvc=tvc, units=NA))
                
Names: z v
Units: days <NA>
Number of individuals: 4
Number of observations: 20
Number of observations per individual:
 5 5 5 5
> covariates(tvc2)
    z v
1 4 a
2 3 e
3 6 d
4 6 c
5 6 b
6 4 b
7 8 a
8 2 e
9 4 d
10 11 c
11 5 c
12 9 b
13 3 a
14 7 e
15 4 d
16 8 d
17 6 c
18 3 b
19 5 a
20 7 e
> print(tvc3 <- tvctomat(v, oldtvc=tvc, dataframe=FALSE, units=NA))
                               
Names: z vb vc vd ve
Units: days <NA> <NA> <NA> <NA>
Number of individuals: 4
Number of observations: 20
Number of observations per individual:
 5 5 5 5
> covariates(tvc3)
    z vb vc vd ve
1 4 0 0 0 0
2 3 0 0 0 1
3 6 0 0 1 0
4 6 0 1 0 0
5 6 1 0 0 0
6 4 1 0 0 0
7 8 0 0 0 0
8 2 0 0 0 1
9 4 0 0 1 0
10 11 0 1 0 0
11 5 0 1 0 0
12 9 1 0 0 0
13 3 0 0 0 0
14 7 0 0 0 1
15 4 0 0 1 0
16 8 0 0 1 0
17 6 0 1 0 0
18 3 1 0 0 0
19 5 0 0 0 0
20 7 0 0 0 1
> print(tvc4 <- tvctomat(tvc2, interaction=c("z","v")))
Error in dimnames(x) <- dn :
  length of 'dimnames' [2] not equal to array extent
Calls: print -> tvctomat -> colnames<-
Execution halted
@swihart
Copy link
Owner Author

swihart commented Apr 24, 2020

tvctomat() lives in ./R/objectrm.r (see chunk below). It has it's own ./man/.Rd file. Find where interaction = TRUE comes into play …

1st instance of data.frame inside tvctomat is line 931 where the interaction if() statement starts on line 1022. So apparently the calls to data.frame before the interaction are ok. Something about the calls inside the interaction if() is triggering the error…

I seem to remember that rmutil has its own definition of data.frame some where…let's see… ah yes, it is in the same file, objectrm.r on line near the bottom. It is not data.frame but as.data.frame…does as.data.frame have a stringAsFactors arugument? Answer: Yes.

Ergh. So what's the recommended fix, here. I have a lot of calls to data.frame. Is it possible that interaction==TRUE only calls as.data.frame...(?) Answer: No. And it is interesting:

Interaction if() loop goes from line 1022 to 1133. Doing a ctrl+f in that region, all 8 times data.frame appear are apart of is.data.frame, which has no argument for stringsAsFactor. Also, the interaction if() loop has a special ifelse if the length of interaction is 2.

Best case scenario: we put in the stringsAsFactors=TRUE into the as.data.frame() definition and it propogates everywhere it is used and it solves our problems. Worse case … find every instance of data.frame and add in the argument. First things first.
Let's do a (after consulting https://twitter.com/bruce_swihart/status/1091371985509277697) devtools::check_win_devel() and reproduce error. Do that and then report back.

### function to create a time-varying covariate (tvcov) object
###
tvctomat <- function(tvcov, names=NULL, units=NULL, interaction=NULL,
	ccov=NULL, oldtvcov=NULL, dataframe=TRUE, description=NULL){
#
# check for compatibility
#
if(inherits(tvcov,"tvcov")&&inherits(oldtvcov,"tvcov")){
	if(length(tvcov$nobs)!=length(oldtvcov$nobs)||
		any(tvcov$nobs!=oldtvcov$nobs))
		stop("incompatible tvcov objects")
	if(!is.null(oldtvcov$units)||!is.null(tvcov$units)){
		if(is.null(oldtvcov$units))
			oldtvcov$units <- rep(NA,dim(oldtvcov$tvcov)[2])
		if(is.null(tvcov$units))
			tvcov$units <- rep(NA,dim(tvcov$tvcov)[2])
		oldtvcov$units <- c(oldtvcov$units,tvcov$units)}
	oldtvcov$tvcov <- cbind(oldtvcov$tvcov,tvcov$tvcov)
	return(oldtvcov)}
nbs <- tvcv <- NULL
if(is.data.frame(tvcov)){
	if(is.null(names))names <- paste(deparse(substitute(tvcov)))
	if(length(names)!=1)stop("too many names")
	if(dataframe){
	# make new one-column dataframe
		nbs <- rep(dim(tvcov)[2],dim(tvcov)[1])
		tvcv <- as.data.frame(as.vector(t(as.matrix(tvcov))))
		colnames(tvcv) <- names}
	# if factors, as.matrix transforms to character for next step
	else tvcov <- as.matrix(tvcov)}
if(is.matrix(tvcov)&&is.character(tvcov)){
	nbs <- rep(dim(tvcov)[2],dim(tvcov)[1])
	if(is.null(names))names <- paste(deparse(substitute(tvcov)))
	if(length(names)!=1)stop("too many names")
	tvcv <- as.factor(as.vector(t(tvcov)))
	if(dataframe){
	# make new one-column dataframe
		tvcv <- as.data.frame(as.vector(t(as.matrix(tvcov))))
		colnames(tvcv) <- names}
	else {
	# make indicator matrix from factor
		mt <- terms(~tvcv)
		tmp3 <- dimnames(get(getOption("contrasts")[[if(is.ordered(tvcv))2 else 1]])(levels(tvcv),contrasts =TRUE))[[2]]
		if(is.null(tmp3))tmp3 <- 1:(length(levels(tvcv))-1)
		names <- paste(names,tmp3,sep="")
#		names <- paste(names,levels(tvcv)[-1],sep="")
		tvcv <- model.matrix(mt,model.frame(mt,na.action=NULL))[,-1,drop=FALSE]
		colnames(tvcv) <- names
		if(!is.null(units))units <- rep(units,dim(tvcv)[2])}}
else if(is.matrix(tvcov)){
#
# transform to a vector
#
	nbs <- rep(dim(tvcov)[2],dim(tvcov)[1])
	tvcv <- matrix(as.vector(t(tvcov)),ncol=1)
	if(!is.null(names)&&length(names)!=1)stop("too many names")
	colnames(tvcv) <- if(!is.null(names)) names
		else paste(deparse(substitute(tvcov)))}
else if(is.list(tvcov)&&!is.data.frame(tvcov)){
	if(inherits(tvcov,"tvcov")){
		nbs <- tvcov$nobs
		tvcv <- tvcov$tvcov}
	else {
		ncols <- dim(as.data.frame(tvcov[[1]]))[2]
		# create names
		if(is.null(names)){
			if(is.null(colnames(tvcov[[1]]))){
				names <- if(is.matrix(tvcov[[1]]))paste(paste(deparse(substitute(tvcov))),1:ncols,sep="")
				else paste(deparse(substitute(tvcov)))}
			else names <- colnames(tvcov[[1]])}
		# concatenate list elements into a matrix
		ff <- TRUE
		for(i in tvcov){
		# create one big dataframe
			i <- as.data.frame(i)
			if(dim(i)[2]!=ncols)
				stop("all elements of the list must have the same number of columns")
			nbs <- c(nbs,dim(i)[1])
			if(ff){
				tvcv <- i
				ff <- FALSE}
			else tvcv <- rbind(tvcv,i)}
		if(is.character(tvcv)||!dataframe){
		# if necessary, transform to indicator matrix
			tmp <- tmp2 <- mt <- NULL
			for(i in 1:ncols){
				if(is.numeric(tvcv[,i])){
					tmp <- cbind(tmp,tvcv[,i])
					tmp2 <- c(tmp2,names[i])}
				else {
					mt <- terms(~tvcv[,i])
					tmp <- cbind(tmp,model.matrix(mt,model.frame(mt,na.action=NULL))[,-1,drop=FALSE])
					tmp3 <- dimnames(get(getOption("contrasts")[[if(is.ordered(tvcv[,i]))2 else 1]])(levels(tvcv[,i]),contrasts =TRUE))[[2]]
					if(is.null(tmp3))tmp3 <- 1:(length(levels(tvcv[,i]))-1)
					tmp2 <- c(tmp2,paste(names[i],tmp3,sep=""))}}
#					tmp2 <- c(tmp2,paste(names[i],levels(as.factor(tvcv[,i]))[-1],sep=""))}}
			tvcv <- tmp
			names <- tmp2
			ncols <- length(names)
			rm(tmp,tmp2,mt)}
		# create colnames if necessary
		if(is.null(colnames(tvcov[[1]]))){
			if(is.null(names))names <- paste(deparse(substitute(tvcov)))}
		else if(length(names)!=ncols)names <- colnames(tvcov[[1]])
		if((length(names)==1&&ncols>1))
			names <- paste(names,1:ncols,sep="")
		if(length(names)!=ncols)
			stop(paste(ncols,"variable names required"))
		colnames(tvcv) <- names}}
else if(!is.data.frame(tvcov))
	stop("Intra-unit (time-varying) covariates must be a matrix, dataframe, or list")
if(!is.null(interaction)){
#
# if necessary, create interactions
#
	name <- colnames(tvcv)
	units <- tvcov$units
	if(is.character(interaction)){
	# if names supplied, find corresponding columns
		mat <- match(interaction,name)
		if(any(is.na(mat)))
			stop(paste("Intra-unit (time-varying) covariate(s)",ccov[is.na(mat)],"not found"))
		interaction <- mat}
	if(is.vector(interaction,mode="numeric")){
		if(length(interaction)>length(name))
			stop("too many interactions")
		if(!is.data.frame(tvcv))colnames(tvcv) <- NULL
		if(!is.null(ccov)){
		# if interactions with time-constant covariates
			if(inherits(ccov,"tccov")){
			  ## bruce swihart edit:
			  ## switch the next two lines to avoid
			  ## R CMD Check error
			  ## in examples of tvctomat.Rd
			  units2 <- ccov$units
			  ccov <- ccov$ccov
			  if(!is.null(units)&&is.null(units2))
					units2 <- rep("NA",dim(ccov)[2])
				if(is.null(units)&&!is.null(units2))
					units <- rep("NA",dim(tvcov$tvcov)[2])}
			if(!is.matrix(ccov)&&!is.data.frame(ccov)){
			# if a vector, transform to matrix
				tmp <- paste(deparse(substitute(ccov)))
				ccov <- matrix(ccov)
				colnames(ccov) <- tmp}
			if(dim(ccov)[1]!=length(nbs))
				stop("ccov does not have one observation per individual")
			# find desired covariates
			if(is.null(names))names <- colnames(ccov)
			mat <- match(names,colnames(ccov))
			if(any(is.na(mat)))stop(paste("covariates",names[is.na(mat)],"not found"))
			oldtvcov <- tvcv
			if(!is.data.frame(oldtvcov))colnames(oldtvcov) <- name
			if(!is.data.frame(oldtvcov)&&!is.data.frame(ccov)){
			# calculate interactions for ordinary matrices
				for(i in 1:length(interaction))for(j in 1:length(mat)){
					oldtvcov <- cbind(oldtvcov,tvcv[,interaction[i]]*rep(ccov[,mat[j]],nbs))
					name <-  c(name,paste(name[interaction[i]],".",names[j],sep=""))
					if(!is.null(units))units <- c(units,paste(units[interaction[i]],".",units2[j],sep=""))}}
			else {
			# calculate interactions when expansion required
				for(i in 1:length(interaction))
					for(j in 1:length(mat)){
					mt <- terms(~tvcv[,interaction[i]]:rep(ccov[,mat[j]],nbs))
					tmp <- model.matrix(mt,model.frame(mt,na.action=NULL))[,-1,drop=FALSE]
			                if(!is.vector(tvcv[,interaction[i]],mode="numeric")){
			                	if(!is.vector(ccov[,mat[j]],mode="numeric")){
			                		nam <- NULL
			                		tmp2 <- paste(name[interaction[i]],levels(tvcv[,interaction[i]])[-1],".",sep="")
			                		for(k in 1:length(levels(ccov[,mat[j]])[-1]))
			                		nam <- c(nam,paste(tmp2,names[j],levels(ccov[,mat[j]])[-1][k],sep=""))
			                		tmp <- tmp[,-c(1:length(levels(tvcv[,interaction[i]])),seq(1,length(levels(tvcv[,interaction[i]]))*length(levels(ccov[,mat[j]])),by=length(levels(tvcv[,interaction[i]])))),drop=FALSE]}
			                	else {
			                		nam <- paste(paste(name[interaction[i]],levels(tvcv[,interaction[i]])[-1],sep=""),".",names[j],sep="")
			                		tmp <- tmp[,-1,drop=FALSE]}
							if(!is.null(units))units <- c(units,rep(units2[j],length(levels(tvcv[,interaction[i]])[-1])))}
			                else {
			                	if(!is.vector(ccov[,mat[j]],mode="numeric")){
			                		nam <- paste(name[interaction[i]],".",paste(names[j],levels(ccov[,mat[j]])[-1],sep=""),sep="")
			                		tmp <- tmp[,-1,drop=FALSE]
							if(!is.null(units))units <- c(units,rep(units[interaction[i]],length(levels(ccov[,mat[j]])[-1])))}
			                	else {
							nam <- paste(name[interaction[i]],".",names[j],sep="")
							if(!is.null(units))units <- c(units,paste(units[interaction[i]],".",units2[j],sep=""))}}
			                colnames(tmp) <- nam
					name <- c(name,nam)
					oldtvcov <- cbind(oldtvcov,tmp)}}
			if(!is.data.frame(oldtvcov))colnames(oldtvcov) <- name
			oldtvcov <- list(tvcov=oldtvcov,nobs=nbs,units=units)}
		else if(length(interaction)==2){
		# one pair of interactions
			if(is.data.frame(tvcv)){
			# expand dataframe
				mt <- terms(~tvcv[,interaction[1]]:tvcv[,interaction[2]])
				tmp <- model.matrix(mt,model.frame(mt,na.action=NULL))[,-1,drop=FALSE]
				if(!is.vector(tvcv[,interaction[1]],mode="numeric")){
					if(!is.vector(tvcv[,interaction[2]],mode="numeric")){
						names <- NULL
						tmp2 <- paste(name[interaction[1]],levels(tvcv[,interaction[1]])[-1],".",sep="")
						for(i in 1:length(levels(tvcv[,interaction[2]])[-1]))
						names <- c(names,paste(tmp2,name[interaction[2]],levels(tvcv[,interaction[2]])[-1][i],sep=""))
						tmp <- tmp[,-c(1:length(levels(tvcv[,interaction[1]])),seq(1,length(levels(tvcv[,interaction[1]]))*length(levels(tvcv[,interaction[2]])),by=length(levels(tvcv[,interaction[1]])))),drop=FALSE]}
					else {
						names <- paste(paste(name[interaction[1]],levels(tvcv[,interaction[1]])[-1],sep=""),".",name[interaction[2]],sep="")
						tmp <- tmp[,-1,drop=FALSE]
						if(!is.null(units))units <- c(units,paste(rep(units[interaction[1]],length(levels(tvcv[,interaction[1]])[-1])),".",units[interaction[2]],sep=""))}}
				else {
					if(!is.vector(tvcv[,interaction[2]],mode="numeric")){
						names <- paste(name[interaction[1]],".",paste(name[interaction[2]],levels(tvcv[,interaction[2]])[-1],sep=""),sep="")
						tmp <- tmp[,-1,drop=FALSE]
						if(!is.null(units))units <- c(units,paste(units[interaction[1]],".",rep(units[interaction[2]],length(levels(tvcv[,interaction[2]])[-1])),sep=""))}
					else {
						names <- paste(name[interaction[1]],".",name[interaction[2]],sep="")
						if(!is.null(units))units <- c(units,paste(units[interaction[1]],".",units[interaction[2]],sep=""))}}
				colnames(tmp) <- names
				oldtvcov <- list(tvcov=cbind(tvcv,tmp),nobs=nbs,units=units)}
			else {
				units <- if(is.null(tvcov$units))NULL else c(tvcov$units,paste(tvcov$units[interaction[1]],".",tvcov$units[interaction[2]],sep=""))
				oldtvcov <- list(tvcov=cbind(tvcv,tvcv[,interaction[1]]*tvcv[,interaction[2]]),nobs=nbs,units=units)}
			if(!is.data.frame(oldtvcov$tvcov))
				colnames(oldtvcov$tvcov) <- c(name,paste(name[interaction[1]],".",name[interaction[2]],sep=""))}
		else stop("interaction must be a vector containing column numbers or variable names")
		class(oldtvcov) <- "tvcov"}}
else if(!is.null(oldtvcov)){
#
# check for variable descriptions
#
if(!is.null(description)){
	if(!is.list(description))stop("description must be a list")
	if(!all(tmp <- names(description)%in%colnames(tvcv)))
		stop(paste("variable(s)",names(description)[!tmp],"not found"))
	for(i in description)if(!is.character(i))
		stop("description list must contain character vectors")}
#
# if old tvcov, combine with new one
#
	if(!inherits(oldtvcov,"tvcov"))
		stop("oldtvcov must have class, tvcov")
	oldtvcov$description <- c(oldtvcov$description,description)
	if(!is.null(oldtvcov$units)||!is.null(units)){
		if(is.null(oldtvcov$units))
			oldtvcov$units <- rep(NA,dim(oldtvcov$tvcov)[2])
		if(is.null(units))units <- rep(NA,dim(tvcv)[2])
		oldtvcov$units <- c(oldtvcov$units,units)}
	if((dim(oldtvcov$tvcov)[1]==dim(tvcv)[1])&&all(oldtvcov$nobs==nbs)){
		if(dataframe)oldtvcov$tvcov <- data.frame(oldtvcov$tvcov,tvcv)
		else oldtvcov$tvcov <- cbind(oldtvcov$tvcov,tvcv)}
	else stop("old and new covariates do not have the same numbers of observations")}
else {
	# check units
	if(!is.null(units)){
		if(!is.character(units))
			stop("units must be a character vector")
		if(length(units)!=dim(tvcv)[2])
			stop("units must be supplied for all covariates")}
	if(dataframe)tvcv <- as.data.frame(tvcv)
	oldtvcov <- list(tvcov=tvcv,nobs=nbs,units=units,description=description)
	class(oldtvcov) <- "tvcov"}
if(!is.null(oldtvcov$units))names(oldtvcov$units) <- colnames(oldtvcov$tvcov)
#
# if no factor variables present, return a matrix anyway
#
if(is.data.frame(oldtvcov$tvcov)){
	fac <- FALSE
	for(i in 1:dim(oldtvcov$tvcov)[2])if(!is.vector(oldtvcov$tvcov[,i],mode="numeric")){
		fac <- TRUE
		break}
	if(!fac)oldtvcov$tvcov <- as.matrix(oldtvcov$tvcov)}
oldtvcov}

@swihart
Copy link
Owner Author

swihart commented Apr 24, 2020

Okay, I have reproduced the error so easily with rhub!

> check_with_rdevel()
─  Building package
─  Uploading package
─  Preparing build, see status at
   https://builder.r-hub.io/status/rmutil_1.1.4.tar.gz-01fb7789abcd459f8921383fb921acc2
─  Build started
─  Downloading and unpacking package file
─  Querying system requirements
─  Installing system requirements
─  Starting Docker container
─  Querying package dependencies
─  Installing package dependencies
─  Running R CMD check
   About to run xvfb-run R CMD check rmutil_1.1.4.tar.gz
─  using log directory ‘/home/docker/rmutil.Rcheck’ (707ms)
─  using R Under development (unstable) (2020-04-18 r78254)
─  using platform: x86_64-pc-linux-gnu (64-bit)
─  using session charset: UTF-8
✓  checking for file ‘rmutil/DESCRIPTION’
─  this is package ‘rmutil’ version ‘1.1.4’
─  package encoding: UTF-8
✓  checking package namespace information
✓  checking package dependencies (2.9s)
✓  checking if this is a source package
✓  checking if there is a namespace
✓  checking for executable files
✓  checking for hidden files and directories (851ms)
✓  checking for portable file names
✓  checking for sufficient/correct file permissions
✓  checking whether package ‘rmutil’ can be installed (21.1s)
✓  checking installed package size
✓  checking package directory
✓  checking DESCRIPTION meta-information (700ms)
✓  checking top-level files
✓  checking for left-over files
✓  checking index information
✓  checking package subdirectories
✓  checking R files for non-ASCII characters
✓  checking R files for syntax errors
✓  checking whether the package can be loaded (835ms)
✓  checking whether the package can be loaded with stated dependencies
✓  checking whether the package can be unloaded cleanly
✓  checking whether the namespace can be loaded with stated dependencies
✓  checking whether the namespace can be unloaded cleanly (711ms)
✓  checking loading without being on the library search path
✓  checking dependencies in R code (2.2s)
✓  checking S3 generic/method consistency (872ms)
✓  checking replacement functions (711ms)
✓  checking foreign function calls (2.2s)
✓  checking R code for possible problems (32.7s)
✓  checking Rd files (708ms)
✓  checking Rd metadata
✓  checking Rd cross-references
✓  checking for missing documentation entries (796ms)
✓  checking for code/documentation mismatches (700ms)
✓  checking Rd \usage sections (2.3s)
✓  checking Rd contents
✓  checking for unstated dependencies in examples (708ms)
✓  checking line endings in C/C++/Fortran sources/headers
✓  checking compiled code (818ms)
E  checking examples (1.6s)
   Running examples in ‘rmutil-Ex.R’ failed
   The error most likely occurred in:
   
   > ### Name: tvctomat
   > ### Title: Create a Time-varying, Intra-individual Covariate (tvcov) Object
   > ### Aliases: tvctomat
   > ### Keywords: manip
   > 
   > ### ** Examples
   > 
   > z <- matrix(rpois(20,5),ncol=5)
   > print(tvc <- tvctomat(z, units="days"))
              
   Names: z   
   Units: days
   Number of individuals:                4 
   Number of observations:               20 
   Number of observations per individual:
    5 5 5 5 
   > covariates(tvc)
    [1]  4  3  6  6  6  4  8  2  4 11  5  9  3  7  4  8  6  3  5  7
   > names(tvc)
   [1] "z"
   > v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5))
   > print(tvc2 <- tvctomat(v, oldtvc=tvc, units=NA))
                   
   Names: z    v   
   Units: days <NA>
   Number of individuals:                4 
   Number of observations:               20 
   Number of observations per individual:
    5 5 5 5 
   > covariates(tvc2)
       z v
   1   4 a
   2   3 e
   3   6 d
   4   6 c
   5   6 b
   6   4 b
   7   8 a
   8   2 e
   9   4 d
   10 11 c
   11  5 c
   12  9 b
   13  3 a
   14  7 e
   15  4 d
   16  8 d
   17  6 c
   18  3 b
   19  5 a
   20  7 e
   > print(tvc3 <- tvctomat(v, oldtvc=tvc, dataframe=FALSE, units=NA))
                                  
   Names: z    vb   vc   vd   ve  
   Units: days <NA> <NA> <NA> <NA>
   Number of individuals:                4 
   Number of observations:               20 
   Number of observations per individual:
    5 5 5 5 
   > covariates(tvc3)
       z vb vc vd ve
   1   4  0  0  0  0
   2   3  0  0  0  1
   3   6  0  0  1  0
   4   6  0  1  0  0
   5   6  1  0  0  0
   6   4  1  0  0  0
   7   8  0  0  0  0
   8   2  0  0  0  1
   9   4  0  0  1  0
   10 11  0  1  0  0
   11  5  0  1  0  0
   12  9  1  0  0  0
   13  3  0  0  0  0
   14  7  0  0  0  1
   15  4  0  0  1  0
   16  8  0  0  1  0
   17  6  0  1  0  0
   18  3  1  0  0  0
   19  5  0  0  0  0
   20  7  0  0  0  1
   > print(tvc4 <- tvctomat(tvc2, interaction=c("z","v")))
   Error in dimnames(x) <- dn : 
     length of 'dimnames' [2] not equal to array extent
   Calls: print -> tvctomat -> colnames<-
   Execution halted
✓  checking PDF version of manual (4.5s)
   
─  Done with R CMD check
─  Saving artifacts

@swihart
Copy link
Owner Author

swihart commented Apr 24, 2020

I noticed that the example for has a data.frame call in it -- what if all my problems are gone if I just change the argument in that one, single, data.frame() call?

\examples{
z <- matrix(rpois(20,5),ncol=5)
print(tvc <- tvctomat(z, units="days"))
covariates(tvc)
names(tvc)
v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5))

Change the last line in chunk above to

v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5), stringsAsFactors = TRUE)

and rerun:

it bombed. ok, comment the offending line and everything after it and resubmit. spoiler alert: everything is ok! so, need to look inside interaction if() loop.

> check_with_rdevel()
─  Building package
─  Uploading package
─  Preparing build, see status at
   https://builder.r-hub.io/status/rmutil_1.1.4.tar.gz-24ca8aa9c6e94138a808f50f478490c4
─  Build started
─  Downloading and unpacking package file
─  Querying system requirements
─  Installing system requirements
─  Starting Docker container
─  Querying package dependencies
─  Installing package dependencies
─  Running R CMD check
   About to run xvfb-run R CMD check rmutil_1.1.4.tar.gz
─  using log directory ‘/home/docker/rmutil.Rcheck’ (834ms)
─  using R Under development (unstable) (2020-04-18 r78254)
─  using platform: x86_64-pc-linux-gnu (64-bit)
─  using session charset: UTF-8
✓  checking for file ‘rmutil/DESCRIPTION’
─  this is package ‘rmutil’ version ‘1.1.4’
─  package encoding: UTF-8
✓  checking package namespace information
✓  checking package dependencies (4s)
✓  checking if this is a source package
✓  checking if there is a namespace
✓  checking for executable files
✓  checking for hidden files and directories
✓  checking for portable file names
✓  checking for sufficient/correct file permissions
✓  checking whether package ‘rmutil’ can be installed (30.2s)
✓  checking installed package size
✓  checking package directory
✓  checking DESCRIPTION meta-information
✓  checking top-level files
✓  checking for left-over files
✓  checking index information
✓  checking package subdirectories
✓  checking R files for non-ASCII characters
✓  checking R files for syntax errors (800ms)
✓  checking whether the package can be loaded
✓  checking whether the package can be loaded with stated dependencies
✓  checking whether the package can be unloaded cleanly (714ms)
✓  checking whether the namespace can be loaded with stated dependencies
✓  checking whether the namespace can be unloaded cleanly (887ms)
✓  checking loading without being on the library search path
✓  checking dependencies in R code (2.3s)
✓  checking S3 generic/method consistency (710ms)
✓  checking replacement functions (707ms)
✓  checking foreign function calls (3.1s)
✓  checking R code for possible problems (38.9s)
✓  checking Rd files (1.5s)
✓  checking Rd metadata
✓  checking Rd cross-references (1.4s)
✓  checking for missing documentation entries
✓  checking for code/documentation mismatches (1.6s)
✓  checking Rd \usage sections (2.2s)
✓  checking Rd contents (1.6s)
✓  checking for unstated dependencies in examples
✓  checking line endings in C/C++/Fortran sources/headers
✓  checking compiled code (702ms)
✓  checking examples (2.2s)
✓  checking PDF version of manual (6s)
   
─  Done with R CMD check
─  Saving artifacts

@swihart
Copy link
Owner Author

swihart commented Apr 24, 2020

Okay, slowly peeled off the commented code and it turns out, the only offending line is when
tvctomat has an interaction of length 2. Start diagnosing.

\examples{
z <- matrix(rpois(20,5),ncol=5)
print(tvc <- tvctomat(z, units="days"))
covariates(tvc)
names(tvc)
v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5), stringsAsFactors = FALSE)
print(tvc2 <- tvctomat(v, oldtvc=tvc, units=NA))
covariates(tvc2)
print(tvc3 <- tvctomat(v, oldtvc=tvc, dataframe=FALSE, units=NA))
covariates(tvc3)
#print(tvc4 <- tvctomat(tvc2, interaction=c("z","v")))
#covariates(tvc4)
x1 <- 1:4
x2 <- gl(4,1)
xx <- tcctomat(data.frame(x1,x2), dataframe=FALSE)
tvctomat(tvc3, interaction="z", ccov=xx)
tvctomat(tvc3, interaction="z", ccov=xx, names="x1")
tvctomat(tvc3, interaction="z", ccov=xx, names=c("x22","x23","x24"))
xx <- tcctomat(data.frame(x1,x2), dataframe=TRUE)
tvctomat(tvc2, interaction="z", ccov=xx)
tvctomat(tvc2, interaction="z", ccov=xx, names="x1")
tvctomat(tvc2, interaction="z", ccov=xx, names="x2")
}

@swihart
Copy link
Owner Author

swihart commented Apr 24, 2020

Googled `Error in dimnames(x) <- dn :
length of 'dimnames' [2] not equal to array extent'

And got this closed SO thread: https://stackoverflow.com/q/32464829/2727349

Will focuse on colnames() calls in the interaction length =2 portion.

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

So I've put in some print statements and am running things without errors (locally, with SFT+CMD+B and then running a line from the example) and against dev externally with rhub::check_with_rdevel(). Getting the following print statements indicating that something is up with "levels" which is called when making the names:

The debugging print-statements laden chunk:

		else if(length(interaction)==2){ 
		# one pair of interactions
			if(is.data.frame(tvcv)){print(paste0(rep(" BRUCE GET IN HERE 1102 ",50)))
			# expand dataframe
				mt <- terms(~tvcv[,interaction[1]]:tvcv[,interaction[2]])
				tmp <- model.matrix(mt,model.frame(mt,na.action=NULL))[,-1,drop=FALSE]
				if(!is.vector(tvcv[,interaction[1]],mode="numeric")){print(paste0(rep(" BRUCE GET IN HERE 1106 ",50)))
					if(!is.vector(tvcv[,interaction[2]],mode="numeric")){print(paste0(rep(" BRUCE GET IN HERE 1107 ",50)))
						names <- NULL
						tmp2 <- paste(name[interaction[1]],levels(tvcv[,interaction[1]])[-1],".",sep="")
						for(i in 1:length(levels(tvcv[,interaction[2]])[-1]))
						names <- c(names,paste(tmp2,name[interaction[2]],levels(tvcv[,interaction[2]])[-1][i],sep=""))
						tmp <- tmp[,-c(1:length(levels(tvcv[,interaction[1]])),seq(1,length(levels(tvcv[,interaction[1]]))*length(levels(tvcv[,interaction[2]])),by=length(levels(tvcv[,interaction[1]])))),drop=FALSE]}
					else {print(paste0(rep(" BRUCE GET IN HERE 1113 ",50)))
						names <- paste(paste(name[interaction[1]],levels(tvcv[,interaction[1]])[-1],sep=""),".",name[interaction[2]],sep="")
						tmp <- tmp[,-1,drop=FALSE]
						if(!is.null(units))units <- c(units,paste(rep(units[interaction[1]],length(levels(tvcv[,interaction[1]])[-1])),".",units[interaction[2]],sep=""))}}
				else {print(paste0(rep(" BRUCE GET IN HERE 1117 ",50)))
					if(!is.vector(tvcv[,interaction[2]],mode="numeric")){print(paste0(rep(" BRUCE GET IN HERE 1118 ",50)));
					  print("line 1119 -- Here are the levels: `levels(tvcv[,interaction[2]])[-1]`"); print(levels(tvcv[,interaction[2]])[-1])
						names <- paste(name[interaction[1]],".",paste(name[interaction[2]],levels(tvcv[,interaction[2]])[-1],sep=""),sep="")
						tmp <- tmp[,-1,drop=FALSE]; print(" line 1121 -- here are the names:"); print(names); print(tmp); print(units[interaction[1]]);
						if(!is.null(units))units <- c(units,paste(units[interaction[1]],".",rep(units[interaction[2]],length(levels(tvcv[,interaction[2]])[-1])),sep=""))}
					else {print(paste0(rep(" BRUCE GET IN HERE 1122 ",50)))
						names <- paste(name[interaction[1]],".",name[interaction[2]],sep="")
						if(!is.null(units))units <- c(units,paste(units[interaction[1]],".",units[interaction[2]],sep=""))}}
				colnames(tmp) <- names
				oldtvcov <- list(tvcov=cbind(tvcv,tmp),nobs=nbs,units=units)}
			else {print(paste0(rep(" BRUCE GET IN HERE Line 1127",50)))
				units <- if(is.null(tvcov$units))NULL else c(tvcov$units,paste(tvcov$units[interaction[1]],".",tvcov$units[interaction[2]],sep=""))
				oldtvcov <- list(tvcov=cbind(tvcv,tvcv[,interaction[1]]*tvcv[,interaction[2]]),nobs=nbs,units=units)}
			if(!is.data.frame(oldtvcov$tvcov))
				colnames(oldtvcov$tvcov) <- c(name,paste(name[interaction[1]],".",name[interaction[2]],sep=""))}
		else stop("interaction must be a vector containing column numbers or variable names")
		class(oldtvcov) <- "tvcov"}}

With errors against devel:

   [1] "line 1119 -- Here are the levels: `levels(tvcv[,interaction[2]])[-1]`"
   NULL
   [1] " line 1121 -- here are the names:"
   [1] "z.v"
      tvcv[, interaction[1]]:tvcv[, interaction[2]]b
   1                                               0
   2                                               0
   3                                               0
   4                                               0
   5                                               6
   6                                               4
   7                                               0
   8                                               0
   9                                               0
   10                                              0
   11                                              0
   12                                              9
   13                                              0
   14                                              0
   15                                              0
   16                                              0
   17                                              0
   18                                              3
   19                                              0
   20                                              0
      tvcv[, interaction[1]]:tvcv[, interaction[2]]c
   1                                               0
   2                                               0
   3                                               0
   4                                               6
   5                                               0
   6                                               0
   7                                               0
   8                                               0
   9                                               0
   10                                             11
   11                                              5
   12                                              0
   13                                              0
   14                                              0
   15                                              0
   16                                              0
   17                                              6
   18                                              0
   19                                              0
   20                                              0
      tvcv[, interaction[1]]:tvcv[, interaction[2]]d
   1                                               0
   2                                               0
   3                                               6
   4                                               0
   5                                               0
   6                                               0
   7                                               0
   8                                               0
   9                                               4
   10                                              0
   11                                              0
   12                                              0
   13                                              0
   14                                              0
   15                                              4
   16                                              8
   17                                              0
   18                                              0
   19                                              0
   20                                              0
      tvcv[, interaction[1]]:tvcv[, interaction[2]]e
   1                                               0
   2                                               3
   3                                               0
   4                                               0
   5                                               0
   6                                               0
   7                                               0
   8                                               2
   9                                               0
   10                                              0
   11                                              0
   12                                              0
   13                                              0
   14                                              7
   15                                              0
   16                                              0
   17                                              0
   18                                              0
   19                                              0
   20                                              7
        z 
   "days" 
   Error in dimnames(x) <- dn : 
     length of 'dimnames' [2] not equal to array extent

without errors locally:

[1] "line 1119 -- Here are the levels: `levels(tvcv[,interaction[2]])[-1]`"
[1] "b" "c" "d" "e"
[1] " line 1121 -- here are the names:"
[1] "z.vb" "z.vc" "z.vd" "z.ve"
   tvcv[, interaction[1]]:tvcv[, interaction[2]]b
1                                               0
2                                               0
3                                               0
4                                               0
5                                               3
6                                               4
7                                               0
8                                               0
9                                               0
10                                              0
11                                              0
12                                              3
13                                              0
14                                              0
15                                              0
16                                              0
17                                              0
18                                              4
19                                              0
20                                              0
   tvcv[, interaction[1]]:tvcv[, interaction[2]]c
1                                               0
2                                               0
3                                               0
4                                               9
5                                               0
6                                               0
7                                               0
8                                               0
9                                               0
10                                              3
11                                             11
12                                              0
13                                              0
14                                              0
15                                              0
16                                              0
17                                              6
18                                              0
19                                              0
20                                              0
   tvcv[, interaction[1]]:tvcv[, interaction[2]]d
1                                               0
2                                               0
3                                               9
4                                               0
5                                               0
6                                               0
7                                               0
8                                               0
9                                               3
10                                              0
11                                              0
12                                              0
13                                              0
14                                              0
15                                              7
16                                              3
17                                              0
18                                              0
19                                              0
20                                              0
   tvcv[, interaction[1]]:tvcv[, interaction[2]]e
1                                               0
2                                               4
3                                               0
4                                               0
5                                               0
6                                               0
7                                               0
8                                               8
9                                               0
10                                              0
11                                              0
12                                              0
13                                              0
14                                              4
15                                              0
16                                              0
17                                              0
18                                              0
19                                              0
20                                              6
     z 
"days"

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

So Let's just focus on the levels, since the tmp dataset looks the same and the units look the same.

levels(tvcv[,interaction[2]])[-1]

Well, interaction[2] is referring to v in the example, which is

v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5))

I've run this with both

v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5), stringsAsFactors=TRUE)
v <- data.frame(matrix(rep(c("a","b","c","d","e"),4),ncol=5), stringsAsFactors=FALSE)

and it made no difference. So what is tvcv? Do some digging, and collapse some if statements like so (take note of line numbers):

Screen Shot 2020-04-24 at 10 51 38 PM

and we test each of those conditions and come upon tvc2 is TRUE for is.list(tvc2)&&!is.data.frame(tvc2) which is from lines 969-1020.

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

Line 970 inherits(tvcov,"tvcov") is true so tvcv is assigned tvc2$tvcov which is a data.frame. Let's put a print system right at line 970 for levels tvcov$tvcov

locally:

> tvc4 <- tvctomat(tvc2, interaction=c("z","v"))
[1] "line 972:  levels(tvcov$tvcov$v) = "
[1] "a" "b" "c" "d" "e"

against devel:

 tvc4 <- tvctomat(tvc2, interaction=c("z","v"))
   [1] "line 972:  levels(tvcov$tvcov$v) = "
   NULL

So this brings to question how was tvc2 in the example created -- right below the v data.frame statement:

tvc2 <- tvctomat(v, oldtvc=tvc, units=NA)

it's so weird that toggling the stringsAsFactors=TRUE didn't affect things. So somehow tvc2 must have levels in my local instance and not have levels in the against devel instance and I confirm this with a print statement just inside the function call:

local:

> tvc4 <- tvctomat(tvc2, interaction=c("z","v"))
[1] "line 915: levels(tvcov) are: "
[1] "a" "b" "c" "d" "e"
[1] "line 972:  levels(tvcov$tvcov$v) = "
[1] "a" "b" "c" "d" "e"
[1] "line 1026 -- `colnames(tvcv)` is"
[1] "z" "v"
[1] " BRUCE GET IN HERE 1102 "
[1] " BRUCE GET IN HERE 1117 "
[1] " BRUCE GET IN HERE 1118 "
[1] "line 1119 -- Here are the levels: `levels(tvcv[,interaction[2]])[-1]`"
[1] "b" "c" "d" "e"
[1] " line 1121 -- here are the names:"
[1] "z.vb" "z.vc" "z.vd" "z.ve"

against-devel:

> #print(tvc4 <- tvctomat(tvc2, interaction=c("z","v")))
   > tvc4 <- tvctomat(tvc2, interaction=c("z","v"))
   [1] "line 915: levels(tvcov) are: "
   NULL
   [1] "line 972:  levels(tvcov$tvcov$v) = "
   NULL
   [1] "line 1026 -- `colnames(tvcv)` is"
   [1] "z" "v"
   [1] " BRUCE GET IN HERE 1102 "
   [1] " BRUCE GET IN HERE 1117 "
   [1] " BRUCE GET IN HERE 1118 "
   [1] "line 1119 -- Here are the levels: `levels(tvcv[,interaction[2]])[-1]`"
   NULL
   [1] " line 1121 -- here are the names:"
   [1] "z.v"
   Error in dimnames(x) <- dn : 
     length of 'dimnames' [2] not equal to array extent
   Calls: tvctomat -> colnames<-
   Execution halted

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

tvc2 is created by the line tvc2 <- tvctomat(v, oldtvc=tvc, units=NA)

I finally got smart and put a str(tvc2) statement in the examples. Check it out!

locally:

> levels(tvc2$tvcov$v);str(tvc2)
[1] "a" "b" "c" "d" "e"
List of 3
 $ z:'data.frame':	20 obs. of  2 variables:
  ..$ z: int [1:20] 6 4 9 9 3 4 5 8 3 3 ...
  ..$ v: Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
 $ v: int [1:4] 5 5 5 5
 $ NA: Named chr [1:2] "days" NA
  ..- attr(*, "names")= chr [1:2] "z" "v"
 - attr(*, "class")= chr "tvcov"

against-devel:

 > levels(tvc2$tvcov$v); str(tvc2);
   NULL
   List of 3
    $ z:'data.frame':	20 obs. of  2 variables:
     ..$ z: int [1:20] 4 3 6 6 6 4 8 2 4 11 ...
     ..$ v: chr [1:20] "a" "e" "d" "c" ...
    $ v: int [1:4] 5 5 5 5
    $ NA: Named chr [1:2] "days" NA
     ..- attr(*, "names")= chr [1:2] "z" "v"
    - attr(*, "class")= chr "tvcov"

And there it is. Clearly. I should have done it first thing. I search for data.frame inside tvctomat and it only happens once and it happens when there is an oldtvc specified, as in the tvc2 case. Let's add a stringsAsFactor=TRUE arg and see what happens, shall we?

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

Nada! Denied. After many more hours of digging, I have found the culprit. Around line 939, we define tvcv. I put in a print statement to see how the str would change. Sure enough, both locally and against-devel start out with Factors (I also put a print statement for str(tvcov) as the very first line of the function to see how things start) but the way the following line is handled is different and thus the culprit:

		tvcv <- as.data.frame(as.vector(t(as.matrix(tvcov)))); 
		print("line 940 Bruce get in here `names` is "); print(names);
		print("line 941 Bruce get in here `str(tvcv)` is "); print(str(tvcv));

which has output locally:

[1] "line 940 Bruce get in here `names` is "
[1] "v"
[1] "line 941 Bruce get in here `str(tvcv)` is "
'data.frame':	20 obs. of  1 variable:
 $ as.vector(t(as.matrix(tvcov))): Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
NULL

and against-devel:

[1] "line 940 Bruce get in here `names` is "
[1] "v"
[1] "line 941 Bruce get in here `str(tvcv)` is "
   'data.frame':	20 obs. of  1 variable:
    $ as.vector(t(as.matrix(tvcov))): chr  "a" "e" "d" "c" ...
   NULL

So how do I ensure that tvcv <- as.data.frame(as.vector(t(as.matrix(tvcov)))); keeps its factors? (and these names make it hard to keep things straight. tvcv vs. tvcov.)

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

15 minutes later -- assigning a data.frame(obj, stringsAsFactors=TRUE) when the obj is already a data.frame that has attributes set doesn't appear to change those attributes. So, I can't just add another line after the defined obj and change it. But, what if I took the offending line:

tvcv <- as.data.frame(as.vector(t(as.matrix(tvcov))))

and forced it to be character and then made it a data.frame with the argument set to TRUE, as in:

tvcv <- data.frame(as.character(as.vector(t(as.matrix(tvcov)))),stringsAsFactors=TRUE); 

Local tests show that I get the same output except for the dimension name now being uglier with as.character in it:

[1] "line 941 Bruce get in here `str(tvcv)` is "
'data.frame':	20 obs. of  1 variable:
  $ as.vector(t(as.matrix(tvcov))): Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
NULL
[1] "line 941 Bruce get in here `str(tvcv)` is "
'data.frame':	20 obs. of  1 variable:
  $ as.character.as.vector.t.as.matrix.tvcov....: Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
NULL

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

Okay, the answer is:

  • add stringsAsFactors=TRUE to the v <- data.frame() call in the example
  • add stringsAsFactors=TRUE to the v <- data.frame() call around line 1160 (oldtvcov$tvcov <- data.frame(oldtvcov$tvcov,tvcv, stringsAsFactors = TRUE);
  • turn tvcv <- as.data.frame(as.vector(t(as.matrix(tvcov)))) into tvcv <- data.frame(as.character(as.vector(t(as.matrix(tvcov)))),stringsAsFactors=TRUE) around line 940.

Survey says! ---------------> NOT QUITE YET BUT PROGRE$$ has been made!

Look at the progress in the against-devel: tvc2 and tvc4 form correctly! But we messed up

   > tvc2 <- tvctomat(v, oldtvc=tvc, units=NA);levels(tvc2$tvcov$v); str(tvc2);
   [1] "first line str(tvcov):"
   'data.frame':	4 obs. of  5 variables:
    $ X1: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
    $ X2: Factor w/ 4 levels "a","b","c","e": 4 1 2 3
    $ X3: Factor w/ 4 levels "a","b","d","e": 3 4 1 2
    $ X4: Factor w/ 4 levels "a","c","d","e": 2 3 4 1
    $ X5: Factor w/ 4 levels "b","c","d","e": 1 2 3 4
   NULL
   [1] "line 931 Bruce get in here, because str(tvcov) is"
   'data.frame':	4 obs. of  5 variables:
    $ X1: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
    $ X2: Factor w/ 4 levels "a","b","c","e": 4 1 2 3
    $ X3: Factor w/ 4 levels "a","b","d","e": 3 4 1 2
    $ X4: Factor w/ 4 levels "a","c","d","e": 2 3 4 1
    $ X5: Factor w/ 4 levels "b","c","d","e": 1 2 3 4
   NULL
   [1] "line 940 Bruce get in here `names` is "
   [1] "v"
   [1] "line 941 Bruce get in here `str(tvcv)` is "
   'data.frame':	20 obs. of  1 variable:
    $ as.character.as.vector.t.as.matrix.tvcov....: Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
   NULL
   [1] "line 944 Bruce get in here"
   [1] "before dataframe call "
    int [1:20, 1] 4 3 6 6 6 4 8 2 4 11 ...
    - attr(*, "dimnames")=List of 2
     ..$ : NULL
     ..$ : chr "z"
   NULL
   'data.frame':	20 obs. of  1 variable:
    $ v: Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
   NULL
   [1] " after dataframe call "
   'data.frame':	20 obs. of  2 variables:
    $ z: int  4 3 6 6 6 4 8 2 4 11 ...
    $ v: Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
   NULL
   [1] "a" "b" "c" "d" "e"
   List of 3
    $ z:'data.frame':	20 obs. of  2 variables:
     ..$ z: int [1:20] 4 3 6 6 6 4 8 2 4 11 ...
     ..$ v: Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
    $ v: int [1:4] 5 5 5 5
    $ NA: Named chr [1:2] "days" NA
     ..- attr(*, "names")= chr [1:2] "z" "v"
    - attr(*, "class")= chr "tvcov"
   > #print(tvc4 <- tvctomat(tvc2, interaction=c("z","v")))
   > tvc4 <- tvctomat(tvc2, interaction=c("z","v"))
   [1] "line 915: levels(tvcov) are: "
   [1] "a" "b" "c" "d" "e"
   [1] "first line str(tvcov):"
   List of 3
    $ z:'data.frame':	20 obs. of  2 variables:
     ..$ z: int [1:20] 4 3 6 6 6 4 8 2 4 11 ...
     ..$ v: Factor w/ 5 levels "a","b","c","d",..: 1 5 4 3 2 2 1 5 4 3 ...
    $ v: int [1:4] 5 5 5 5
    $ NA: Named chr [1:2] "days" NA
     ..- attr(*, "names")= chr [1:2] "z" "v"
    - attr(*, "class")= chr "tvcov"
   NULL
   [1] "line 972:  levels(tvcov$tvcov$v) = "
   [1] "a" "b" "c" "d" "e"
   [1] "line 1026 -- `colnames(tvcv)` is"
   [1] "z" "v"
   [1] " BRUCE GET IN HERE 1102 "
   [1] " BRUCE GET IN HERE 1117 "
   [1] " BRUCE GET IN HERE 1118 "
   [1] "line 1119 -- Here are the levels: `levels(tvcv[,interaction[2]])[-1]`"
   [1] "b" "c" "d" "e"
   [1] " line 1121 -- here are the names:"
   [1] "z.vb" "z.vc" "z.vd" "z.ve"

but we messed up the following call tvctomat(tvc3, interaction="z", ccov=xx)

> tvctomat(tvc3, interaction="z", ccov=xx)
   [1] "first line str(tvcov):"
   List of 3
    $ z : num [1:20, 1:5] 4 3 6 6 6 4 8 2 4 11 ...
     ..- attr(*, "dimnames")=List of 2
     .. ..$ : chr [1:20] "1" "2" "3" "4" ...
     .. ..$ : chr [1:5] "z" "vb" "vc" "vd" ...
    $ vb: int [1:4] 5 5 5 5
    $ vc: Named chr [1:5] "days" NA NA NA ...
     ..- attr(*, "names")= chr [1:5] "z" "vb" "vc" "vd" ...
    - attr(*, "class")= chr "tvcov"
   NULL
   [1] "line 972:  levels(tvcov$tvcov$v) = "
   Error: $ operator is invalid for atomic vectors
   Execution halted

Luckily that print statement from the first line was installed and worked.

BUT WAIT! -- Looks like the damage was self inflicted from a print statement. To confirm, we comment out the following and rerun and we achieve against-devel success!

print("line 972: levels(tvcov$tvcov$v) = "); print(levels(tvcov$tvcov$v));

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

Ok, so now do the git checkout thing on some files and just implement the following:

  • add stringsAsFactors=TRUE to the v <- data.frame() call in the example
  • add stringsAsFactors=TRUE to the data.frame() call around line 1160 (oldtvcov$tvcov <- data.frame(oldtvcov$tvcov,tvcv, stringsAsFactors = TRUE);
  • turn tvcv <- as.data.frame(as.vector(t(as.matrix(tvcov)))) into tvcv <- data.frame(as.character(as.vector(t(as.matrix(tvcov)))),stringsAsFactors=TRUE) around line 940.

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

Updated NEWS.md

  • In response to K. Hornik email regarding the stringsAsFactors=FALSE default in upcoming R versions, I did the following 3 edits:
    • add stringsAsFactors=TRUE to the v <- data.frame() call in the example of tvctomat.Rd
    • add stringsAsFactors=TRUE to the data.frame() call in the function tvctomat() (around line 1160 in objectrm.r): (oldtvcov$tvcov <- data.frame(oldtvcov$tvcov,tvcv, stringsAsFactors = TRUE)
    • turn tvcv <- as.data.frame(as.vector(t(as.matrix(tvcov)))) into tvcv <- data.frame(as.character(as.vector(t(as.matrix(tvcov)))),stringsAsFactors=TRUE) in the function tvctomat() (around line 937 of objectrm.r)

@swihart
Copy link
Owner Author

swihart commented Apr 25, 2020

Dear maintainer,

thanks, package rmutil_1.1.4.tar.gz is on its way to CRAN.

Best regards,
CRAN teams' auto-check service
Package check result: OK

No changes to worse in reverse depends.

@swihart swihart closed this as completed Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant