shinyApps hosting

This demonstrates explains how to deploy a mirtCAT GUI on shinyApps.io, and saving the results to a local Dropbox folder through the rdrop2 package.

Configurations

Before getting started, installation of additional packages are required. Run the following code to install the required packages:

install.packages("rdrop2")

Follow the instructions for rdrop2 to obtain a .rdata file with your Dropbox credentials. As well, you’ll need to set up an account with shinyApps.io, and the following tutorial will help to get you started. For a general idea how how to save results when using shinyApps.io, as well as an introduction to other methods of exporting data, see this link.

Be sure to test that shinyApps.io is working for you by hosting a test application consisting of a server.R and ui.R file, and test that the Dropbox credentials allow you to upload R objects to your Dropbox.

mirtCAT files

The standard mirtCAT() function cannot be run on shinyApps.io, therefore the process must be broken into two separate functions and placed in an external source file (i.e., no server.R or ui.R files are required). The two functions are: mirtCAT_preamble(), which is essentially equivalent to mirtCAT() and accepts all the arguments, and createShinyApp() which generates the required GUI. Three things are important here:

  • To run a mirtCAt GUI, one file must be submitted called app.R, containing all the instructions to deploy the GUI
  • mirtCAT_preamble() must be run before createShinyApp() to ensure that the GUI is properly set up, and
  • createShinyApp() must be the last function called in app.R. Implicitly shinyApps.io will wrap this function in a runApp() function, so you don’t have to.

Additional files may be uploaded, such as a questions.csv file or other files, and can be imported in the app.R file.

Setup

Before you can upload R objects to your Dropbox folder you must first setup Dropbox to be accessed. After doing this you can save your token to a file and read this in later once it has been uploaded to the shinyApp.io server.

token <- drop_auth()
saveRDS(token, "dropbox_token.rds") # Upload this saved droptoken to the server

Example

The following defines a simple survey and stores the results to a local Dropbox folder with the save_fun() definition which is passed to the final_fun argument. This function is the last function called before the interface is terminated, and therefore will save the results when shinyApps.io is finalized. The following saves the person objects to a suitable Dropbox directory called responses/.

#######################################
# example 'app.R' file for shinyApps.io
#######################################

# Load the required packages
library(rdrop2)
library(mirtCAT)

drop_auth(rdstoken = "dropbox_token.rds") # authenticate the token
token <- readRDS("dropbox_token.rds") #read in dropbox token
drop_acc(dtoken = token) #load token globally

# create 'responses' directy to store objects to (can be done manually)
# drop_create("responses", dtoken = token)

# function to upload object with corresponding filename 
#    (.. args passed to rdrop2::drop_upload)
drop_person_upload <- function(obj, file = "person.rds", ...){
    saveRDS(obj, file = file)
    drop_upload(file, ...)
    file.remove(file)
    invisible()
}

#-----------------------------------------
## mirtCAT definitions

options(stringsAsFactors = FALSE)

# simple interface, no stems
options <- matrix(c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"),
                  nrow = 3, ncol = 5, byrow = TRUE)
questions <- c("Building CATs with mirtCAT is difficult.",
               "mirtCAT requires a substantial amount of coding.",
               "I would use mirtCAT in my research.")

df <- data.frame(Question = questions, Option = options, Type = "radio")

#-----------------------------------------
# helper function

# function to save to my authenticated dropbox folder
final_fun <- function(person){
    drop_person_upload(person, path = 'responses')
    invisible()
}

#-----------------------------------------
# run the interface

mirtCAT_preamble(df = df, shinyGUI = list(forced_choice = FALSE),
                 final_fun = final_fun)
createShinyGUI()

In the example above, two files are inintially uploaded to shinyApps.io:

  • The app.R file, containing the above instructions, and
  • An R binary file called 'dropbox_token.rds', containing the object Dropbox credentials for authenticating the personal Dropbox folder

After running the shiny session an object called 'person.rds' is stored to the Dropbox folder responses/ containing all the person information, which can later be read back into R via person <- readRDS('person.rds'). Modifying the arguments in mirtCAT_preamble() will change how the session is organized, perhaps changed to an adaptive session if a mo object is included, and the GUI can be tested locally by using a call to runApp(createShinyGUI()).