Skip to content

Commit

Permalink
feat: get dimensions of a csv data file without loading it
Browse files Browse the repository at this point in the history
  • Loading branch information
tin900 committed Oct 3, 2023
1 parent 8b35cd4 commit 8b91df8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export("%=%")
export(.repath)
export(close_view)
export(get_file_info)
export(get_git_branch)
export(sa_branch_get)
export(sa_branch_set)
Expand Down
29 changes: 29 additions & 0 deletions R/get_file_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' Get Column Names and Row Count
#'
#' This function returns the number of columns and the number of rows in a given file.
#' It uses the `system` function to execute shell commands.
#'
#' @param filepath The path to the file.
#' @param verbose If `TRUE`, message the number of columns and rows.
#' @return A list with the number of columns and rows.
#' @export
get_file_info <- function(filepath, verbose = FALSE) {
# Get the first line of the file and split it into column names
column_names <- system(paste0('head -n 1 "', filepath, '"'), intern = TRUE)
num_cols <- length(unlist(strsplit(column_names, split = ';')))

# Get the number of lines in the file
row_number <- system(if (.Platform$OS.type == "windows") {
paste0('find /c /v "" "', filepath, '"')
} else {
paste0('wc -l "', filepath, '"')
}, intern = TRUE)
num_rows <- as.integer(sub(".*: ", "", row_number[2])) - 1

if (verbose) {
message(paste("number of columns:", num_cols))
message(paste("number of rows:", num_rows))
}

return(list(num_cols = num_cols, num_rows = num_rows))
}
20 changes: 20 additions & 0 deletions man/get_file_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b91df8

Please sign in to comment.