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

Feat sep.names #480

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ VignetteBuilder: knitr
Suggests:
knitr,
testthat
RoxygenNote: 6.0.1.9000
RoxygenNote: 6.1.1
Collate:
'CommentClass.R'
'HyperlinkClass.R'
Expand Down
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
openxlsx 4.1.1
----------------------------------------------------------------

NEW FEATURES

* sep.names allows choose other separator than '.' for variable names with a blank inside

openxlsx 4.1.0
----------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ getCellInfo <- function(xmlFile, sharedStrings, skipEmptyRows, startRow, rows, g
.Call(`_openxlsx_getCellInfo`, xmlFile, sharedStrings, skipEmptyRows, startRow, rows, getDates)
}

read_workbook <- function(cols_in, rows_in, v, string_inds, is_date, hasColNames, skipEmptyRows, skipEmptyCols, nRows, clean_names) {
.Call(`_openxlsx_read_workbook`, cols_in, rows_in, v, string_inds, is_date, hasColNames, skipEmptyRows, skipEmptyCols, nRows, clean_names)
read_workbook <- function(cols_in, rows_in, v, string_inds, is_date, hasColNames, hasSepNames, skipEmptyRows, skipEmptyCols, nRows, clean_names) {
.Call(`_openxlsx_read_workbook`, cols_in, rows_in, v, string_inds, is_date, hasColNames, hasSepNames, skipEmptyRows, skipEmptyCols, nRows, clean_names)
}

calc_number_rows <- function(x, skipEmptyRows) {
Expand Down
4 changes: 2 additions & 2 deletions R/helperFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,9 @@ getSharedStringsFromFile <- function(sharedStringsFile, isFile){
}


clean_names <- function(x){
clean_names <- function(x,schar){
x <- gsub("^[[:space:]]+|[[:space:]]+$", "", x)
x <- gsub("[[:space:]]+", ".", x)
x <- gsub("[[:space:]]+", schar, x)
return(x)
}

Expand Down
9 changes: 9 additions & 0 deletions R/readWorkbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#' If NULL, all rows are read.
#' @param check.names logical. If TRUE then the names of the variables in the data frame
#' are checked to ensure that they are syntactically valid variable names
#' @param sep.names One character which substitutes blanks in column names. By default, "."
#' @param namedRegion A named region in the Workbook. If not NULL startRow, rows and cols parameters are ignored.
#' @param na.strings A character vector of strings which are to be interpreted as NA. Blank cells will be returned as NA.
#' @param fillMergedCells If TRUE, the value in a merged cell is given to all cells within the merge.
Expand Down Expand Up @@ -72,6 +73,7 @@ read.xlsx <- function(xlsxFile,
rows = NULL,
cols = NULL,
check.names = FALSE,
sep.names = ".",
namedRegion = NULL,
na.strings = "NA",
fillMergedCells = FALSE){
Expand All @@ -92,6 +94,7 @@ read.xlsx.default <- function(xlsxFile,
rows = NULL,
cols = NULL,
check.names = FALSE,
sep.names = ".",
namedRegion = NULL,
na.strings = "NA",
fillMergedCells = FALSE){
Expand Down Expand Up @@ -121,6 +124,9 @@ read.xlsx.default <- function(xlsxFile,
if(!is.logical(check.names))
stop("check.names must be TRUE/FALSE.")

if(!is.character(sep.names) | nchar(sep.names)!=1)
stop("sep.names must be a character and only one.")

if(length(sheet) > 1)
stop("sheet must be of length 1.")

Expand Down Expand Up @@ -440,6 +446,7 @@ read.xlsx.default <- function(xlsxFile,
string_inds = string_refs,
is_date = isDate,
hasColNames = colNames,
hasSepNames = sep.names,
skipEmptyRows = skipEmptyRows,
skipEmptyCols = skipEmptyCols,
nRows = nRows,
Expand Down Expand Up @@ -494,6 +501,7 @@ readWorkbook <- function(xlsxFile,
rows = NULL,
cols = NULL,
check.names = FALSE,
sep.names = ".",
namedRegion = NULL,
na.strings = "NA",
fillMergedCells = FALSE){
Expand All @@ -509,6 +517,7 @@ readWorkbook <- function(xlsxFile,
rows = rows,
cols = cols,
check.names = check.names,
sep.names = ".",
namedRegion = namedRegion,
na.strings = na.strings,
fillMergedCells = fillMergedCells)
Expand Down
5 changes: 5 additions & 0 deletions R/workbook_read_workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ read.xlsx.Workbook <- function(xlsxFile,
rows = NULL,
cols = NULL,
check.names = FALSE,
sep.names = ".",
namedRegion = NULL,
na.strings = "NA",
fillMergedCells = FALSE){
Expand All @@ -35,6 +36,9 @@ read.xlsx.Workbook <- function(xlsxFile,
if(!is.logical(check.names))
stop("check.names must be TRUE/FALSE.")

if(!is.character(sep.names) | nchar(sep.names)!=1)
stop("sep.names must be a character and only one.")

if(length(sheet) != 1)
stop("sheet must be of length 1.")

Expand Down Expand Up @@ -326,6 +330,7 @@ read.xlsx.Workbook <- function(xlsxFile,
, string_inds = string_refs
, is_date = isDate
, hasColNames = colNames
, hasSepNames = sep.names
, skipEmptyRows = skipEmptyRows
, skipEmptyCols = skipEmptyCols
, nRows = nRows
Expand Down
4 changes: 3 additions & 1 deletion R/wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -2066,7 +2066,9 @@ pageSetup <- function(wb, sheet, orientation = NULL, scale = 100,
#' addWorksheet(wb, "S1")
#' writeDataTable(wb, 1, x = iris[1:30,])
#' # Formatting cells / columns is allowed , but inserting / deleting columns is protected:
#' protectWorksheet(wb, "S1", protect = TRUE, lockFormattingCells = FALSE, lockFormattingColumns = FALSE, lockInsertingColumns = TRUE, lockDeletingColumns = TRUE)
#' protectWorksheet(wb, "S1", protect = TRUE,
#' lockFormattingCells = FALSE, lockFormattingColumns = FALSE,
#' lockInsertingColumns = TRUE, lockDeletingColumns = TRUE)
#'
#' # Remove the protection
#' protectWorksheet(wb, "S1", protect = FALSE)
Expand Down
3 changes: 2 additions & 1 deletion man/addStyle.Rd

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

11 changes: 6 additions & 5 deletions man/addWorksheet.Rd

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

4 changes: 2 additions & 2 deletions man/createWorkbook.Rd

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

5 changes: 3 additions & 2 deletions man/insertPlot.Rd

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

3 changes: 2 additions & 1 deletion man/makeHyperlinkString.Rd

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

6 changes: 3 additions & 3 deletions man/pageSetup.Rd

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

4 changes: 3 additions & 1 deletion man/protectWorksheet.Rd

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

7 changes: 5 additions & 2 deletions man/read.xlsx.Rd

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

7 changes: 5 additions & 2 deletions man/readWorkbook.Rd

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

9 changes: 5 additions & 4 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ BEGIN_RCPP
END_RCPP
}
// read_workbook
SEXP read_workbook(IntegerVector cols_in, IntegerVector rows_in, CharacterVector v, IntegerVector string_inds, LogicalVector is_date, bool hasColNames, bool skipEmptyRows, bool skipEmptyCols, int nRows, Function clean_names);
RcppExport SEXP _openxlsx_read_workbook(SEXP cols_inSEXP, SEXP rows_inSEXP, SEXP vSEXP, SEXP string_indsSEXP, SEXP is_dateSEXP, SEXP hasColNamesSEXP, SEXP skipEmptyRowsSEXP, SEXP skipEmptyColsSEXP, SEXP nRowsSEXP, SEXP clean_namesSEXP) {
SEXP read_workbook(IntegerVector cols_in, IntegerVector rows_in, CharacterVector v, IntegerVector string_inds, LogicalVector is_date, bool hasColNames, char hasSepNames, bool skipEmptyRows, bool skipEmptyCols, int nRows, Function clean_names);
RcppExport SEXP _openxlsx_read_workbook(SEXP cols_inSEXP, SEXP rows_inSEXP, SEXP vSEXP, SEXP string_indsSEXP, SEXP is_dateSEXP, SEXP hasColNamesSEXP, SEXP hasSepNamesSEXP, SEXP skipEmptyRowsSEXP, SEXP skipEmptyColsSEXP, SEXP nRowsSEXP, SEXP clean_namesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Expand All @@ -263,11 +263,12 @@ BEGIN_RCPP
Rcpp::traits::input_parameter< IntegerVector >::type string_inds(string_indsSEXP);
Rcpp::traits::input_parameter< LogicalVector >::type is_date(is_dateSEXP);
Rcpp::traits::input_parameter< bool >::type hasColNames(hasColNamesSEXP);
Rcpp::traits::input_parameter< char >::type hasSepNames(hasSepNamesSEXP);
Rcpp::traits::input_parameter< bool >::type skipEmptyRows(skipEmptyRowsSEXP);
Rcpp::traits::input_parameter< bool >::type skipEmptyCols(skipEmptyColsSEXP);
Rcpp::traits::input_parameter< int >::type nRows(nRowsSEXP);
Rcpp::traits::input_parameter< Function >::type clean_names(clean_namesSEXP);
rcpp_result_gen = Rcpp::wrap(read_workbook(cols_in, rows_in, v, string_inds, is_date, hasColNames, skipEmptyRows, skipEmptyCols, nRows, clean_names));
rcpp_result_gen = Rcpp::wrap(read_workbook(cols_in, rows_in, v, string_inds, is_date, hasColNames, hasSepNames, skipEmptyRows, skipEmptyCols, nRows, clean_names));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -465,7 +466,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_openxlsx_int_2_cell_ref", (DL_FUNC) &_openxlsx_int_2_cell_ref, 1},
{"_openxlsx_get_shared_strings", (DL_FUNC) &_openxlsx_get_shared_strings, 2},
{"_openxlsx_getCellInfo", (DL_FUNC) &_openxlsx_getCellInfo, 6},
{"_openxlsx_read_workbook", (DL_FUNC) &_openxlsx_read_workbook, 10},
{"_openxlsx_read_workbook", (DL_FUNC) &_openxlsx_read_workbook, 11},
{"_openxlsx_calc_number_rows", (DL_FUNC) &_openxlsx_calc_number_rows, 2},
{"_openxlsx_map_cell_types_to_integer", (DL_FUNC) &_openxlsx_map_cell_types_to_integer, 1},
{"_openxlsx_map_cell_types_to_char", (DL_FUNC) &_openxlsx_map_cell_types_to_char, 1},
Expand Down
4 changes: 2 additions & 2 deletions src/openxlsx_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extern SEXP _openxlsx_map_cell_types_to_char(SEXP);
extern SEXP _openxlsx_map_cell_types_to_integer(SEXP);
extern SEXP _openxlsx_matrixRowInds(SEXP);
extern SEXP _openxlsx_read_file_newline(SEXP);
extern SEXP _openxlsx_read_workbook(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP _openxlsx_read_workbook(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP _openxlsx_write_worksheet_xml(SEXP, SEXP, SEXP, SEXP);
extern SEXP _openxlsx_write_worksheet_xml_2(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP _openxlsx_write_file(SEXP, SEXP, SEXP, SEXP);
Expand Down Expand Up @@ -76,7 +76,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_openxlsx_map_cell_types_to_integer", (DL_FUNC) &_openxlsx_map_cell_types_to_integer, 1},
{"_openxlsx_matrixRowInds", (DL_FUNC) &_openxlsx_matrixRowInds, 1},
{"_openxlsx_read_file_newline", (DL_FUNC) &_openxlsx_read_file_newline, 1},
{"_openxlsx_read_workbook", (DL_FUNC) &_openxlsx_read_workbook, 10},
{"_openxlsx_read_workbook", (DL_FUNC) &_openxlsx_read_workbook, 11},
{"_openxlsx_write_worksheet_xml", (DL_FUNC) &_openxlsx_write_worksheet_xml, 4},
{"_openxlsx_write_worksheet_xml_2", (DL_FUNC) &_openxlsx_write_worksheet_xml_2, 5},
{"_openxlsx_write_file", (DL_FUNC) &_openxlsx_write_file, 4},
Expand Down
3 changes: 2 additions & 1 deletion src/read_workbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ SEXP read_workbook(IntegerVector cols_in,
IntegerVector string_inds,
LogicalVector is_date,
bool hasColNames,
char hasSepNames,
bool skipEmptyRows,
bool skipEmptyCols,
int nRows,
Expand Down Expand Up @@ -520,7 +521,7 @@ SEXP read_workbook(IntegerVector cols_in,
}

// tidy up column names
col_names = clean_names(col_names);
col_names = clean_names(col_names,hasSepNames);

//--------------------------------------------------------------------------------
// Remove elements from rows, cols, v that have been used as headers
Expand Down