{"action":"opened","issue":{"url":"https://api.github.com/repos/hotliu/Web-Log/issues/9","repository_url":"https://api.github.com/repos/hotliu/Web-Log","labels_url":"https://api.github.com/repos/hotliu/Web-Log/issues/9/labels{/name}","comments_url":"https://api.github.com/repos/hotliu/Web-Log/issues/9/comments","events_url":"https://api.github.com/repos/hotliu/Web-Log/issues/9/events","html_url":"https://github.com/hotliu/Web-Log/issues/9","id":196275839,"number":9,"title":"tabulizer_tutorial.md","user":{"login":"hotliu","id":11190589,"avatar_url":"https://avatars.githubusercontent.com/u/11190589?v=3","gravatar_id":"","url":"https://api.github.com/users/hotliu","html_url":"https://github.com/hotliu","followers_url":"https://api.github.com/users/hotliu/followers","following_url":"https://api.github.com/users/hotliu/following{/other_user}","gists_url":"https://api.github.com/users/hotliu/gists{/gist_id}","starred_url":"https://api.github.com/users/hotliu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hotliu/subscriptions","organizations_url":"https://api.github.com/users/hotliu/orgs","repos_url":"https://api.github.com/users/hotliu/repos","events_url":"https://api.github.com/users/hotliu/events{/privacy}","received_events_url":"https://api.github.com/users/hotliu/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-12-18T13:31:52Z","updated_at":"2016-12-18T13:31:52Z","closed_at":null,"body":"### tabulizer tutorial\r\n\r\n#### for v0.1.22\r\n\r\n* [Installation][0]\r\n* [Using the package][1]\r\n* [Citing][2]\r\n* [License and bugs][3]\r\n\r\n**tabulizer** provides R bindings to the [Tabula java library][4], which can be used to computationaly extract tables from PDF documents.\r\n\r\n## Installation\r\n\r\nIt's not yet on CRAN\r\n\r\nDevelopment version from GitHub\r\n\r\n    if(!require(\"ghit\")){\r\n        install.packages(\"ghit\")\r\n    }\r\n    # on 64-bit Windows\r\n    ghit::install_github(c(\"ropenscilabs/tabulizerjars\", \"ropenscilabs/tabulizer\"), INSTALL_opts = \"--no-multiarch\")\r\n    # elsewhere\r\n    ghit::install_github(c(\"ropenscilabs/tabulizerjars\", \"ropenscilabs/tabulizer\"))\r\n\r\nSee [the source][5] for more details on installation.\r\n\r\n    library(\"tabulizer\")\r\n\r\n## Usage\r\n\r\nThe main function extract_tables() mimics the command-line behavior of the Tabula, by extracting all tables from a PDF file and, by default, returns those tables as a list of character matrices in R.\r\n\r\n    library(\"tabulizer\")\r\n    f <- system.file(\"examples\", \"data.pdf\", package = \"tabulizer\")\r\n    \r\n    # extract table from first page of example PDF\r\n    tab <- extract_tables(f, pages = 1)\r\n    head(tab[[1]])\r\n    #>      [,1]   [,2]  [,3]    [,4]  [,5]   [,6]    [,7]    [,8] [,9] [,10] \r\n    #> [1,] \"mpg\"  \"cyl\" \"disp\"  \"hp\"  \"drat\" \"wt\"    \"qsec\"  \"vs\" \"am\" \"gear\"\r\n    #> [2,] \"21.0\" \"6\"   \"160.0\" \"110\" \"3.90\" \"2.620\" \"16.46\" \"0\"  \"1\"  \"4\"   \r\n    #> [3,] \"21.0\" \"6\"   \"160.0\" \"110\" \"3.90\" \"2.875\" \"17.02\" \"0\"  \"1\"  \"4\"   \r\n    #> [4,] \"22.8\" \"4\"   \"108.0\" \"93\"  \"3.85\" \"2.320\" \"18.61\" \"1\"  \"1\"  \"4\"   \r\n    #> [5,] \"21.4\" \"6\"   \"258.0\" \"110\" \"3.08\" \"3.215\" \"19.44\" \"1\"  \"0\"  \"3\"   \r\n    #> [6,] \"18.7\" \"8\"   \"360.0\" \"175\" \"3.15\" \"3.440\" \"17.02\" \"0\"  \"0\"  \"3\"\r\n\r\nThe pages argument allows you to select which pages to attempt to extract tables from. By default, Tabula (and thus tabulizer) checks every page for tables using a detection algorithm and returns all of them. pages can be an integer vector of any length; pages are indexed from 1.\r\n\r\nIt is possible to specify a remote file, which will be copied to R's temporary directory before processing:\r\n\r\n    f2 <- \"https://github.com/leeper/tabulizer/raw/master/inst/examples/data.pdf\"\r\n    extract_tables(f2, pages = 2)\r\n    #> [[1]]\r\n    #>      [,1]            [,2]           [,3]            [,4]          \r\n    #> [1,] \"Sepal.Length \" \"Sepal.Width \" \"Petal.Length \" \"Petal.Width \"\r\n    #> [2,] \"5.1 \"          \"3.5 \"         \"1.4 \"          \"0.2 \"        \r\n    #> [3,] \"4.9 \"          \"3.0 \"         \"1.4 \"          \"0.2 \"        \r\n    #> [4,] \"4.7 \"          \"3.2 \"         \"1.3 \"          \"0.2 \"        \r\n    #> [5,] \"4.6 \"          \"3.1 \"         \"1.5 \"          \"0.2 \"        \r\n    #> [6,] \"5.0 \"          \"3.6 \"         \"1.4 \"          \"0.2 \"        \r\n    #> [7,] \"5.4 \"          \"3.9 \"         \"1.7 \"          \"0.4 \"        \r\n    #>      [,5]     \r\n    #> [1,] \"Species\"\r\n    #> [2,] \"setosa\" \r\n    #> [3,] \"setosa\" \r\n    #> [4,] \"setosa\" \r\n    #> [5,] \"setosa\" \r\n    #> [6,] \"setosa\" \r\n    #> [7,] \"setosa\" \r\n    #> \r\n    #> [[2]]\r\n    #>      [,1]   [,2]            [,3]           [,4]            [,5]          \r\n    #> [1,] \"\"     \"Sepal.Length \" \"Sepal.Width \" \"Petal.Length \" \"Petal.Width \"\r\n    #> [2,] \"145 \" \"6.7 \"          \"3.3 \"         \"5.7 \"          \"2.5 \"        \r\n    #> [3,] \"146 \" \"6.7 \"          \"3.0 \"         \"5.2 \"          \"2.3 \"        \r\n    #> [4,] \"147 \" \"6.3 \"          \"2.5 \"         \"5.0 \"          \"1.9 \"        \r\n    #> [5,] \"148 \" \"6.5 \"          \"3.0 \"         \"5.2 \"          \"2.0 \"        \r\n    #> [6,] \"149 \" \"6.2 \"          \"3.4 \"         \"5.4 \"          \"2.3 \"        \r\n    #> [7,] \"150 \" \"5.9 \"          \"3.0 \"         \"5.1 \"          \"1.8 \"        \r\n    #>      [,6]       \r\n    #> [1,] \"Species\"  \r\n    #> [2,] \"virginica\"\r\n    #> [3,] \"virginica\"\r\n    #> [4,] \"virginica\"\r\n    #> [5,] \"virginica\"\r\n    #> [6,] \"virginica\"\r\n    #> [7,] \"virginica\"\r\n\r\n### Modifying the Return Value\r\n\r\nBy default, extract_tables() returns a list of character matrices. This is because many tables might be malformed or irregular and thus not be easily coerced to an R data.frame. This can easily be changed by specifying the method argument:\r\n\r\n    # attempt to coerce tables to data.frames\r\n    extract_tables(f, pages = 2, method = \"data.frame\")\r\n    #> [[1]]\r\n    #>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species\r\n    #> 1          5.1         3.5          1.4         0.2  setosa\r\n    #> 2          4.9         3.0          1.4         0.2  setosa\r\n    #> 3          4.7         3.2          1.3         0.2  setosa\r\n    #> 4          4.6         3.1          1.5         0.2  setosa\r\n    #> 5          5.0         3.6          1.4         0.2  setosa\r\n    #> 6          5.4         3.9          1.7         0.4  setosa\r\n    #> \r\n    #> [[2]]\r\n    #>     X Sepal.Length Sepal.Width Petal.Length Petal.Width   Species\r\n    #> 1 145          6.7         3.3          5.7         2.5 virginica\r\n    #> 2 146          6.7         3.0          5.2         2.3 virginica\r\n    #> 3 147          6.3         2.5          5.0         1.9 virginica\r\n    #> 4 148          6.5         3.0          5.2         2.0 virginica\r\n    #> 5 149          6.2         3.4          5.4         2.3 virginica\r\n    #> 6 150          5.9         3.0          5.1         1.8 virginica\r\n\r\nTabula itself implements three \"writer\" methods that write extracted tables to disk as CSV, TSV, or JSON files. These can be specified by method = \"csv\", method = \"tsv\", and method = \"json\", respectively. For CSV and TSV, one file is written to disk for each table, in the same directory as the original file (or the temporary directory if the file is a remote PDF). For JSON, one file is written containing information about all tables. For these methods, extract_tables() returns a path to the directory containing the output files.\r\n\r\n    # extract tables to CSVs\r\n    extract_tables(f, method = \"csv\")\r\n    #> [1] \"/Library/Frameworks/R.framework/Versions/3.3/Resources/library/tabulizer/examples\"\r\n\r\nIf none of the standard methods works well, you can specify method = \"asis\" to return an rJava \"jobjRef\" object, which is a pointer to a Java ArrayList of Tabula Table objects. Working with that object might be quite awkward as it requires knowledge of Java and Tabula's internals, but might be useful to advanced users for debugging purposes.\r\n\r\n### Extracting Areas\r\n\r\nBy default, tabulizer uses Tabula's table detection algorithms to automatically identify tables within each page of a PDF. This automatic detection can be toggled off by setting guess = FALSE and specifying an \"area\" within each PDF page to extract the table from. Here is a comparison of the default settings, versus extracting from two alternative areas within a page:\r\n\r\n    str(extract_tables(f, pages = 2, guess = TRUE, method = \"data.frame\"))\r\n    #> List of 2\r\n    #>  $ :'data.frame':    6 obs. of  5 variables:\r\n    #>   ..$ Sepal.Length: num [1:6] 5.1 4.9 4.7 4.6 5 5.4\r\n    #>   ..$ Sepal.Width : num [1:6] 3.5 3 3.2 3.1 3.6 3.9\r\n    #>   ..$ Petal.Length: num [1:6] 1.4 1.4 1.3 1.5 1.4 1.7\r\n    #>   ..$ Petal.Width : num [1:6] 0.2 0.2 0.2 0.2 0.2 0.4\r\n    #>   ..$ Species     : chr [1:6] \"setosa\" \"setosa\" \"setosa\" \"setosa\" ...\r\n    #>  $ :'data.frame':    6 obs. of  6 variables:\r\n    #>   ..$ X           : num [1:6] 145 146 147 148 149 150\r\n    #>   ..$ Sepal.Length: num [1:6] 6.7 6.7 6.3 6.5 6.2 5.9\r\n    #>   ..$ Sepal.Width : num [1:6] 3.3 3 2.5 3 3.4 3\r\n    #>   ..$ Petal.Length: num [1:6] 5.7 5.2 5 5.2 5.4 5.1\r\n    #>   ..$ Petal.Width : num [1:6] 2.5 2.3 1.9 2 2.3 1.8\r\n    #>   ..$ Species     : chr [1:6] \"virginica\" \"virginica\" \"virginica\" \"virginica\" ...\r\n    str(extract_tables(f, pages = 2, area = list(c(126, 149, 212, 462)), guess = FALSE, method = \"data.frame\"))\r\n    #> List of 1\r\n    #>  $ :'data.frame':    6 obs. of  5 variables:\r\n    #>   ..$ Sepal.Length: num [1:6] 5.1 4.9 4.7 4.6 5 5.4\r\n    #>   ..$ Sepal.Width : num [1:6] 3.5 3 3.2 3.1 3.6 3.9\r\n    #>   ..$ Petal.Length: num [1:6] 1.4 1.4 1.3 1.5 1.4 1.7\r\n    #>   ..$ Petal.Width : num [1:6] 0.2 0.2 0.2 0.2 0.2 0.4\r\n    #>   ..$ Species     : chr [1:6] \"setosa\" \"setosa\" \"setosa\" \"setosa\" ...\r\n    str(extract_tables(f, pages = 2, area = list(c(126, 284, 174, 417)), guess = FALSE, method = \"data.frame\"))\r\n    #> List of 1\r\n    #>  $ :'data.frame':    3 obs. of  2 variables:\r\n    #>   ..$ Petal.Length: num [1:3] 1.4 1.4 1.3\r\n    #>   ..$ Petal.Width : num [1:3] 0.2 0.2 0.2\r\n\r\nThe area argument should be a list either of length 1 (to use the same area for each specified page) or equal to the number of pages specified. This also means that you can extract multiple areas from one page, but specifying the page twice and indicating the two areas separately:\r\n\r\n    a2 <- list(c(126, 149, 212, 462), c(126, 284, 174, 417))\r\n    str(extract_tables(f, pages = c(2,2), area = a2, guess = FALSE, method = \"data.frame\"))\r\n    #> List of 2\r\n    #>  $ :'data.frame':    6 obs. of  5 variables:\r\n    #>   ..$ Sepal.Length: num [1:6] 5.1 4.9 4.7 4.6 5 5.4\r\n    #>   ..$ Sepal.Width : num [1:6] 3.5 3 3.2 3.1 3.6 3.9\r\n    #>   ..$ Petal.Length: num [1:6] 1.4 1.4 1.3 1.5 1.4 1.7\r\n    #>   ..$ Petal.Width : num [1:6] 0.2 0.2 0.2 0.2 0.2 0.4\r\n    #>   ..$ Species     : chr [1:6] \"setosa\" \"setosa\" \"setosa\" \"setosa\" ...\r\n    #>  $ :'data.frame':    3 obs. of  2 variables:\r\n    #>   ..$ Petal.Length: num [1:3] 1.4 1.4 1.3\r\n    #>   ..$ Petal.Width : num [1:3] 0.2 0.2 0.2\r\n\r\n### Interactive Table Extraction\r\n\r\nIn addition to the programmatic extraction offered by extract_tables(), it is also possible to work interactively with PDFs via the extract_areas() function. This function triggers a process by which each (specified) page of a PDF is converted to a PNG image file and then loaded as an R graphic. From there, you can use your mouse to specify upper-left and lower-right bounds of an area on each page. Pages are cycled through automatically and, after selecting areas for each page, those areas are extracted auto-magically (and the return value is the same as for extract_tables()). Here's a shot of it in action:\r\n\r\n[![extract_areas()](http://i.imgur.com/USTyQl7.gif)](http://i.imgur.com/USTyQl7.gif)\r\n\r\nlocate_areas() handles the area identification process without performing the extraction, which may be useful as a debugger, or simply to define areas to be used in a programmatic extraction.\r\n\r\n### Miscellaneous Functionality\r\n\r\nTabula is built on top of the [Java PDFBox library][6]), which provides low-level functionality for working with PDFs. A few of these tools are exposed through tabulizer, as they might be useful for debugging or generally for working with PDFs. These functions include:\r\n\r\n* extract_text() converts the text of an entire file or specified pages into an R character vector.\r\n* split_pdf() and merge_pdfs() split and merge PDF documents, respectively.\r\n* extract_metadata() extracts PDF metadata as a list.\r\n* get_n_pages() determines the number of pages in a document.\r\n* get_page_dims() determines the width and height of each page in pt (the unit used by area and columns arguments).\r\n* make_thumbnails() converts specified pages of a PDF file to image files.\r\n\r\n## Citing\r\n\r\n> Thomas J. Leeper (2016). tabulizer: Bindings for Tabula PDF Table Extractor Library. R package version 0.1.21. [> https://github.com/ropenscilabs/tabulizer][7]\r\n\r\n## License and bugs\r\n\r\n* License: [MIT][8]\r\n* Report bugs at [our GitHub repo for tabulizer][9]\r\n\r\n[0]: #installation\r\n[1]: #usage\r\n[2]: #citing\r\n[3]: #license_bugs\r\n[4]: https://github.com/tabulapdf/tabula-java/\r\n[5]: https://github.com/ropenscilabs/tabulizer#installation\r\n[6]: https://pdfbox.apache.org/\r\n[7]: https://github.com/ropenscilabs/tabulizer\r\n[8]: http://opensource.org/licenses/MIT\r\n[9]: https://github.com/ropenscilabs/tabulizer/issues?state=open"}}
{"actor":{"display_login":"hotliu"}}