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

adding new functionality suggested in #107 #108

Open
wants to merge 7 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Authors@R: c(person("Thomas J.", "Leeper",
comment = "rOpenSci reviewer"),
person("Lincoln", "Mullen",
role = "ctb",
comment = "rOpenSci reviewer"))
comment = "rOpenSci reviewer"),
person("Thore", "Engel",
role = "ctb"))
Maintainer: Tom Paskhalis <[email protected]>
Description: Bindings for the 'Tabula' <http://tabula.technology/> 'Java'
library, which can extract tables from PDF documents. The 'tabulizerjars'
Expand All @@ -41,4 +43,4 @@ Suggests:
testthat
SystemRequirements: Java (>= 7.0)
VignetteBuilder: knitr
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(extract_text)
export(get_n_pages)
export(get_page_dims)
export(locate_areas)
export(locate_columns)
export(make_thumbnails)
export(merge_pdfs)
export(split_pdf)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@


# CHANGES TO tabulizer 0.2.2

* `extract_tables()` gets `outdir` argument for writing out CSV, TSV and JSON
files.
* Fixes in vignette.
* addition of `locate_columns()` function.

# CHANGES TO tabulizer 0.2.1

Expand Down
108 changes: 108 additions & 0 deletions R/locate_columns.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@

#' Locate separators between columns
#'
#' This function allows the user to manually locate the separators between columns of a table in a pdf. The output can be used as the \code{columns} argument in \code{extract_tables()}
#'
#' Manually selecting the separators can ensure that values stay in their respective columns. This is useful when some rows of a table have no or only little white space between columns. The code is an adaptation of the \code{locate_area()} function and its helpers.
#' @param file A character string specifying the path or URL to a PDF file.
#' @param pages An optional integer vector specifying pages to extract from.
#' @param resolution An integer specifying the resolution of the PNG images conversions. A low resolution is used by default to speed image loading.
#' @param copy Specifies whether the original local file(s) should be copied to tempdir() before processing. FALSE by default. The argument is ignored if file is URL.
#' @return a list.
#' @author Thore Engel <[email protected]>
#' @export
#'
#' @examples
#' \donttest{
#' f <- system.file("examples", "data.pdf", package = "tabulizer")
#' separators<-locate_columns(f, pages= 1 )
#' extract_tables(f,pages = 1, columns = separators[1])
#' }
#'
locate_columns <- function(file,
pages = NULL,
resolution = 60L,
copy = FALSE) {
if (!interactive()) {
stop("locate_columns() is only available in an interactive session")
} else {
requireNamespace("graphics")
requireNamespace("grDevices")
}

file <- localize_file(file, copy = copy)
# on.exit(unlink(file), add = TRUE)
dims <- get_page_dims(file, pages = pages)
paths <- make_thumbnails(file,
outdir = tempdir(),
pages = pages,
format = "png",
resolution = resolution)
on.exit(unlink(paths), add = TRUE)

separators <- rep(list(NULL), length(paths))
i <- 1
warnThisTime <- TRUE
while (TRUE) {
if (!is.na(paths[i])) {
a <- try_columns_reduced(file = paths[i],
dims = dims[[i]],warn = warnThisTime)
if(warnThisTime) warnThisTime <- F
if (!is.null(a[["separators"]])) {
separators[[i]] <- a[["separators"]]
}
if (tolower(a[["key"]]) %in% c("del", "delete", "ctrl-h")) {
separators[i] <- list(NULL)
next
} else if (tolower(a[["key"]]) %in% c("home")) {
i <- 1
next
} else if (tolower(a[["key"]]) %in% c("end")) {
i <- length(paths)
next
} else if (tolower(a[["key"]]) %in% c("pgup", "page_up", "up", "left")) {
i <- if (i == 1) 1 else i - 1
next
} else if (tolower(a[["key"]]) %in% c("q")) {
break
}
}
i <- i + 1
if (i > length(paths)) {
break
}
}
return(separators)
}


#' Helper function to locate_columns()
#'
#' @param file A character string specifying the path or URL to a PDF file.
#' @param dims An integer specifying the resolution of the PNG images conversions. A low resolution is used by default to speed image loading.
#' @param warn Display warning?

try_columns_reduced <- function(file, dims, warn = FALSE) {
if (warn) {
message("Click at the locations of separators between columns.")
}
if (grDevices::dev.capabilities()[["rasterImage"]] == "no") {
stop("Graphics device does not support rasterImage() plotting")
}
thispng <- readPNG(file, native = TRUE)
drawPage <- function() {
graphics::plot(c(0, dims[1]), c(0, dims[2]), type = "n", xlab = "", ylab = "", asp = 1)
graphics::rasterImage(thispng, 0, 0, dims[1], dims[2])
}

pre_par <- graphics::par(mar=c(0,0,0,0), xaxs = "i", yaxs = "i", bty = "n")
on.exit(graphics::par(pre_par), add = TRUE)
drawPage()
on.exit(grDevices::dev.off(), add = TRUE)

tmp <- locator()
graphics::abline(v=tmp$x)
Sys.sleep(4)
separators= as.numeric(tmp$x)
return(list(key = "right", separators = separators))
}
4 changes: 2 additions & 2 deletions man/extract_areas.Rd

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

37 changes: 37 additions & 0 deletions man/locate_columns.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/make_thumbnails.Rd

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

18 changes: 18 additions & 0 deletions man/try_columns_reduced.Rd

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