Skip to contents

For each file a scan read and flagged, writes a copy beside it with the flagged fields redacted by pii_redact(): survey.dta becomes survey_redacted.dta. Untouched columns are written as they were read; Stata and SPSS variable and value labels are kept. In text files, such as logs and scripts, matched email addresses, phone numbers and IP addresses are replaced wherever they occur.

Usage

pii_redact_files(
  path,
  findings = pii_scan(path, config = config),
  method = "placeholder",
  key = NULL,
  min_confidence = NULL,
  suffix = "_redacted",
  overwrite = FALSE,
  config = pii_config()
)

Arguments

path

Files or directories, as for pii_scan().

findings

The result of pii_scan() on path. Scanned if missing.

method

One method for every flagged column, or a named vector giving the method per finding type, with an optional default.

key

A secret string that determines the pseudonyms; needed only for "pseudonym". Keep it out of the shared files; use the same key to redact files that must still join.

min_confidence

The weakest findings to act on; the configuration's min_confidence, or "medium", unless given.

suffix

Added to a file's name to name its redacted copy.

overwrite

Whether to replace a redacted copy that already exists.

config

The project's configuration, from pii_config(); used for the scan when findings is missing.

Value

A tibble with one row per file: source, status, the destination written, and in detail what was redacted and what was left.

Details

Every file is accounted for in the result. A file can be redacted, partly redacted (something flagged could not be removed, such as a name inside free text), clean (nothing to redact; no copy written), unsupported (the format cannot be written back), skipped (it is itself a redacted copy), not read, or failed.

Delimited text, Excel, Stata, SPSS (sav, zsav), SAS transport (xpt), R data and Parquet files can be written back, as can plain text. An Excel file is written as xlsx without its formatting or formulas and needs the 'writexl' package. SAS data files, SPSS portable files, JSON and documents are reported as unsupported.

The originals are not changed or moved. Until they are removed or kept out of version control, the personal data are still in the project.

Examples

dir <- tempfile()
dir.create(dir)
survey <- data.frame(
  id = 1:20,
  q12 = paste0("person", 1:20, "@example.org"),
  village = rep(c("Palpur", "Netrad"), 10)
)
write.csv(survey, file.path(dir, "survey.csv"), row.names = FALSE)
writeLines("Call 98765 43210 after six.", file.path(dir, "notes.txt"))

report <- pii_redact_files(dir, method = c(email = "pseudonym", default = "drop"), key = "s3")
#> Wrote 2 redacted copies.
#> ℹ The originals are unchanged and still hold the personal data.
report
#> # A tibble: 2 × 4
#>   source                                      status   destination        detail
#>   <chr>                                       <chr>    <chr>              <chr> 
#> 1 /tmp/RtmpeoiXV6/file1d797591ce43/notes.txt  redacted /tmp/RtmpeoiXV6/f… repla…
#> 2 /tmp/RtmpeoiXV6/file1d797591ce43/survey.csv redacted /tmp/RtmpeoiXV6/f… redac…
read.csv(file.path(dir, "survey_redacted.csv"))[1:3, ]
#>   id                 q12
#> 1  1 grufwb9@wmfxars.wuv
#> 2  2 lliqur6@mieytmk.bse
#> 3  3 lhqjro8@pfspxjo.cru