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

Convert readxl from Rcpp to cpp11 #659

Merged
merged 32 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
40d1300
init convert to cpp11
Jul 28, 2021
2a20277
check readxl fx in zip
Jul 29, 2021
0faa29a
add Jims changes
Jul 29, 2021
aa0523a
Merge branch 'master' into use_cpp11
sbearrows Jul 29, 2021
ca4ca54
Merge commit 'd7b71b0a0c63d2450d26f35e986748526a523feb'
Jul 29, 2021
ec7c62c
convert StringSet.h and define macros for cpp11
Jul 30, 2021
7480b9a
Merge commit 'aa0523ad54e80b3cea4c6426883dee27588b1bda'
Jul 30, 2021
1278c16
converted utils,h
Jul 30, 2021
0e48fb4
Remove Rcpp exports
jimhester Jul 30, 2021
8299cd7
Reemove RcppExports R file as well
jimhester Jul 30, 2021
4e6aab5
Include cpp11.cpp
jimhester Jul 30, 2021
8574e0c
convert CellLimits and friends and update tests
Aug 2, 2021
3f7553f
Jims edits
Aug 2, 2021
0470661
fixed crash
Aug 3, 2021
79fc036
remove changes to limits
Aug 3, 2021
5f07e14
fix strlen error
Aug 3, 2021
988bb94
Use R_xlen_t literal syntax
jimhester Aug 3, 2021
51a5957
converting more files
Aug 4, 2021
33d6136
convert WorkBook and friends
Aug 4, 2021
d6bf9a3
use r_string for sexp
Aug 4, 2021
a50c859
Include visibility in the exported init function
jimhester Aug 4, 2021
7c8ad23
fix current errors with Jim
Aug 4, 2021
8bc4c5c
converted ColSpec
Aug 4, 2021
deecf96
Merge commit 'a50c859972ccb2725534d9c89c6ae66cd4cb1d78'
Aug 4, 2021
9215609
reorg header files and change limits to ints
Aug 5, 2021
24a8970
Fix crashes
jimhester Aug 5, 2021
51e82a9
remove all refs to Rcpp
Aug 5, 2021
db8fc06
remove excess writable vars
Aug 5, 2021
1f20ad9
Use a dev branch of cpp11
jennybc Aug 6, 2021
2842599
clean up and reorg headers
Aug 6, 2021
33c79a5
Tweaks to get filenames to work in read_xls_
jimhester Aug 6, 2021
5aaf592
Regenerate registrations
jimhester Aug 6, 2021
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
9 changes: 6 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ URL: https://readxl.tidyverse.org,
BugReports: https://github.com/tidyverse/readxl/issues
Imports:
cellranger,
Rcpp (>= 0.12.18),
tibble (>= 1.3.1),
utils
Suggests:
Expand All @@ -55,8 +54,8 @@ Suggests:
rprojroot (>= 1.1),
testthat (>= 2.0.0)
LinkingTo:
progress,
Rcpp
cpp11,
progress
VignetteBuilder:
knitr
Config/testthat/edition: 3
Expand All @@ -65,3 +64,7 @@ Encoding: UTF-8
Note: libxls v1.6.2 4482400
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1.9001
SystemRequirements: C++11
Remotes:
r-lib/cpp11#218

1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export(read_xls)
export(read_xlsx)
export(readxl_example)
export(readxl_progress)
importFrom(Rcpp,sourceCpp)
importFrom(cellranger,anchored)
importFrom(cellranger,cell_cols)
importFrom(cellranger,cell_limits)
Expand Down
39 changes: 0 additions & 39 deletions R/RcppExports.R

This file was deleted.

37 changes: 37 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by cpp11: do not edit by hand

xls_sheets <- function(path) {
.Call(`_readxl_xls_sheets`, path)
}

xls_date_formats <- function(path) {
.Call(`_readxl_xls_date_formats`, path)
}

read_xls_ <- function(path, sheet_i, limits, shim, col_names, col_types, na, trim_ws, guess_max, progress) {
.Call(`_readxl_read_xls_`, path, sheet_i, limits, shim, col_names, col_types, na, trim_ws, guess_max, progress)
}

xlsx_sheets <- function(path) {
.Call(`_readxl_xlsx_sheets`, path)
}

xlsx_strings <- function(path) {
.Call(`_readxl_xlsx_strings`, path)
}

xlsx_date_formats <- function(path) {
.Call(`_readxl_xlsx_date_formats`, path)
}

parse_ref <- function(ref) {
.Call(`_readxl_parse_ref`, ref)
}

read_xlsx_ <- function(path, sheet_i, limits, shim, col_names, col_types, na, trim_ws, guess_max, progress) {
.Call(`_readxl_read_xlsx_`, path, sheet_i, limits, shim, col_names, col_types, na, trim_ws, guess_max, progress)
}

