
Convert seekr_match vectors to and from data frames
Source: R/seekr-match.R
as_tibble.seekr_match.Rdas_tibble() and as.data.frame() convert a seekr_match vector into a
tibble or plain data frame, with one row per match and one column per field.
as_match() is the reverse: it converts a data frame back into a
seekr_match vector, validating all fields and checking for overlapping
matches within each file before returning.
Together, these functions unlock the full tidyverse toolkit for manipulating
match metadata. A typical pattern is to convert to a tibble, use
dplyr::mutate() or dplyr::filter() to derive or modify columns,
including the replacement field, and then convert back with as_match()
before calling replace_files().
Usage
# S3 method for class 'seekr_match'
as_tibble(x, ...)
# S3 method for class 'seekr_match'
as.data.frame(x, ...)
as_match(x)Arguments
- x
For
as_tibble()andas.data.frame(): aseekr_matchvector.For
as_match(): a data frame with at least the columns listed in the Fields section of seekr_match. Additional columns are silently ignored. All required columns must have the correct type:characterfor string fields,integerfor position fields.- ...
Not used. Present for compatibility with S3 generics.
Value
as_tibble() returns a tbl_df. as.data.frame() returns a plain
data.frame. In both cases the result has one row per match and one column
per field (see the Fields section of seekr_match).
as_match() returns a seekr_match vector. Matches within each file are
sorted by position. Overlapping or incoherent matches within the same file
cause an error.
Note: the empty_stage and exclusions attributes of the original
seekr_match vector are not preserved through the conversion.
See also
seekr_match for the list of available fields.
filter_match()for simpler subsetting that does not require conversion.vctrs::field()to access or modify a single field in place.replace_files()to apply staged replacements after converting back.
Examples
if (FALSE) { # \dontrun{
library(dplyr)
ext_path <- system.file("extdata", package = "seekr")
x <- seekr("TODO", path = ext_path)
# Convert to tibble
df <- as_tibble(x)
df
# Convert to plain data frame
as.data.frame(x)
# Convert back to seekr_match
as_match(df)
# Set a replacement for all matches
df$replacement <- "DONE"
as_match(df)
# Suppose you want to replace `"foo"` with `"bar"`, but only for the last
# match in each file, and only in files with at least three matches:
x <- seekr("foo", "bar", path = ext_path)
y <-
x |>
as_tibble() |>
mutate(
ith_match_per_file_rev = n():1L,
n_match_per_file = n(),
.by = path
) |>
filter(ith_match_per_file_rev == 1L, n_match_per_file >= 3L) |>
as_match()
# replace_files(y)
} # }