djnavarro / xprmntr

Build browser-based behavioural experiments in R
https://djnavarro.github.io/xprmntr/
Other
36 stars 3 forks source link

improving run_locally() #4

Open CrumpLab opened 5 years ago

CrumpLab commented 5 years ago

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.

CrumpLab commented 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:

  1. option to run the experiment in the viewer pane, or directly in a browser
  2. options to set the host and port defaults, and print them as a message.

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)

}