9  Common Tasks

9.1 Install dependencies

Install the last versions of all the packages:

if (!require('pak')) install.packages('pak'); pak::pkg_install("gorkang/jsPsychAdmin")
if (!require('pak')) install.packages('pak'); pak::pkg_install("gorkang/jsPsychMaker")
if (!require('pak')) install.packages('pak'); pak::pkg_install("gorkang/jsPsychMonkeys")
if (!require('pak')) install.packages('pak'); pak::pkg_install("gorkang/jsPsychHelpeR")

9.2 Developing a protocol

9.2.1 Adding new tasks

Copy example tasks to a local folder, and adapt with the information of the new task. For example:

jsPsychMaker::copy_example_tasks(
  destination_folder = "~/Downloads/ExampleTasks", 
  which_tasks = "MultiChoice")

9.2.2 Creating a protocol

You can choose any of the canonical tasks (jsPsychMaker::list_available_tasks()$tasks), and/or a folder with the new tasks (~/Downloads/ExampleTasks).

jsPsychMaker::create_protocol(
  canonical_tasks = c("BNT"), # Berlin Numeracy Test
  folder_tasks = "~/Downloads/ExampleTasks/",
  folder_output = "~/Downloads/protocol9996",
  launch_browser = TRUE
)

9.2.3 Piloting the protocol

Use local_folder_tasks to test local protocols.

jsPsychMonkeys::release_the_monkeys(
  uid = 1:10,
  sequential_parallel = "parallel",
  number_of_cores = 10,
  local_folder_tasks = "~/Downloads/protocol9996/",
  open_VNC = FALSE
)

If your protocol is on a server, use server_folder_tasks. You will need a credentials_folder with the server access credentials.

jsPsychMonkeys::release_the_monkeys(
  uid = "1:10",
  sequential_parallel = "parallel",
  number_of_cores = 10,
  server_folder_tasks = "test/protocols_DEV/999",
  DEBUG = FALSE,
  credentials_folder = "~/my_location/.vault/", 
  open_VNC = FALSE
  )

9.2.4 Creating helper project

If it is a local protocol:

jsPsychHelpeR::run_initial_setup(
  pid = 9996,
  data_location = "~/Downloads/protocol9996/.data/",
  folder = "~/Downloads/jsPsychHelpeR9996", 
  dont_ask = TRUE
  )

If it is an online protocol:

jsPsychHelpeR::run_initial_setup(
  pid = 9996,
  download_files = TRUE,
  credentials_file = "/path_to_credentials/.credentials",
  folder = "~/Downloads/jsPsychHelpeR9996", 
  dont_ask = TRUE
  )

9.2.5 Deleting pilot data

    # Delete the XYZ protocol rows in all the MYSQL tables
    source("admin/mysql_helper_functions.R")
    # list_credentials = decrypt_data(key_public = readLines(".vault/data_public_key.txt"), data_encrypted = ".vault/data_encrypted.rds", mysupersecretpassword = rstudioapi::askForPassword())
    delete_MySQL_tables_pid(pid)
    
  
  # 3) Limpiar los archivos de resultados de Monkeys -----------------------
    
    # Delete csv files in .data/
    # rstudioapi::navigateToFile(".vault/.credentials") 
    DELETE_data_server(pid = PROTOCOLID)

9.2.6 Preparing Helper project

9.2.6.1 Get data

Will download a zip file with the data.

# if (!require('renv')) utils::install.packages('renv'); renv::install('gorkang/jsPsychHelpeR')

# Developing protocol
jsPsychHelpeR::get_zip(pid = "test/protocols_DEV/31", 
                       what = "data", 
                       where = "data/")

# Production protocol
jsPsychHelpeR::get_zip(pid = "999",
                       what = "data", 
                       where = "data/")

9.3 Protocol to production

From admin/000_PREPARE_protocol_for_production.R in jsPsychAdmin

# Checklist para pasar protocolos de test/protocols_DEV/ a produccion

  # PARAMETERS --------------------------------

  PROTOCOLID = "test/protocols_DEV/31"
  number_of_monkeys = "1:100"

  # -------------------------------------------

  # Automatic parameters
  pid = gsub("test/protocols_DEV/", "", PROTOCOLID)


   cli::cli_h1("PROTOCOL {pid}")


  # 1) Pilotaje final on Monkeys! -------------------------------------------

  # Clean data and MySQL DB
  # rstudioapi::navigateToFile(".vault/.credentials")
  jsPsychAdmin::clean_up_dev_protocol(protocol_id = PROTOCOLID) # Will ask for server password


  # LAUNCH MONKEYS
  jsPsychMonkeys::release_the_monkeys(uid = number_of_monkeys,
                                      server_folder_tasks = PROTOCOLID,
                                      sequential_parallel = "parallel",
                                      number_of_cores = 10,
                                      big_container = TRUE,
                                      keep_alive = FALSE, 
                                      open_VNC = FALSE, 
                                      screenshot = FALSE,
                                      credentials_folder = here::here(".vault/"))


  # CHECK jsPsychHelpeR runs OK

  # || THIS WILL take a while, as all the renv packages need to update
  # Create NEW jsPsychHelpeR project, downloading the files from the server
  jsPsychHelpeR::run_initial_setup(pid = PROTOCOLID,
                                   download_files = TRUE,
                                   folder = "~/Downloads/jsPsychR_TESTING_for_PRODUCTION")

    # REMEMBER TO DO targets::tar_make() in jsPsychHelpeR project!


  # 2) Clean data and  MySQL DB --------------------------------------------

    # rstudioapi::navigateToFile(".vault/.credentials")
    jsPsychAdmin::clean_up_dev_protocol(protocol_id = pid) # Will ask for server password


  # 3) Revisar el config.js para pasar el experiment a produccion ----------

    # -[] online = true
    # -[] pid OK?
    # -[] debug_mode = false
    # - ETC...


  # 4) Copiar protocolo ZIPeado a test/protocols_DEV/OLD_TESTS/ -------------

    # TODO: automatico!

  # 5) Copiar protocolo a protocols/ ----------------------------------------

    # TODO: automatico!

  # 6) BORRAR protocolo de test/protocols_DEV/OLD_TESTS/ --------------------

    # TODO: automatico!