Browse Source

working

master
boB Rudis 6 years ago
parent
commit
0845396c09
  1. 4
      NAMESPACE
  2. 1
      R/aaa.r
  3. 4
      R/env.R
  4. 28
      R/onAttach.R
  5. 20
      R/read-html.r
  6. 3
      R/version.r
  7. 4
      man/chrome_dump_pdf.Rd
  8. 4
      man/chrome_read_html.Rd
  9. 5
      man/chrome_shot.Rd
  10. 5
      man/chrome_version.Rd
  11. 6
      man/get_chrome_env.Rd
  12. 6
      man/set_chrome_env.Rd

4
NAMESPACE

@ -4,7 +4,7 @@ export(chrome_dump_pdf)
export(chrome_read_html)
export(chrome_shot)
export(chrome_version)
export(get_env)
export(set_env)
export(get_chrome_env)
export(set_chrome_env)
import(magick)
import(xml2)

1
R/aaa.r

@ -1 +0,0 @@
chrome_bin <- Sys.getenv("HEADLESS_CHROME")

4
R/env.R

@ -5,7 +5,7 @@
#' @export
#' @examples
#' get_env()
get_env <- function() {
get_chrome_env <- function() {
Sys.getenv("HEADLESS_CHROME")
}
@ -17,6 +17,6 @@ get_env <- function() {
#' @export
#' @examples
#' set_env("C:/Program Files/Google/Chrome/Application/chrome.exe")
set_env <- function(env=Sys.getenv("HEADLESS_CHROME")) {
set_chrome_env <- function(env=Sys.getenv("HEADLESS_CHROME")) {
Sys.setenv(HEADLESS_CHROME=env)
}

28
R/onAttach.R

@ -2,20 +2,26 @@
if (interactive()) {
if (unname(Sys.info()["sysname"] == "Windows")) {
if (Sys.getenv("HEADLESS_CHROME") == "") {
if (unname(Sys.info()["sysname"] == "Windows")) {
if (unname(Sys.info()["machine"] == "x86-64")) {
Sys.setenv(HEADLESS_CHROME="C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")
} else {
Sys.setenv(HEADLESS_CHROME="C:/Program Files/Google/Chrome/Application/chrome.exe")
}
if (unname(Sys.info()["machine"] == "x86-64")) {
Sys.setenv(HEADLESS_CHROME="C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")
} else {
Sys.setenv(HEADLESS_CHROME="C:/Program Files/Google/Chrome/Application/chrome.exe")
}
}
if (unname(Sys.info()["sysname"] == "Darwin")) {
Sys.setenv(HEADLESS_CHROME="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome")
}
# check other os
chrome_bin <- Sys.getenv("HEADLESS_CHROME")
if (unname(Sys.info()["sysname"] == "Darwin")) {
Sys.setenv(HEADLESS_CHROME="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome")
}
message(sprintf("Set Chrome binary to [%s].\nPass in manually to functions or use decapitated::set_chrome_env()",
Sys.getenv("HEADLESS_CHROME")))
}
}

20
R/read-html.r

@ -3,11 +3,14 @@
#' @md
#' @note This only grabs the `<body>` `innerHTML` contents
#' @param url URL to read from
#' @param chrome_bin the path to Chrome (auto-set from `HEADLESS_CHROME` environment variable)
#' @export
#' @examples
#' chrome_read_html("https://www.r-project.org/")
chrome_read_html <- function(url) {
tmp <- system2(chrome_bin, c("--version", "--headless", "--disable-gpu", "--dump-dom", url), stdout=TRUE)
chrome_read_html <- function(url, chrome_bin=Sys.getenv("HEADLESS_CHROME")) {
url <- shQuote(url)
tmp <- system2(chrome_bin, c("--headless", "--disable-gpu", "--dump-dom", url), stdout=TRUE)
print(tmp)
xml2::read_html(tmp)
}
@ -16,11 +19,13 @@ chrome_read_html <- function(url) {
#' @md
#' @note this is a quick version of the function and will overwrite `output.pdf` if it exists in CWD
#' @param url URL to read from
#' @param chrome_bin the path to Chrome (auto-set from `HEADLESS_CHROME` environment variable)
#' @export
#' @examples
#' chrome_dump_pdf("https://www.r-project.org/")
chrome_dump_pdf <- function(url) {
tmp <- system2(chrome_bin, c("--version", "--headless", "--disable-gpu", "--print-to-pdf", url))
chrome_dump_pdf <- function(url, chrome_bin=Sys.getenv("HEADLESS_CHROME")) {
url <- shQuote(url)
tmp <- system2(chrome_bin, c("--headless", "--disable-gpu", "--print-to-pdf", url))
}
#' Capture a screenshot
@ -34,13 +39,16 @@ chrome_dump_pdf <- function(url) {
#' @note this is a quick version of the function and will overwrite `screenshot.png` if it exists in CWD
#' @param url URL to read from
#' @param width,height screen size to emulate
#' @param chrome_bin the path to Chrome (auto-set from `HEADLESS_CHROME` environment variable)
#' @return `magick`
#' @export
#' @examples
#' chrome_shot("https://www.r-project.org/logo/Rlogo.svg")
chrome_shot <- function(url, width=NULL, height=NULL) {
chrome_shot <- function(url, width=NULL, height=NULL, chrome_bin=Sys.getenv("HEADLESS_CHROME")) {
args <- c("--version", "--headless", "--disable-gpu", "--screenshot")
args <- c("--headless", "--disable-gpu", "--screenshot")
url <- shQuote(url)
if (!is.null(width) & !is.null(height)) {
args <- c(args, sprintf("--window-size=%s,%s", height, width))

3
R/version.r

@ -1,9 +1,10 @@
#' Get Chrome version
#'
#' @md
#' @param chrome_bin the path to Chrome (auto-set from `HEADLESS_CHROME` environment variable)
#' @export
#' @examples
#' chrome_version()
chrome_version <- function() {
chrome_version <- function(chrome_bin=Sys.getenv("HEADLESS_CHROME")) {
system2(chrome_bin, "--version")
}

4
man/chrome_dump_pdf.Rd

@ -4,10 +4,12 @@
\alias{chrome_dump_pdf}
\title{"Print" to PDF}
\usage{
chrome_dump_pdf(url)
chrome_dump_pdf(url, chrome_bin = Sys.getenv("HEADLESS_CHROME"))
}
\arguments{
\item{url}{URL to read from}
\item{chrome_bin}{the path to Chrome (auto-set from \code{HEADLESS_CHROME} environment variable)}
}
\description{
"Print" to PDF

4
man/chrome_read_html.Rd

@ -4,10 +4,12 @@
\alias{chrome_read_html}
\title{Read a URL via headless Chrome and return the renderd \code{<body>} \code{innerHTML} DOM elements}
\usage{
chrome_read_html(url)
chrome_read_html(url, chrome_bin = Sys.getenv("HEADLESS_CHROME"))
}
\arguments{
\item{url}{URL to read from}
\item{chrome_bin}{the path to Chrome (auto-set from \code{HEADLESS_CHROME} environment variable)}
}
\description{
Read a URL via headless Chrome and return the renderd \code{<body>} \code{innerHTML} DOM elements

5
man/chrome_shot.Rd

@ -4,12 +4,15 @@
\alias{chrome_shot}
\title{Capture a screenshot}
\usage{
chrome_shot(url, width = NULL, height = NULL)
chrome_shot(url, width = NULL, height = NULL,
chrome_bin = Sys.getenv("HEADLESS_CHROME"))
}
\arguments{
\item{url}{URL to read from}
\item{width, height}{screen size to emulate}
\item{chrome_bin}{the path to Chrome (auto-set from \code{HEADLESS_CHROME} environment variable)}
}
\value{
\code{magick}

5
man/chrome_version.Rd

@ -4,7 +4,10 @@
\alias{chrome_version}
\title{Get Chrome version}
\usage{
chrome_version()
chrome_version(chrome_bin = Sys.getenv("HEADLESS_CHROME"))
}
\arguments{
\item{chrome_bin}{the path to Chrome (auto-set from \code{HEADLESS_CHROME} environment variable)}
}
\description{
Get Chrome version

6
man/get_env.Rd → man/get_chrome_env.Rd

@ -1,10 +1,10 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/env.R
\name{get_env}
\alias{get_env}
\name{get_chrome_env}
\alias{get_chrome_env}
\title{get an envrionment variable \code{HEADLESS_CHROME}}
\usage{
get_env()
get_chrome_env()
}
\description{
get an envrionment variable \code{HEADLESS_CHROME}

6
man/set_env.Rd → man/set_chrome_env.Rd

@ -1,10 +1,10 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/env.R
\name{set_env}
\alias{set_env}
\name{set_chrome_env}
\alias{set_chrome_env}
\title{set an envrionment variable \code{HEADLESS_CHROME}}
\usage{
set_env(env = Sys.getenv("HEADLESS_CHROME"))
set_chrome_env(env = Sys.getenv("HEADLESS_CHROME"))
}
\arguments{
\item{env}{path of chrome execute file for an envrionment variable \code{HEADLESS_CHROME}}
Loading…
Cancel
Save