R/as.data.table.ffdf.R

Defines functions as.ffdf.cohort as.ffdf.data.table as.data.table.cohort as.data.table.ffdf

Documented in as.data.table.cohort as.data.table.ffdf as.ffdf.cohort as.ffdf.data.table

as.data.table.ffdf <- function(x, keep.rownames = FALSE, ...){
	# Converts ffdf to data.table
	if (ncol(x) == 1){
		# Rewrite this function because it otherwise fails
		# if the table has only one column
		out <- data.table::data.table(temp := x[[1]][])
		data.table::setnames(out, origname)
		return(out)
	} else {
		as.data.table(as.data.frame(x), keep.rownames = keep.rownames, ...)
	}
}

as.data.table.cohort <- function(x, keep.rownames = FALSE, ...){
	# Converts cohort to data.table
	if (is.data.table(x)){
		x
	} else {
		out <- as.data.table(x, keep.rownames = keep.rownames, ...)
		setattr(out, 'idcolname', attr(x, 'idcolname'))
		setkeyv(out, attr(x, 'idcolname'))
		setattr(out, 'description', attr(x, 'description'))
		if (!is.cohort(out)){
			setattr(out, 'class', c('cohort', class(out)))
		}
		out
	}
}


as.ffdf.data.table <- function(x, vmode = NULL, col_args = list(), ...){
	# Converts data.table to ffdf
	# Converts characters to factors if necessary
	if (any(sapply(x, function(z) is.character(z)))){
		message('Converting character vectors to factor in the original data.table')
		toconvert <- colnames(x)[sapply(x, function(z) is.character(z))]
		for (i in toconvert){
			x[, eval(parse(text = paste('`', i,
				'` := factor(`', i, '`)', sep = '')))]
		}
	}
	as.ffdf(as.data.frame(x), vmode = vmode, col_args = col_args, ...)
}

as.ffdf.cohort <- function(x, vmode = NULL, col_args = list(), ...){
	# Converts cohort to data.table
	if (is.ffdf(x)){
		x
	} else {
		# Converts data.table cohort to ffdf
		# Converts characters to factors if necessary
		if (any(sapply(x, function(z) is.character(z)))){
			message('Converting character vectors to factor in the original data.table')
			toconvert <- colnames(x)[sapply(x, function(z) is.character(z))]
			for (i in toconvert){
				x[, eval(parse(text = paste('`', i,
					'` := factor(`', i, '`)', sep = '')))]
			}
		}
		out <- as.ffdf(as.data.frame(x), vmode = vmode, col_args = col_args, ...)
		setattr(out, 'idcolname', attr(x, 'idcolname'))
		setattr(out, 'description', attr(x, 'description'))
		if (!is.cohort(out)){
			setattr(out, 'class', c('cohort', class(out)))
		}
		out
	}
}

Try the CALIBERdatamanage package in your browser

Any scripts or data that you put into this service are public.

CALIBERdatamanage documentation built on Nov. 23, 2021, 3 p.m.