zip_xml <- function(zip_path, file_path) {
invisible(.Call(`_readxl_zip_xml`, zip_path, file_path))
}
15 changes: 9 additions & 6 deletions R/read_excel.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#' @useDynLib readxl, .registration = TRUE
#' @importFrom Rcpp sourceCpp
NULL

#' Read xls and xlsx files
Expand Down Expand Up @@ -274,14 +273,18 @@ standardise_limits <- function(range, skip, n_max, has_col_names) {
} else {
limits <- cellranger::as.cell_limits(range)
limits <- c(
min_row = limits[["ul"]][1] - 1,
max_row = limits[["lr"]][1] - 1,
min_col = limits[["ul"]][2] - 1,
max_col = limits[["lr"]][2] - 1
)
min_row = (limits[["ul"]][1] - 1),
max_row = (limits[["lr"]][1] - 1),
min_col = (limits[["ul"]][2] - 1),
max_col = (limits[["lr"]][2] - 1)
)
jennybc marked this conversation as resolved.
Show resolved Hide resolved
}
limits[is.na(limits)] <- -1
names <- names(limits)
limits <- as.integer(limits)
names(limits) <- names
limits

}

check_col_types <- function(col_types) {
Expand Down
1 change: 0 additions & 1 deletion revdep/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
|R.oo |1.22.0 |1.22.0 | |
|R.utils |2.8.0 |2.8.0 | |
|R6 |2.3.0 |2.3.0 | |
|Rcpp |1.0.0 |1.0.0 | |
|rematch |1.0.1 |1.0.1 | |
|rlang |0.3.1 |0.3.1 | |
|roloc |0.1-1 |0.1-1 | |
Expand Down
11 changes: 4 additions & 7 deletions src/CellLimits.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef READXL_CELLLIMITS_
#define READXL_CELLLIMITS_

#include <Rcpp.h>
#include "XlsCell.h"
#include <cpp11/integers.hpp>

class CellLimits {
int minRow_, maxRow_, minCol_, maxCol_;
Expand All @@ -14,7 +14,7 @@ class CellLimits {
minCol_ = -1;
maxCol_ = -1;
}
CellLimits(Rcpp::IntegerVector limits) {
CellLimits(cpp11::integers limits) {
minRow_ = limits[0];
maxRow_ = limits[1];
minCol_ = limits[2];
Expand Down Expand Up @@ -74,12 +74,9 @@ class CellLimits {
}

void print() {
Rcpp::Rcout << "row min, max: " << minRow_ << ", "
<< maxRow_ << "\t"
<< "col min, max: " << minCol_<< ", "
<< maxCol_ << std::endl;
Rprintf("row min, max: %d, %d \t col min, max: %d, %d",
minRow_, maxRow_, minCol_, maxCol_);
}

private:

bool contains(int min, int max, int val) const {
Expand Down
49 changes: 28 additions & 21 deletions src/ColSpec.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#ifndef READXL_COLSPEC_
#define READXL_COLSPEC_

#include <Rcpp.h>
#include "libxls/xls.h"
#include "utils.h"
#include "StringSet.h"
#include "libxls/xls.h"

#include <cpp11/protect.hpp>
#include <cpp11/strings.hpp>
#include <cpp11/doubles.hpp>
#include <cpp11/list.hpp>
#include <cpp11/logicals.hpp>
#include <cpp11/sexp.hpp>

enum CellType {
CELL_UNKNOWN,
Expand Down Expand Up @@ -57,15 +64,15 @@ inline std::string colTypeDesc(ColType type) {
return "???";
}

inline Rcpp::CharacterVector colTypeDescs(std::vector<ColType> types) {
Rcpp::CharacterVector out(types.size());
inline cpp11::strings colTypeDescs(std::vector<ColType> types) {
cpp11::writable::strings out(types.size());
for (size_t i = 0; i < types.size(); ++i) {
out[i] = colTypeDesc(types[i]);
}
return out;
}

inline std::vector<ColType> colTypeStrings(Rcpp::CharacterVector x) {
inline std::vector<ColType> colTypeStrings(cpp11::strings x) {
std::vector<ColType> types;
types.reserve(x.size());

Expand All @@ -88,7 +95,7 @@ inline std::vector<ColType> colTypeStrings(Rcpp::CharacterVector x) {
} else if (type == "skip") {
types.push_back(COL_SKIP);
} else {
Rcpp::stop("Unknown column type '%s' at position %i", type, i + 1);
cpp11::stop("Unknown column type '%s' at position %i", type.c_str(), (i + 1));
}
}

Expand Down Expand Up @@ -194,7 +201,7 @@ inline std::vector<ColType> finalizeTypes(std::vector<ColType> types) {
return types;
}

inline Rcpp::CharacterVector reconcileNames(Rcpp::CharacterVector names,
inline cpp11::strings reconcileNames(cpp11::strings names,
const std::vector<ColType>& types,
int sheet_i) {
size_t ncol_names = names.size();
Expand All @@ -211,11 +218,11 @@ inline Rcpp::CharacterVector reconcileNames(Rcpp::CharacterVector names,
}
}
if (ncol_names != ncol_noskip) {
Rcpp::stop("Sheet %d has %d columns (%d unskipped), but `col_names` has length %d.",
sheet_i + 1, ncol_types, ncol_noskip, ncol_names);
cpp11::stop("Sheet %d has %d columns (%d unskipped), but `col_names` has length %d.",
(sheet_i + 1), ncol_types, ncol_noskip, ncol_names);
}

Rcpp::CharacterVector newNames(ncol_types, "");
cpp11::writable::strings newNames(ncol_types);
size_t j_short = 0;
for (size_t j_long = 0; j_long < ncol_types; ++j_long) {
if (types[j_long] == COL_SKIP) {
Expand All @@ -227,33 +234,33 @@ inline Rcpp::CharacterVector reconcileNames(Rcpp::CharacterVector names,
return newNames;
}

inline Rcpp::RObject makeCol(ColType type, int n) {
inline cpp11::sexp makeCol(ColType type, int n) {
switch(type) {
case COL_UNKNOWN:
case COL_BLANK:
case COL_SKIP:
return R_NilValue;
case COL_LOGICAL:
return Rcpp::LogicalVector(n, NA_LOGICAL);
return new_vector<cpp11::writable::logicals>(n, NA_LOGICAL);
case COL_DATE: {
Rcpp::RObject col = Rcpp::NumericVector(n, NA_REAL);
col.attr("class") = Rcpp::CharacterVector::create("POSIXct", "POSIXt");
cpp11::sexp col = new_vector<cpp11::writable::doubles>(n, NA_REAL);
col.attr("class") = {"POSIXct", "POSIXt"};
col.attr("tzone") = "UTC";
return col;
}
case COL_NUMERIC:
return Rcpp::NumericVector(n, NA_REAL);
return new_vector<cpp11::writable::doubles>(n, NA_REAL);
case COL_TEXT:
return Rcpp::CharacterVector(n, NA_STRING);
return new_vector<cpp11::writable::strings>(n, NA_STRING);
case COL_LIST:
return Rcpp::List(n, Rcpp::LogicalVector(1, NA_LOGICAL));
return new_vector<cpp11::writable::list>(n, new_vector<cpp11::writable::logicals>(1, NA_LOGICAL));
}

return R_NilValue;
}

inline Rcpp::List removeSkippedColumns(Rcpp::List cols,
Rcpp::CharacterVector names,
inline cpp11::list removeSkippedColumns(cpp11::list cols,
cpp11::strings names,
std::vector<ColType> types) {
int p = cols.size();

Expand All @@ -263,8 +270,8 @@ inline Rcpp::List removeSkippedColumns(Rcpp::List cols,
p_out++;
}

Rcpp::List out(p_out);
Rcpp::CharacterVector names_out(p_out);
cpp11::writable::list out(p_out);
cpp11::writable::strings names_out(p_out);
int j_out = 0;
for (int j = 0; j < p; ++j) {
if (types[j] == COL_SKIP) {
Expand Down
2 changes: 1 addition & 1 deletion src/Makevars
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PKG_CPPFLAGS = -Iunix -I. -Irapidxml -DRCPP_DEFAULT_INCLUDE_CALL=false
PKG_CFLAGS = $(C_VISIBILITY)
PKG_CXXFLAGS = $(CXX_VISIBILITY)

SOURCES = RcppExports.cpp XlsWorkBook.cpp XlsWorkSheet.cpp XlsxWorkBook.cpp XlsxWorkSheet.cpp zip.cpp
SOURCES = cpp11.cpp XlsWorkBook.cpp XlsWorkSheet.cpp XlsxWorkBook.cpp XlsxWorkSheet.cpp zip.cpp
OBJECTS = $(SOURCES:.cpp=.o) cran.o libxls/xlstool.o libxls/endian.o libxls/ole.o libxls/xls.o libxls/locale.o

all: $(SHLIB)
Expand Down
2 changes: 1 addition & 1 deletion src/Makevars.win
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PKG_CXXFLAGS = $(CXX_VISIBILITY)
PKG_CPPFLAGS=-Iwindows -I. -Irapidxml -D__USE_MINGW_ANSI_STDIO -DRCPP_DEFAULT_INCLUDE_CALL=false
PKG_LIBS=-lRiconv

SOURCES = RcppExports.cpp XlsWorkBook.cpp XlsWorkSheet.cpp XlsxWorkBook.cpp XlsxWorkSheet.cpp zip.cpp
SOURCES = cpp11.cpp XlsWorkBook.cpp XlsWorkSheet.cpp XlsxWorkBook.cpp XlsxWorkSheet.cpp zip.cpp
OBJECTS = $(SOURCES:.cpp=.o) cran.o libxls/xlstool.o libxls/endian.o libxls/ole.o libxls/xls.o libxls/locale.o

all: $(SHLIB)
Expand Down
Loading