diff --git a/.Rbuildignore b/.Rbuildignore index c5eb406..b5a7031 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -14,3 +14,5 @@ ignore/* ^codecov\.yml$ ^dev$ ^README\.html$ +^inst/examples/data\.qmd$ +^inst/examples/text\.qmd$ diff --git a/R/extract_metadata.R b/R/extract_metadata.R index 14b64e6..64d499a 100644 --- a/R/extract_metadata.R +++ b/R/extract_metadata.R @@ -23,6 +23,18 @@ extract_metadata <- function(file, password = NULL, copy = FALSE) { on.exit(pdfDocument$close()) info <- pdfDocument$getDocumentInformation() + + info_creation_date <- info$getCreationDate() + info_modification_date <- info$getModificationDate() + + if (!is.null(info_creation_date)) { + info_creation_date <- info_creation_date$getTime()$toString() + } + + if (!is.null(info_modification_date)) { + info_modification_date <- info_modification_date$getTime()$toString() + } + list( pages = pdfDocument$getNumberOfPages(), title = info$getTitle(), @@ -31,8 +43,8 @@ extract_metadata <- function(file, password = NULL, copy = FALSE) { keywords = info$getKeywords(), creator = info$getCreator(), producer = info$getProducer(), - created = info$getCreationDate()$getTime()$toString(), - modified = info$getModificationDate()$getTime()$toString(), + created = info_creation_date, + modified = info_modification_date, trapped = info$getTrapped() ) } diff --git a/R/output.R b/R/output.R index 47a68fb..44777ed 100644 --- a/R/output.R +++ b/R/output.R @@ -99,7 +99,7 @@ list_characters <- function(tables, delim = "\t", encoding = NULL, ...) { list_data_frames <- function(tables, delim = "\t", encoding = NULL, ...) { char <- list_characters(tables = tables, delim = delim, encoding = encoding) lapply(char, function(x) { - o <- try(read_delim(file = x, delim = delim)) + o <- try(read_delim(file = x, delim = delim, show_col_types = FALSE)) if (inherits(o, "try-error")) { return(x) } else { diff --git a/inst/examples/data.Rnw b/inst/examples/data.Rnw deleted file mode 100644 index 774f754..0000000 --- a/inst/examples/data.Rnw +++ /dev/null @@ -1,33 +0,0 @@ -\documentclass{article} - -\begin{document} - -<>= -library("xtable") -library("datasets") -@ - -<>= -xtable(mtcars, align = "|l|r|r|r|r|r|r|r|r|r|r|r|") -@ - -\clearpage - -<>= -xtable(head(iris), align = "|l|r|r|r|r|r|") -@ - -\vspace{20em} - -<>= -xtable(tail(iris), align = "|l|r|r|r|r|r|") -@ - -\clearpage - -<>= -xtable(ToothGrowth, align = "|l|r|r|r|") -@ - - -\end{document} \ No newline at end of file diff --git a/inst/examples/data.pdf b/inst/examples/data.pdf index 8bdda7f..0c111d9 100644 Binary files a/inst/examples/data.pdf and b/inst/examples/data.pdf differ diff --git a/inst/examples/data.qmd b/inst/examples/data.qmd new file mode 100644 index 0000000..e75fa21 --- /dev/null +++ b/inst/examples/data.qmd @@ -0,0 +1,33 @@ +--- +format: pdf +--- + +\pagenumbering{gobble} + +```{r, echo=FALSE} +library(xtable) +library(datasets) +options(xtable.comment = FALSE) +``` + +```{r, results="asis", echo=FALSE} +print(xtable(mtcars, align = "|l|r|r|r|r|r|r|r|r|r|r|r|"), include.rownames = FALSE) +``` + +\clearpage + +```{r, results="asis", echo=FALSE} +print(xtable(head(iris), align = "|l|r|r|r|r|r|"), include.rownames = FALSE) +``` + +\vspace{20em} + +```{r, results="asis", echo=FALSE} +print(xtable(tail(iris), align = "|l|r|r|r|r|r|"), include.rownames = FALSE) +``` + +\clearpage + +```{r, results="asis", echo=FALSE} +print(xtable(ToothGrowth[1:15, ], align = "|l|r|r|r|"), include.rownames = FALSE) +``` diff --git a/inst/examples/data.tex b/inst/examples/data.tex deleted file mode 100644 index 0abaa48..0000000 --- a/inst/examples/data.tex +++ /dev/null @@ -1,219 +0,0 @@ -\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color} -%% maxwidth is the original width if it is less than linewidth -%% otherwise use linewidth (to make sure the graphics do not exceed the margin) -\makeatletter -\def\maxwidth{ % - \ifdim\Gin@nat@width>\linewidth - \linewidth - \else - \Gin@nat@width - \fi -} -\makeatother - -\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345} -\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}% -\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}% -\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}% -\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}% -\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}% -\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}% -\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}% -\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}% -\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}% - -\usepackage{framed} -\makeatletter -\newenvironment{kframe}{% - \def\at@end@of@kframe{}% - \ifinner\ifhmode% - \def\at@end@of@kframe{\end{minipage}}% - \begin{minipage}{\columnwidth}% - \fi\fi% - \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep - \colorbox{shadecolor}{##1}\hskip-\fboxsep - % There is no \\@totalrightmargin, so: - \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}% - \MakeFramed {\advance\hsize-\width - \@totalleftmargin\z@ \linewidth\hsize - \@setminipage}}% - {\par\unskip\endMakeFramed% - \at@end@of@kframe} -\makeatother - -\definecolor{shadecolor}{rgb}{.97, .97, .97} -\definecolor{messagecolor}{rgb}{0, 0, 0} -\definecolor{warningcolor}{rgb}{1, 0, 1} -\definecolor{errorcolor}{rgb}{1, 0, 0} -\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX - -\usepackage{alltt} -\IfFileExists{upquote.sty}{\usepackage{upquote}}{} -\begin{document} - - - -% latex table generated in R 3.2.5 by xtable 1.8-2 package -% Fri Apr 29 22:30:10 2016 -\begin{table}[ht] -\centering -\begin{tabular}{|l|r|r|r|r|r|r|r|r|r|r|r|} - \hline - & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ - \hline -Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62 & 16.46 & 0.00 & 1.00 & 4.00 & 4.00 \\ - Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88 & 17.02 & 0.00 & 1.00 & 4.00 & 4.00 \\ - Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32 & 18.61 & 1.00 & 1.00 & 4.00 & 1.00 \\ - Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21 & 19.44 & 1.00 & 0.00 & 3.00 & 1.00 \\ - Hornet Sportabout & 18.70 & 8.00 & 360.00 & 175.00 & 3.15 & 3.44 & 17.02 & 0.00 & 0.00 & 3.00 & 2.00 \\ - Valiant & 18.10 & 6.00 & 225.00 & 105.00 & 2.76 & 3.46 & 20.22 & 1.00 & 0.00 & 3.00 & 1.00 \\ - Duster 360 & 14.30 & 8.00 & 360.00 & 245.00 & 3.21 & 3.57 & 15.84 & 0.00 & 0.00 & 3.00 & 4.00 \\ - Merc 240D & 24.40 & 4.00 & 146.70 & 62.00 & 3.69 & 3.19 & 20.00 & 1.00 & 0.00 & 4.00 & 2.00 \\ - Merc 230 & 22.80 & 4.00 & 140.80 & 95.00 & 3.92 & 3.15 & 22.90 & 1.00 & 0.00 & 4.00 & 2.00 \\ - Merc 280 & 19.20 & 6.00 & 167.60 & 123.00 & 3.92 & 3.44 & 18.30 & 1.00 & 0.00 & 4.00 & 4.00 \\ - Merc 280C & 17.80 & 6.00 & 167.60 & 123.00 & 3.92 & 3.44 & 18.90 & 1.00 & 0.00 & 4.00 & 4.00 \\ - Merc 450SE & 16.40 & 8.00 & 275.80 & 180.00 & 3.07 & 4.07 & 17.40 & 0.00 & 0.00 & 3.00 & 3.00 \\ - Merc 450SL & 17.30 & 8.00 & 275.80 & 180.00 & 3.07 & 3.73 & 17.60 & 0.00 & 0.00 & 3.00 & 3.00 \\ - Merc 450SLC & 15.20 & 8.00 & 275.80 & 180.00 & 3.07 & 3.78 & 18.00 & 0.00 & 0.00 & 3.00 & 3.00 \\ - Cadillac Fleetwood & 10.40 & 8.00 & 472.00 & 205.00 & 2.93 & 5.25 & 17.98 & 0.00 & 0.00 & 3.00 & 4.00 \\ - Lincoln Continental & 10.40 & 8.00 & 460.00 & 215.00 & 3.00 & 5.42 & 17.82 & 0.00 & 0.00 & 3.00 & 4.00 \\ - Chrysler Imperial & 14.70 & 8.00 & 440.00 & 230.00 & 3.23 & 5.34 & 17.42 & 0.00 & 0.00 & 3.00 & 4.00 \\ - Fiat 128 & 32.40 & 4.00 & 78.70 & 66.00 & 4.08 & 2.20 & 19.47 & 1.00 & 1.00 & 4.00 & 1.00 \\ - Honda Civic & 30.40 & 4.00 & 75.70 & 52.00 & 4.93 & 1.61 & 18.52 & 1.00 & 1.00 & 4.00 & 2.00 \\ - Toyota Corolla & 33.90 & 4.00 & 71.10 & 65.00 & 4.22 & 1.83 & 19.90 & 1.00 & 1.00 & 4.00 & 1.00 \\ - Toyota Corona & 21.50 & 4.00 & 120.10 & 97.00 & 3.70 & 2.46 & 20.01 & 1.00 & 0.00 & 3.00 & 1.00 \\ - Dodge Challenger & 15.50 & 8.00 & 318.00 & 150.00 & 2.76 & 3.52 & 16.87 & 0.00 & 0.00 & 3.00 & 2.00 \\ - AMC Javelin & 15.20 & 8.00 & 304.00 & 150.00 & 3.15 & 3.44 & 17.30 & 0.00 & 0.00 & 3.00 & 2.00 \\ - Camaro Z28 & 13.30 & 8.00 & 350.00 & 245.00 & 3.73 & 3.84 & 15.41 & 0.00 & 0.00 & 3.00 & 4.00 \\ - Pontiac Firebird & 19.20 & 8.00 & 400.00 & 175.00 & 3.08 & 3.85 & 17.05 & 0.00 & 0.00 & 3.00 & 2.00 \\ - Fiat X1-9 & 27.30 & 4.00 & 79.00 & 66.00 & 4.08 & 1.94 & 18.90 & 1.00 & 1.00 & 4.00 & 1.00 \\ - Porsche 914-2 & 26.00 & 4.00 & 120.30 & 91.00 & 4.43 & 2.14 & 16.70 & 0.00 & 1.00 & 5.00 & 2.00 \\ - Lotus Europa & 30.40 & 4.00 & 95.10 & 113.00 & 3.77 & 1.51 & 16.90 & 1.00 & 1.00 & 5.00 & 2.00 \\ - Ford Pantera L & 15.80 & 8.00 & 351.00 & 264.00 & 4.22 & 3.17 & 14.50 & 0.00 & 1.00 & 5.00 & 4.00 \\ - Ferrari Dino & 19.70 & 6.00 & 145.00 & 175.00 & 3.62 & 2.77 & 15.50 & 0.00 & 1.00 & 5.00 & 6.00 \\ - Maserati Bora & 15.00 & 8.00 & 301.00 & 335.00 & 3.54 & 3.57 & 14.60 & 0.00 & 1.00 & 5.00 & 8.00 \\ - Volvo 142E & 21.40 & 4.00 & 121.00 & 109.00 & 4.11 & 2.78 & 18.60 & 1.00 & 1.00 & 4.00 & 2.00 \\ - \hline -\end{tabular} -\end{table} - - -\clearpage - -% latex table generated in R 3.2.5 by xtable 1.8-2 package -% Fri Apr 29 22:30:10 2016 -\begin{table}[ht] -\centering -\begin{tabular}{|l|r|r|r|r|r|} - \hline - & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ - \hline -1 & 5.10 & 3.50 & 1.40 & 0.20 & setosa \\ - 2 & 4.90 & 3.00 & 1.40 & 0.20 & setosa \\ - 3 & 4.70 & 3.20 & 1.30 & 0.20 & setosa \\ - 4 & 4.60 & 3.10 & 1.50 & 0.20 & setosa \\ - 5 & 5.00 & 3.60 & 1.40 & 0.20 & setosa \\ - 6 & 5.40 & 3.90 & 1.70 & 0.40 & setosa \\ - \hline -\end{tabular} -\end{table} - - -\vspace{20em} - -% latex table generated in R 3.2.5 by xtable 1.8-2 package -% Fri Apr 29 22:30:10 2016 -\begin{table}[ht] -\centering -\begin{tabular}{|l|r|r|r|r|r|} - \hline - & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ - \hline -145 & 6.70 & 3.30 & 5.70 & 2.50 & virginica \\ - 146 & 6.70 & 3.00 & 5.20 & 2.30 & virginica \\ - 147 & 6.30 & 2.50 & 5.00 & 1.90 & virginica \\ - 148 & 6.50 & 3.00 & 5.20 & 2.00 & virginica \\ - 149 & 6.20 & 3.40 & 5.40 & 2.30 & virginica \\ - 150 & 5.90 & 3.00 & 5.10 & 1.80 & virginica \\ - \hline -\end{tabular} -\end{table} - - -\clearpage - -% latex table generated in R 3.2.5 by xtable 1.8-2 package -% Fri Apr 29 22:30:10 2016 -\begin{table}[ht] -\centering -\begin{tabular}{|l|r|r|r|} - \hline - & len & supp & dose \\ - \hline -1 & 4.20 & VC & 0.50 \\ - 2 & 11.50 & VC & 0.50 \\ - 3 & 7.30 & VC & 0.50 \\ - 4 & 5.80 & VC & 0.50 \\ - 5 & 6.40 & VC & 0.50 \\ - 6 & 10.00 & VC & 0.50 \\ - 7 & 11.20 & VC & 0.50 \\ - 8 & 11.20 & VC & 0.50 \\ - 9 & 5.20 & VC & 0.50 \\ - 10 & 7.00 & VC & 0.50 \\ - 11 & 16.50 & VC & 1.00 \\ - 12 & 16.50 & VC & 1.00 \\ - 13 & 15.20 & VC & 1.00 \\ - 14 & 17.30 & VC & 1.00 \\ - 15 & 22.50 & VC & 1.00 \\ - 16 & 17.30 & VC & 1.00 \\ - 17 & 13.60 & VC & 1.00 \\ - 18 & 14.50 & VC & 1.00 \\ - 19 & 18.80 & VC & 1.00 \\ - 20 & 15.50 & VC & 1.00 \\ - 21 & 23.60 & VC & 2.00 \\ - 22 & 18.50 & VC & 2.00 \\ - 23 & 33.90 & VC & 2.00 \\ - 24 & 25.50 & VC & 2.00 \\ - 25 & 26.40 & VC & 2.00 \\ - 26 & 32.50 & VC & 2.00 \\ - 27 & 26.70 & VC & 2.00 \\ - 28 & 21.50 & VC & 2.00 \\ - 29 & 23.30 & VC & 2.00 \\ - 30 & 29.50 & VC & 2.00 \\ - 31 & 15.20 & OJ & 0.50 \\ - 32 & 21.50 & OJ & 0.50 \\ - 33 & 17.60 & OJ & 0.50 \\ - 34 & 9.70 & OJ & 0.50 \\ - 35 & 14.50 & OJ & 0.50 \\ - 36 & 10.00 & OJ & 0.50 \\ - 37 & 8.20 & OJ & 0.50 \\ - 38 & 9.40 & OJ & 0.50 \\ - 39 & 16.50 & OJ & 0.50 \\ - 40 & 9.70 & OJ & 0.50 \\ - 41 & 19.70 & OJ & 1.00 \\ - 42 & 23.30 & OJ & 1.00 \\ - 43 & 23.60 & OJ & 1.00 \\ - 44 & 26.40 & OJ & 1.00 \\ - 45 & 20.00 & OJ & 1.00 \\ - 46 & 25.20 & OJ & 1.00 \\ - 47 & 25.80 & OJ & 1.00 \\ - 48 & 21.20 & OJ & 1.00 \\ - 49 & 14.50 & OJ & 1.00 \\ - 50 & 27.30 & OJ & 1.00 \\ - 51 & 25.50 & OJ & 2.00 \\ - 52 & 26.40 & OJ & 2.00 \\ - 53 & 22.40 & OJ & 2.00 \\ - 54 & 24.50 & OJ & 2.00 \\ - 55 & 24.80 & OJ & 2.00 \\ - 56 & 30.90 & OJ & 2.00 \\ - 57 & 26.40 & OJ & 2.00 \\ - 58 & 27.30 & OJ & 2.00 \\ - 59 & 29.40 & OJ & 2.00 \\ - 60 & 23.00 & OJ & 2.00 \\ - \hline -\end{tabular} -\end{table} - - - -\end{document} diff --git a/tests/testthat/test_extract_tables.R b/tests/testthat/test_extract_tables.R index 8d4c03d..cd7a7eb 100644 --- a/tests/testthat/test_extract_tables.R +++ b/tests/testthat/test_extract_tables.R @@ -16,12 +16,12 @@ test_that("Warning for ignored arguments", { test_that("Test 'guess' argument", { tab1a <- extract_tables(sf, pages = 1, guess = TRUE) tab1b <- extract_tables(sf, pages = 1, guess = FALSE) - expect_true(ncol(tab1a[[1]]) == 10) - expect_true(ncol(tab1b[[1]]) == 13) + expect_true(ncol(tab1a[[1]]) == 11) + expect_true(ncol(tab1b[[1]]) == 11) tab2a <- extract_tables(sf, pages = 2, guess = TRUE) tab2b <- extract_tables(sf, pages = 2, guess = FALSE) - expect_true(length(tab2a) == 2) + expect_true(length(tab2a) == 1) expect_true(length(tab2b) == 1) }) @@ -46,14 +46,14 @@ test_that("Test 'area' argument", { tab4a <- extract_tables(sf, pages = 1, area = a4a, guess = FALSE, output = "tibble") expect_true(is.list(tab4a)) expect_true(is.data.frame(tab4a[[1]])) - expect_true(nrow(tab4a[[1]]) == 32) - expect_true(ncol(tab4a[[1]]) == 12) + expect_true(nrow(tab4a[[1]]) == 28) + expect_true(ncol(tab4a[[1]]) == 10) a4b <- list(c(122, 149, 251, 464)) tab4b <- extract_tables(sf, pages = 1, area = a4b, guess = FALSE, output = "tibble") expect_true(is.list(tab4b)) expect_true(is.data.frame(tab4b[[1]])) - expect_true(nrow(tab4b[[1]]) == 9) - expect_true(ncol(tab4b[[1]]) == 8) + expect_true(nrow(tab4b[[1]]) == 8) + expect_true(ncol(tab4b[[1]]) == 9) }) test_that("Test 'columns' argument", { @@ -61,7 +61,7 @@ test_that("Test 'columns' argument", { expect_true(is.list(tab5)) expect_true(length(tab5) == 1) expect_true(ncol(tab5[[1]]) == 2) - expect_true(nrow(tab5[[1]]) == 33) + expect_true(nrow(tab5[[1]]) == 32) }) test_that("Extract from encrypted PDF", { diff --git a/tests/testthat/test_write_methods.R b/tests/testthat/test_write_methods.R index daace32..206c949 100644 --- a/tests/testthat/test_write_methods.R +++ b/tests/testthat/test_write_methods.R @@ -11,8 +11,8 @@ test_that("Make list of matrices", { t1 <- tabulapdf:::list_matrices(tabs) expect_true(is.list(t1)) expect_true(is.matrix(t1[[1]])) - expect_true(ncol(t1[[1]]) == 10) - expect_true(nrow(t1[[1]]) == 32) + expect_true(ncol(t1[[1]]) == 11) + expect_true(nrow(t1[[1]]) == 33) }) test_that("Make list of character vectors", { @@ -30,17 +30,17 @@ test_that("Make list of data.frames", { test_that("Write CSV Files", { t4 <- tabulapdf:::write_csvs(tabs, file = pdffile, outdir = tempdir()) expect_true(is.character(t4)) - expect_identical(length(dir(t4, pattern = "csv$")), 4L) + expect_identical(length(dir(t4, pattern = "csv$")), 3L) }) test_that("Write TSV Files", { t5 <- tabulapdf:::write_tsvs(tabs, file = pdffile, outdir = tempdir()) expect_true(is.character(t5)) - expect_identical(length(dir(t5, pattern = "tsv$")), 4L) + expect_identical(length(dir(t5, pattern = "tsv$")), 3L) }) test_that("Write JSON Files", { t6 <- tabulapdf:::write_jsons(tabs, file = pdffile, outdir = tempdir()) expect_true(is.character(t6)) - expect_identical(length(dir(t6, pattern = "json$")), 4L) + expect_identical(length(dir(t6, pattern = "json$")), 3L) })