Skip to content
Closed
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
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ LazyData: true
URL: https://github.com/r-lib/rematch2#readme
BugReports: https://github.com/r-lib/rematch2/issues
RoxygenNote: 7.1.0
Imports:
tibble
Suggests:
covr,
testthat
testthat,
tibble
Encoding: UTF-8
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ export(re_exec)
export(re_exec_all)
export(re_match)
export(re_match_all)
importFrom(tibble,new_tibble)
importFrom(tibble,tibble)
5 changes: 3 additions & 2 deletions R/all.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ re_match_all <- function(text, pattern, perl=TRUE, ...) {
res,
names = c(attr(match[[1]], "capture.names"), ".match"),
row.names = seq_along(text),
class = c("tbl_df", "tbl", "data.frame")
class = "data.frame"
)

res$.text <- text
nc <- ncol(res)
res[, c(seq_len(nc - 2), nc, nc - 1)]
res <- res[, c(seq_len(nc - 2), nc, nc - 1)]
as_tibble(res)
}

match1 <- function(text1, match1) {
Expand Down
10 changes: 7 additions & 3 deletions R/bind_re_match.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#'
#' Taking a data frame and a column name as input, this function will run
#' \code{\link{re_match}} and bind the results as new columns to the original
#' table., returning a \code{\link[tibble]{tibble}}. This makes it friendly for
#' table., returning a \code{\link[tibble]{tibble}} if the tibble package is
#' installed, a \code{data.frame} otherwise. This makes it friendly for
#' pipe-oriented programming with \link[magrittr]{magrittr}.
#'
#' @note If named capture groups will result in multiple columns with the same
Expand All @@ -23,7 +24,8 @@
#' suitable for programming.
#'
#' @examples
#' match_cars <- tibble::rownames_to_column(mtcars)
#' match_cars <- mtcars
#' match_cars$rowname <- rownames(mtcars)
#' bind_re_match(match_cars, rowname, "^(?<make>\\w+) ?(?<model>.+)?$")
#'
#' @export
Expand All @@ -36,7 +38,9 @@ bind_re_match <- function(df, from, ..., keep_match = FALSE) {
bind_re_match_ <- function(df, from, ..., keep_match = FALSE) {

stopifnot(is.data.frame(df))
if (!tibble::has_name(df, from))
stopifnot(is_installed("tibble"))

if (!from %in% names(df))
stop(from, " is not present in the data frame.")

res <- re_match(text = df[[from]], ...)
Expand Down
10 changes: 6 additions & 4 deletions R/exec.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ re_exec <- function(text, pattern, perl=TRUE, ...) {
})
)

res <- new_tibble(
res <- structure(
list(text, matchlist),
names = c(".text", ".match"),
nrow = length(text)
row.names = seq_along(text),
class = "data.frame"
)

if (!is.null(attr(match, "capture.start"))) {
Expand Down Expand Up @@ -113,10 +114,11 @@ re_exec <- function(text, pattern, perl=TRUE, ...) {
}
)

res <- new_tibble(
res <- structure(
c(grouplists, res),
names = c(attr(match, "capture.names"), ".text", ".match"),
nrow = length(res[[1]])
row.names = seq_along(text),
class = "data.frame"
)
}

Expand Down
5 changes: 2 additions & 3 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#' groups from the match of a regular expression to a character vector.
#' See \code{\link{re_match}}.
#'
#' @importFrom tibble tibble new_tibble
"_PACKAGE"

#' Extract Regular Expression Matches Into a Data Frame
Expand Down Expand Up @@ -78,6 +77,6 @@ re_match <- function(text, pattern, perl = TRUE, ...) {
}

names(res) <- c(attr(match, "capture.names"), ".text", ".match")
class(res) <- c("tbl_df", "tbl", class(res))
res

as_tibble(res)
}
11 changes: 11 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
as_tibble <- function(x) {
if (is_installed("tibble")) {
tibble::new_tibble(x, nrow = NROW(x))
} else {
x
}
}

is_installed <- function(pkg) {
isTRUE(requireNamespace(pkg, quietly = TRUE))
}
6 changes: 4 additions & 2 deletions man/bind_re_match.Rd

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