diff --git a/DESCRIPTION b/DESCRIPTION index 939e1d5..5dd2b6c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,5 +17,6 @@ Imports: mime, rjson, Rook -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.3 Encoding: UTF-8 +Roxygen: list(markdown = TRUE) diff --git a/R/app.R b/R/app.R index 32e0126..494e0f4 100644 --- a/R/app.R +++ b/R/app.R @@ -1,49 +1,50 @@ # This file is part of digitizeR -# +# # digitizeR is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. -# +# # digitizeR is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with digitizeR. If not, see . #' @importFrom rjson fromJSON -.wpd_createBackend <- function() { +.wpd_createBackend <- function() { backend <- list( - call = function(req) { - # Handle HTTP requests - + call = function(req) { + # Handle HTTP requests + # Host files from checked out WebPlotDigitizer reqFile <- req$PATH_INFO - packagePath <- system.file('wpd', package="digitizeR") + packagePath <- system.file('wpd', package="digitizeR") filepath <- paste(packagePath, reqFile, sep="/") - + # If the file is not found, then return a 404 code. message <- list(status = 404L, headers = list('Content-Type' = 'text/html'), body = 'error!') - - if (file.exists(filepath)) { - # if this is a PHP script, then we probably got a POST request to generate a CSV or JSON file - # Maybe we can even run R scripts in the future? - # TODO: Use tryCatch here: + + if (file.exists(filepath)) { + # if this is a PHP script, then we probably got a POST request + # to generate a CSV or JSON file Maybe we can even run R scripts + # in the future? + # TODO: Use tryCatch here: if (grepl("json.php$", reqFile)) { # Generate JSON file - + # Read POST data postVariables <- list(data = "", filename = "") - + postInput <- req[["rook.input"]] postData <- postInput$read_lines() postData <- Rook::Utils$unescape(postData) - - # There's probably a better way to do this: + + # There's probably a better way to do this: postRegexPattern <- "^data=(\\{.*)\\&filename=(.*)" - if(grepl(postRegexPattern, postData)) { + if(grepl(postRegexPattern, postData)) { postVariables$data <- gsub(postRegexPattern, "\\1", postData) postVariables$filename <- gsub(postRegexPattern, "\\2", postData) message <- list(status = 200L, @@ -52,21 +53,21 @@ body = postVariables$data ) } else { - cat("ERROR: Invalid POST data sent for", reqFile, "\n") - } + cat("ERROR: Invalid POST data sent for", reqFile, "\n") + } } else if (grepl("csvexport.php$", reqFile)) { # Generate CSV file - + # Read POST data postVariables <- list(data = "", filename = "") - + postInput <- req[["rook.input"]] - postData <- postInput$read_lines() + postData <- postInput$read_lines() postData <- Rook::Utils$unescape(postData) - - # There's probably a better way to do this: + + # There's probably a better way to do this: postRegexPattern <- "^data=(.*)\\&filename=(.*)" - if(grepl(postRegexPattern, postData)) { + if(grepl(postRegexPattern, postData)) { postVariables$data <- gsub(postRegexPattern, "\\1", postData) postVariables$filename <- gsub(postRegexPattern, "\\2", postData) csvData <- rjson::fromJSON(postVariables$data) @@ -76,25 +77,25 @@ body = csvData ) } else { - cat("ERROR: Invalid POST data sent for", reqFile, "\n") - } - + cat("ERROR: Invalid POST data sent for", reqFile, "\n") + } + } else { # if this is not a PHP file, then just determine the mime type and return mimetype <- mime::guess_type(filepath) message <- list(status = 200L, headers = list('Content-Type' = mimetype), - body = list(file = filepath)) - } + body = list(file = filepath)) + } } - + return(message) }, onWSOpen = function(ws) { - # Handle WebSocket communication in the future + # Handle WebSocket communication in the future } ) - + return(backend) } - + diff --git a/R/digitizeR.R b/R/digitizeR.R index 0f42d4d..1fa9061 100644 --- a/R/digitizeR.R +++ b/R/digitizeR.R @@ -1,42 +1,43 @@ # This file is part of digitizeR -# +# # digitizeR is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. -# +# # digitizeR is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with digitizeR. If not, see . #' digitizeR: Tool to extract numerical data from images of plots, maps etc. -#' -#' The digitizeR package provides a web based interface (WebPlotDigitizer) to interactively extract -#' numerical data from images of plots, maps, microscope images etc. Some tools to make simple distance -#' and angle measurements are also available. In the future, this package will also allow -#' real-time interaction between R and the hosted web application via WebSockets. -#' +#' +#' The digitizeR package provides a web based interface (WebPlotDigitizer) to +#' interactively extract numerical data from images of plots, maps, microscope +#' images etc. Some tools to make simple distance and angle measurements are +#' also available. In the future, this package will also allow real-time +#' interaction between R and the hosted web application via WebSockets. +#' #' @section Available Functions: -#' -#' \itemize{ -#' \item \code{\link{wpd_launch}}: Start HTTP server that hosts WebPlotDigitizer and +#' +#' +#' - [wpd_launch()]: Start HTTP server that hosts WebPlotDigitizer and #' open the local URL in the browser. -#' \item \code{\link{wpd_close}}: Shutdown the HTTP server. -#' \item \code{\link{wpd_isOpen}}: Check is the HTTP server is currently running. -#' } -#' -#' +#' - [wpd_close()]: Shutdown the HTTP server. +#' - [wpd_isOpen()]: Check is the HTTP server is currently running. +#' +#' +#' #' @docType package #' @name digitizeR NULL -#' Start a local HTTP server that hosts WebPlotDigitizer. +#' Start a local HTTP server that hosts WebPlotDigitizer. #' This will also open a browser window pointing to the local URL. -#' +#' #' @param location IP address or machine name of the server. Defaults to "0.0.0.0". #' @param port Port number of the HTTP server. Defaults to 8000. #' @return Server handle that is later used to shutdown the server using wpd_close() @@ -48,14 +49,14 @@ NULL #' app <- wpd_launch(location="192.168.1.100", port=8080) #' } wpd_launch <- function(location = '0.0.0.0', port = 8000) { - + app <- new.env() - + # Start httpuv based server in the background app$backend = .wpd_createBackend() - + app$serverInstance <- httpuv::startDaemonizedServer(location, port, app$backend) - + # Construct the hosted URL link url <- 'http://' if (location == '0.0.0.0') { @@ -64,16 +65,16 @@ wpd_launch <- function(location = '0.0.0.0', port = 8000) { url <- paste(url, location, sep='') } url <- paste(url, ':', port, '/index.html', sep='') - + utils::browseURL(url) # Launch browser with the WPD url - + cat('Starting WPD. If a browser window does not open, then browse to:', url, '\n') app$isOpen <- TRUE return(app) } #' Shutdown the HTTP server that is currently hosting WebPlotDigitizer. -#' +#' #' @param app Server handle that was obtained by executing wpd_launch() #' @export #' @examples @@ -89,7 +90,7 @@ wpd_close <- function(app) { } #' Check if the HTTP server is currently running. -#' +#' #' @param app Server handle that was obtained by executng wpd_launch() #' @export wpd_isOpen <- function(app) { diff --git a/R/read_wpd.R b/R/read_wpd.R index 735b935..7e32286 100644 --- a/R/read_wpd.R +++ b/R/read_wpd.R @@ -1,10 +1,9 @@ #' Read WebPlotDigitizer JSON data #' #' @inheritParams rjson::fromJSON -#' @param include_pixel Include the pixel data in the returned data frame -#' @return A data.frame with colums for \code{x}, \code{y}, and \code{DataSet} -#' and optionally (if \code{include_pixel=TRUE}) \code{x_pixel} and -#' \code{y_pixel}. +#' @param include_pixel Include the pixel data in the returned data frame. +#' @return A data.frame with colums for `x`, `y`, and `DataSet` and optionally +#' (if `include_pixel=TRUE`) `x_pixel` and `y_pixel`. #' @export #' @importFrom rjson fromJSON read_wpd <- function(json_str, file, include_pixel=FALSE) { diff --git a/digitizeR.Rproj b/digitizeR.Rproj index 6e4a306..80dcc6d 100644 --- a/digitizeR.Rproj +++ b/digitizeR.Rproj @@ -12,7 +12,9 @@ Encoding: UTF-8 RnwWeave: Sweave LaTeX: pdfLaTeX +StripTrailingWhitespace: Yes + BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source -PackageRoxygenize: rd +PackageRoxygenize: rd,collate,namespace,vignette diff --git a/man/digitizeR.Rd b/man/digitizeR.Rd index cd5f103..8023a73 100644 --- a/man/digitizeR.Rd +++ b/man/digitizeR.Rd @@ -5,19 +5,19 @@ \alias{digitizeR} \title{digitizeR: Tool to extract numerical data from images of plots, maps etc.} \description{ -The digitizeR package provides a web based interface (WebPlotDigitizer) to interactively extract -numerical data from images of plots, maps, microscope images etc. Some tools to make simple distance -and angle measurements are also available. In the future, this package will also allow -real-time interaction between R and the hosted web application via WebSockets. +The digitizeR package provides a web based interface (WebPlotDigitizer) to +interactively extract numerical data from images of plots, maps, microscope +images etc. Some tools to make simple distance and angle measurements are +also available. In the future, this package will also allow real-time +interaction between R and the hosted web application via WebSockets. } \section{Available Functions}{ - \itemize{ - \item \code{\link{wpd_launch}}: Start HTTP server that hosts WebPlotDigitizer and - open the local URL in the browser. - \item \code{\link{wpd_close}}: Shutdown the HTTP server. - \item \code{\link{wpd_isOpen}}: Check is the HTTP server is currently running. +\item \code{\link[=wpd_launch]{wpd_launch()}}: Start HTTP server that hosts WebPlotDigitizer and +open the local URL in the browser. +\item \code{\link[=wpd_close]{wpd_close()}}: Shutdown the HTTP server. +\item \code{\link[=wpd_isOpen]{wpd_isOpen()}}: Check is the HTTP server is currently running. } } diff --git a/man/read_wpd.Rd b/man/read_wpd.Rd index 1d78573..048eda9 100644 --- a/man/read_wpd.Rd +++ b/man/read_wpd.Rd @@ -11,12 +11,11 @@ read_wpd(json_str, file, include_pixel = FALSE) \item{file}{the name of a file to read the json_str from; this can also be a URL. Only one of json_str or file must be supplied.} -\item{include_pixel}{Include the pixel data in the returned data frame} +\item{include_pixel}{Include the pixel data in the returned data frame.} } \value{ -A data.frame with colums for \code{x}, \code{y}, and \code{DataSet} - and optionally (if \code{include_pixel=TRUE}) \code{x_pixel} and - \code{y_pixel}. +A data.frame with colums for \code{x}, \code{y}, and \code{DataSet} and optionally +(if \code{include_pixel=TRUE}) \code{x_pixel} and \code{y_pixel}. } \description{ Read WebPlotDigitizer JSON data diff --git a/man/wpd_launch.Rd b/man/wpd_launch.Rd index 5948ef9..785796f 100644 --- a/man/wpd_launch.Rd +++ b/man/wpd_launch.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/digitizeR.R \name{wpd_launch} \alias{wpd_launch} -\title{Start a local HTTP server that hosts WebPlotDigitizer. +\title{Start a local HTTP server that hosts WebPlotDigitizer. This will also open a browser window pointing to the local URL.} \usage{ wpd_launch(location = "0.0.0.0", port = 8000) @@ -16,7 +16,7 @@ wpd_launch(location = "0.0.0.0", port = 8000) Server handle that is later used to shutdown the server using wpd_close() } \description{ -Start a local HTTP server that hosts WebPlotDigitizer. +Start a local HTTP server that hosts WebPlotDigitizer. This will also open a browser window pointing to the local URL. } \examples{