''
# Load packages
library(surveymonkey)
library(tidyverse)
library(janitor)
library(kableExtra)
library(clipr)
library(omni)

Survey Monkeys

Lots of OMNI projects use Survey Monkey for data collection. OMNI is trying to migrate to R for data analysis and reporting. Regardless of this migration effort, tools that help connect R to Survey Monkey could be useful.

A web search revealed a few potential options for downloading data from Survey Monkey to R.

In this guide we are going to use the package surveymonkey.

Package surveymonkey is simple and straight forward. It is not yet on CRAN so we will download it from the developer’s Github account.

Steps

First copy and paste this code into your console to download the surveymonkey package to your machine.

devtools::install_github("mattroumaya/surveymonkey")

The steps for linking your R account to Survey Monkey are on the github page for the author of survey monkey, which you can see here.

In brief:

  • Get an OAuth token

  • to do this you need to access Remark using the typical OMNI process for accessing Remark and Survey Monkey from a remote computer, here.

  • once you are connected to REMARK, open a new tab in edge and go to https://developer.surveymonkey.com/apps. Create a new app and set it to private. Got to settings and enable: View Surveys, View Collectors, View Contacts, View Responses, View Response Details - others could be added as needed.

  • do not ‘deploy’ the app, just create it. You are only doing this for the token

  • Look at the settings page for the app and find the “Access Token” - this will be a very long character string

  • Run this code in R:

usethis::edit_r_profile()
  • add a line like this:

options(sm_oauth_token = “kmda332fkdlakfld8am7ml3dafka-dafknalkfmalkfad-THIS IS NOT THE REAL KEY THOUGH, use the one you just copied from the app you made in survey monkey”)

  • Save and close the .Rprofile

  • Restart R (click on session, above, then Restart R)

  • check to make sure that this worked - type: getOption("sm_oauth_token") and you should see the character string you pasted in to your R users profile.

That’s it! You do that one time at the start of the a project. After a period of time, like 90 days or so, the token will expire and you will have to do this again.

Now, find the surveys you want to download into R.

This will create a data.frame that lists 100 of the surveys on the OMNI account. You can change the number displayed by changing the number 100 to whatever you need.

surveys <- browse_surveys(100) # you probably don't need to see 1000 of them, change as needed
# View(surveys)

Use

View(surveys)

to see the surveys and then filter to find the name of the one you want.

Find the survey you want to download and save the number to an object.

# for example this is the ALHBCU survey 
my_survey = 506367492 # <- for example. Your survey number will be different.
# replace the number here with the number of your survey

get the survey (example code from another project):

alhbcu_stu = fetch_survey_obj(my_survey)
# make data frames 
stu_df = parse_survey(alhbcu_stu)
# staff_df = parse_survey(alhbcu_staff)

Now you are up and running.

Streamline the previous few chunks by running this code each time you want to download the survey again.

I would put this code, and the initial steps of data cleaning, in a separate script. Specifically, in a .r file that you can call if needed by running something like:

source("scripts/get_surveymonkey.r")
survey_df <- 506367492 %>%
  fetch_survey_obj() %>%
  parse_survey()

If you want to use setnames to rename columns (highly recommended), then you can use the following to start your table in Excel for renaming:

# rename columns - copy paste to excel and then use data.table's set names 
writeClipboard(colnames(stu_df))