Skip to content

Commit

Permalink
updating output to account for different dimensions and documenting p…
Browse files Browse the repository at this point in the history
…arams
  • Loading branch information
robbriers committed Apr 18, 2019
1 parent e790034 commit f2e77dd
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions R/cde-class.r
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
#' Class definition for cde_df
#' @description Output from a call to the EA website is returned as an
#' object of class '`cde_df`, which is basically a dataframe with a
#' modified `print` method to format the output ro fit the console width
#
#' @param x A dataframe to be converted to class `cde_df`
#'
#'@noRd

# define cde_df class (modified df)
as.cde <- function(x) {
class(x) <- c("cde_df", setdiff(class(x), "cde_df"))
x
}

# custom print method
#' Print method definition for cde_df
#' @description Custom `print` method for objects of class '`cde_df`.
#' Formats output to fit current width of console.
#'
#' @param x An object of class `cde_df`
#'
#'@noRd
print.cde_df <- function(x){
# find number of columns that will fit on current width
# if the maximum length of all column names is greater than the width
# sebset the columns
# subset the columns
if(max(cumsum(nchar(names(x))+2)>getOption("width"))){
cols<-min(which(cumsum(nchar(names(x))+2) > getOption("width")))-1
}else{cols<-ncol(x)}
# subset df for just these columns

# subset cde_df for just these columns
data_to_print <- x[,1:cols]

# get column name lengths for use in truncation
Expand All @@ -23,24 +37,37 @@ print.cde_df <- function(x){
if (nrow(data_to_print)>10){
data_to_print <- data_to_print[1:10,]
}

# truncate strings within rows to fit as well
if(!nrow(x)==0){
data_to_print <- as.data.frame(t(apply(data_to_print, 1, trunc_char, cols, col_name_lengths)))
data_to_print <- as.data.frame(t(apply(data_to_print, 1, trunc_char, col_name_lengths)))
print(data_to_print, row.names=FALSE)
}else{cat("No data returned - printing not possible")}
# output data that fits

# if more than 10 rows, indicate missing data
if(nrow(x)>10){
# #### need to handle if 11 or just one more column
cat(paste0("With an additional ", nrow(x)-10, " rows and ", ncol(x)-ncol(data_to_print), " columns of data."),"\n")
cat("Row values may be truncated to fit console.")
# if there are more columns that visible on the screen
if (ncol(x)>ncol(data_to_print)){
cat(paste0("With an additional ", nrow(x)-10, " rows and ", ncol(x)-ncol(data_to_print), " columns of data."),"\n")
}else{
cat(paste0("With an additional ", nrow(x)-10, " rows of data."),"\n")
}
}
# end of function
cat("Row values may be truncated to fit console.")
# end of function
}

# string truncation function
trunc_char <- function(x, cols, col_name_lengths){
#' Truncate strings within `cde_df` objects to fit console
#' @description Truncates the length of strings within rows of `cde_df`
#' objects to the same length as the column name, ensuring that they
#' fit current width of console.
#'
#' @param x An object of class `cde_df`
#'
#' @param col_name_lengths Vector containing lengths of column names
#'
#'@noRd
trunc_char <- function(x, col_name_lengths){
if (is.character(x)==TRUE){
substr(x,1,col_name_lengths)
}
Expand Down

0 comments on commit f2e77dd

Please sign in to comment.