Open CrumpLab opened 5 years ago
I ended up making some additions to run_locally()
on my forked version of this repo. In case these changes are broadly useful, I thought I would add what I did here:
What's new is:
The code adds new documentation for three new params (show_in, xprmntr_host, xprmntr_port), and the new code in the function is between the hashtags.
# file: api_build.R
# author: Danielle Navarro
#' Run a jspsych experiment
#'
#' @param path path
#' @param show_in string, "viewer" to show in viewer, or "browser" to show in browser
#' @param xprmntr_host host defaults to 127.0.0.1
#' @param xprmntr_port port defaults to 8000
#' @export
run_locally <- function(path = ".",
show_in = "viewer",
xprmntr_host = "127.0.0.1",
xprmntr_port = 8000) {
pr <- plumber::plumber$new()
static_site <- file.path(path, "experiment")
data_folder <- file.path(path, "data")
static_router <- plumber::PlumberStatic$new(static_site)
pr$mount("/", static_router)
pr$handle("POST", "/submit", function(req, res){
dat <- jsonlite::fromJSON(req$postBody)
dat <- readr::read_csv(dat$filedata)
tsp <- get_timestamp()
file_id <- paste("data", get_timestamp(), get_alphanumeric(10), sep = "_")
dat$file_id <- file_id
dat <- dat[, c(ncol(dat), 1:ncol(dat)-1), drop = FALSE]
readr::write_csv(dat, file.path(data_folder, paste0(file_id, ".csv")))
})
# add message, and options to display in viewer or browser
message(paste("Point the browser to http://",xprmntr_host,":",xprmntr_port, sep=""))
if(show_in == "viewer"){
viewer <- getOption("viewer")
viewer(paste("http://",xprmntr_host,":",xprmntr_port, sep=""))
}
if(show_in == "browser") utils::browseURL(paste("http://",xprmntr_host,":",xprmntr_port, sep=""))
#
pr$run(swagger = FALSE, host=xprmntr_host, port= xprmntr_port)
}
This is a great function. It took me a while to figure out how to access the server in a web-browser because the function only returned the port number.
Would be nice to figure out how to load the page in the viewer, like what Shiny does. That way it just seems more transparent to the user.