Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Update style in documentation #11

Open
wants to merge 10 commits into
base: master
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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ Imports:
mime,
rjson,
Rook
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
73 changes: 37 additions & 36 deletions R/app.R
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#' @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,
Expand All @@ -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)
Expand All @@ -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)
}

55 changes: 28 additions & 27 deletions R/digitizeR.R
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#' 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()
Expand All @@ -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') {
Expand All @@ -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
Expand All @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions R/read_wpd.R
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion digitizeR.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions man/digitizeR.Rd

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

7 changes: 3 additions & 4 deletions man/read_wpd.Rd

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

4 changes: 2 additions & 2 deletions man/wpd_launch.Rd

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