Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improving run_locally() #4

Open
CrumpLab opened this issue Aug 7, 2019 · 1 comment
Open

improving run_locally() #4

CrumpLab opened this issue Aug 7, 2019 · 1 comment

Comments

@CrumpLab
Copy link

CrumpLab commented Aug 7, 2019

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
Copy link
Author

CrumpLab commented Sep 3, 2019

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)

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant