Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 12 additions & 50 deletions R/list_ods_sheets.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#' List all sheets in an ODS File
#' Get information in an (F)ODS File
#'
#' List all sheets in an ods file.
#' `list_(f)ods_sheets` lists all sheets in an (f)ods file.
#'
#' @param path Path to the ods file
#' @param path Path to the (f)ods file
#' @param include_external_data A boolean value to show or hide sheets containing archived linked data (default false)
#' @return A character vector of sheet names.
#' @return For `list_(f)ods_sheets`, a character vector of sheet names; for `get_num_sheets_in_(f)ods`, the total number of sheets
#' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
#' @examples
#' \dontrun{
#' # Get the list of names of sheets
#' list_ods_sheets("starwars.ods")
#' list_fods_sheets("starwars.fods")
#' # Get the number of sheets
#' get_num_sheets_in_ods("starwars.ods")
#' get_num_sheets_in_fods("starwars.fods")
#' }
#' @seealso
#' use \code{\link{read_ods}} to read the data
Expand All @@ -18,64 +22,22 @@ list_ods_sheets <- function(path, include_external_data = FALSE) {
return(get_sheet_names_(path, include_external_data))
}

#' Get the Number of Sheets in an ODS File
#'
#' Get the number of sheets in an ods file
#'
#' @param path path to the ods file
#' @param include_external_data A boolean value declaring if sheets holding archived linked data should be included
#' @return Number of sheets
#' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
#' @examples
#' \dontrun{
#' # Get the number of sheets
#' get_num_sheets_in_ods("starwars.ods")
#' }
#' @seealso
#' use \code{\link{read_ods}} to read the data
#' @rdname list_ods_sheets
#' @export
get_num_sheets_in_ods <- function(path, include_external_data = FALSE) {
sheets <- get_sheet_names_(path, include_external_data)
return(length(sheets))
}

#' List all sheets in an FODS File
#'
#' List all sheets in an fods file.
#'
#' @param path Path to the fods file
#' @param include_external_data A boolean value to show or hide sheets containing archived linked data (default false)
#' @return A character vector of sheet names.
#' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
#' @examples
#' \dontrun{
#' # Get the list of names of sheets
#' list_fods_sheets("starwars.fods")
#' }
#' @seealso
#' use \code{\link{read_fods}} to read the data
#' @rdname list_ods_sheets
#' @export
list_fods_sheets <- function(path, include_external_data = FALSE) {
return(get_flat_sheet_names_(path, include_external_data))
}

#' Get the Number of Sheets in an FODS File
#'
#' Get the number of sheets in an fods file
#'
#' @param path path to the fods file
#' @param include_external_data A boolean value declaring if sheets holding archived linked data should be included
#' @return Number of sheets
#' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
#' @examples
#' \dontrun{
#' # Get the number of sheets
#' get_num_sheets_in_fods("starwars.fods")
#' }
#' @seealso
#' use \code{\link{read_fods}} to read the data
#' @rdname list_ods_sheets
#' @export
get_num_sheets_in_fods <- function(path, include_external_data = FALSE) {
sheets <- get_flat_sheet_names_(path, include_external_data)
return(length(sheets))
}
}
58 changes: 0 additions & 58 deletions R/read_fods.R

This file was deleted.

173 changes: 102 additions & 71 deletions R/read_ods.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.change_df_with_col_row_header <- function(x, col_header, row_header){
if((nrow(x) < 2 && col_header )|| (ncol(x) < 2 && row_header)){
.change_df_with_col_row_header <- function(x, col_header, row_header) {
if((nrow(x) < 2 && col_header )|| (ncol(x) < 2 && row_header)) {
warning("Cannot make column/row names if this would cause the dataframe to be empty.", call. = FALSE)
return(x)
}
Expand All @@ -12,14 +12,11 @@
return(g)
}




## Based on readxl, although the implementation is different.
## If max row is -1, read to end of row.
## Row and column-numbers are 1-based
.standardise_limits <- function(range, skip) {
if(is.null(range)){
if(is.null(range)) {
skip <- check_nonnegative_integer(skip, "skip")
limits <- c(
min_row = skip + 1,
Expand All @@ -28,12 +25,12 @@
max_col = -1
)
} else {
if(skip != 0){
if(skip != 0) {
warning("Range and non-zero value for skip given. Defaulting to range.", call. = FALSE)
}
tryCatch({
limits <- cellranger::as.cell_limits(range)
}, error = function(e){
}, error = function(e) {
stop("Invalid `range`")
})
limits <- c(
Expand Down Expand Up @@ -101,69 +98,6 @@
}
}


#' Read Data From ODS File
#'
#' read_ods is a function to read a single sheet from an ods file and return a data frame.
#'
#' @param path path to the ods file.
#' @param sheet sheet to read. Either a string (the sheet name), or an integer sheet number. The default is 1.
#' @param col_names logical, indicating whether the file contains the names of the variables as its first line. Default is TRUE.
#' @param col_types Either NULL to guess from the spreadsheet or refer to [readr::type_convert()] to specify cols specification. NA will return a data frame with all columns being "characters".
#' @param na Character vector of strings to use for missing values. By default read_ods converts blank cells to missing data. It can also be set to
#' NULL, so that empty cells are treated as NA.
#' @param skip the number of lines of the data file to skip before beginning to read data. If this parameter is larger than the total number of lines in the ods file, an empty data frame is returned.
#' @param formula_as_formula logical, a switch to display formulas as formulas "SUM(A1:A3)" or as the resulting value "3"... or "8".. . Default is FALSE.
#' @param range selection of rectangle using Excel-like cell range, such as \code{range = "D12:F15"} or \code{range = "R1C12:R6C15"}. Cell range processing is handled by the \code{\link[=cellranger]{cellranger}} package.
#' @param row_names logical, indicating whether the file contains the names of the rows as its first column. Default is FALSE.
#' @param strings_as_factors logical, if character columns to be converted to factors. Default is FALSE.
#' @param check_names logical, passed down to base::data.frame(). Default is FALSE.
#' @param verbose logical, if messages should be displayed. Default is FALSE.
#' @return A data frame (\code{data.frame}) containing a representation of data in the ods file.
#' @note For flat ods files (.fods or .xml), use (\code{read_fods}).
#' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
#' @examples
#' \dontrun{
#' # Read a file
#' read_ods("starwars.ods")
#' # Read a specific sheet, e.g. the 2nd sheet
#' read_ods("starwars.ods", sheet = 2)
#' # Read a specific range, e.g. A1:C11
#' read_ods("starwars.ods", sheet = 2, range = "A1:C11")
#' }
#' @export
read_ods <- function(path,
sheet = 1,
col_names = TRUE,
col_types = NULL,
na = "",
skip = 0,
formula_as_formula = FALSE,
range = NULL,
row_names = FALSE,
strings_as_factors = FALSE,
check_names = FALSE,
verbose = FALSE

) {
## Should use match.call but there's a weird bug if one of the variable names is 'file'
.read_ods(path,
sheet,
col_names,
col_types,
na,
skip,
formula_as_formula,
range,
row_names,
strings_as_factors,
check_names,
verbose,
flat = FALSE)
}



.read_ods <- function(path,
sheet = 1,
col_names = TRUE,
Expand Down Expand Up @@ -271,3 +205,100 @@ read_ods <- function(path,
return(res)

}

#' Read Data From (F)ODS File
#'
#' read_ods is a function to read a single sheet from an (f)ods file and return a data frame. For flat ods files (.fods or .xml),
#' use (\code{read_fods}).
#'
#' @param path path to the (f)ods file.
#' @param sheet sheet to read. Either a string (the sheet name), or an integer sheet number. The default is 1.
#' @param col_names logical, indicating whether the file contains the names of the variables as its first line. Default is TRUE.
#' @param col_types Either NULL to guess from the spreadsheet or refer to [readr::type_convert()] to specify cols specification. NA will return a data frame with all columns being "characters".
#' @param na Character vector of strings to use for missing values. By default read_ods converts blank cells to missing data. It can also be set to
#' NULL, so that empty cells are treated as NA.
#' @param skip the number of lines of the data file to skip before beginning to read data. If this parameter is larger than the total number of lines in the ods file, an empty data frame is returned.
#' @param formula_as_formula logical, a switch to display formulas as formulas "SUM(A1:A3)" or as the resulting value "3"... or "8".. . Default is FALSE.
#' @param range selection of rectangle using Excel-like cell range, such as \code{range = "D12:F15"} or \code{range = "R1C12:R6C15"}. Cell range processing is handled by the \code{\link[=cellranger]{cellranger}} package.
#' @param row_names logical, indicating whether the file contains the names of the rows as its first column. Default is FALSE.
#' @param strings_as_factors logical, if character columns to be converted to factors. Default is FALSE.
#' @param check_names logical, passed down to base::data.frame(). Default is FALSE.
#' @param verbose logical, if messages should be displayed. Default is FALSE.
#' @return A data frame (\code{data.frame}) containing a representation of data in the (f)ods file.
#' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
#' @examples
#' \dontrun{
#' # Read an ODS file
#' read_ods("starwars.ods")
#' # Read a specific sheet, e.g. the 2nd sheet
#' read_ods("starwars.ods", sheet = 2)
#' # Read a specific range, e.g. A1:C11
#' read_ods("starwars.ods", sheet = 2, range = "A1:C11")
#' # Read an FODS file
#' read_fods("starwars.fods")
#' # Read a specific sheet, e.g. the 2nd sheet
#' read_fods("starwars.fods", sheet = 2)
#' # Read a specific range, e.g. A1:C11
#' read_fods("starwars.fods", sheet = 2, range = "A1:C11")
#' }
#' @export
read_ods <- function(path,
sheet = 1,
col_names = TRUE,
col_types = NULL,
na = "",
skip = 0,
formula_as_formula = FALSE,
range = NULL,
row_names = FALSE,
strings_as_factors = FALSE,
check_names = FALSE,
verbose = FALSE

) {
## Should use match.call but there's a weird bug if one of the variable names is 'file'
.read_ods(path,
sheet,
col_names,
col_types,
na,
skip,
formula_as_formula,
range,
row_names,
strings_as_factors,
check_names,
verbose,
flat = FALSE)
}

#' @rdname read_ods
#' @export
read_fods <- function(path,
sheet = 1,
col_names = TRUE,
col_types = NULL,
na = "",
skip = 0,
formula_as_formula = FALSE,
range = NULL,
row_names = FALSE,
strings_as_factors = FALSE,
check_names = FALSE,
verbose = FALSE
) {
## Should use match.call but there's a weird bug if one of the variable names is 'file'
.read_ods(path,
sheet,
col_names,
col_types,
na,
skip,
formula_as_formula,
range,
row_names,
strings_as_factors,
check_names,
verbose,
flat = TRUE)
}
31 changes: 0 additions & 31 deletions man/get_num_sheets_in_fods.Rd

This file was deleted.

Loading