Has anyone used the statistical programming language R to import survey responses with the API? | XM Community
Solved

Has anyone used the statistical programming language R to import survey responses with the API?


Userlevel 1
Hi all,

I have created a blank survey in Qualtrics which represents an external survey which I want to have available in Qualtrics. I have been playing with the POST function in R from the httr package. But I cannot seem to formulate the script to get an accepted status code.

The file itself has been formatted in the same way as the 'example' file was done, as suggested on this page: https://api.qualtrics.com/docs/import-responses

If anyone has done this before in R, I would appreciate a hand.

Kind regards,

Ant
icon

Best answer by Anthony_R_CX 9 July 2018, 12:56

View original

10 replies

Badge +1
Hey Anthony, have you tried https://github.com/ropensci/qualtRics? That package pretty much does it all and has recently been brought under the ropensci umbrella so it will only get better. If you still want help writing the function I can certainly help but it would be easier to just use that package and not reinvent the wheel.
Userlevel 1
Hi RTSullivan, thank you for responding to my question. I have tried the 'qualtRics' package and it is amazing! Unfortunately, the current functionality is focused on downloading response from Qualtrics to R. I need to be able to post information from R into Qualtrics.

I did manage to put together a solution to my question using a combination of features within the httr package. The script below picks up a .csv file from a local directory and posts the responses to a given location.

# identify the path and file of interest
vFilePath <- paste(dir_Output, "/", vFileName, sep = "")

# Load the csv file into the Twitter Analysis Qualtrics survey
vQualtrics_Upload <- POST(url = vPOST_URL,
add_headers('X-API-TOKEN' = vQualtrics_Token),
body = list(surveyId = vQualtrics_SurveyID,
file = upload_file(vFilePath, type = "text/csv")))

# delete the csv file from local directory if successfully loaded into Qualtrics
if(vQualtrics_Upload$status_code == 200) {file.remove(vFileName)}
Badge +1
@Anthony_R_CX what's the use case here? I can't tell what API call your making without seeing your POST_URL variable. I at one time was going to write out functions for posting data and do a PR to the qualtRics repo but I haven't gotten around to it.
Userlevel 1
Hi RTSullivan, I am connecting to Twitter via R, completing some analysis on the Tweets which I receive and then I wanted to load those tweets into a blank Qualtrics survey which I set up. The reason I wanted to load it into Qualtrics was to use the Text IQ functionality. I used the API call at the following link to post the survey responses into my blank Twitter survey. api.qualtrics.com/docs/update-response

I have used a couple of the POST API functions to modify and enhance some information which isn't captured in the survey. It works quite nicely.
@Anthony_R_CX Hello Anthony. I was running into a similar issue using the httr package in R and trying to import a survey into an account based on a local qsf file. The upload_file() function was what was missing in my POST verb. It now works, thanks to your post.

I am however trying to extend the functionality to copy surveys directly from the account rather than importing them. I've been trying a few syntaxes already but failed to make it work.

I was wondering if you had tried that already? Or if you had any clue on how to figure out the right query options with this POST verb.

Happy to raise this as a new discussion thread if considered more appropriate by the community.
Userlevel 1
@Pierre I am glad my previous post was helpful, I have found most of the API calls from R to be a bit fiddly. I am afraid that I have not tried to use the copy survey API call, I just very quickly tried to hack an attempt at it but it didn't seem to work, but I am not 100% why it didn't. You may have already tried this, but my starting place was here:

vCopySurvey <- POST(url = vPOST_URL,
add_headers('X-API-TOKEN' = vQualtrics_Token,
'X-COPY-SOURCE' = vSurveyID,
'X-COPY-DESTINATION-OWNER' = vUserID,
'Content-Type' = 'application/json'))

Sorry I couldn't be more helpful. But I do hope you nail it!

Please let me know if you find the solution, I'd be interested in understanding how it works.
@Anthony_R_CX Thank you for your answer. Yes, I tried exactly the same piece of code and multiple variants of it but nothing worked. I will make sure to report here if finally manage to send this query.

In general, I think there is a good use case to extend the functionalities of the qualtRics package to deal with all API functionalities. If not in their vision, then there is a case for building a new R package...

Best.
I have just put on GitHub a function that can read a survey from Qualtrics

source(
"https://raw.githubusercontent.com/avi-kluger/Qualtrix/master/read_Qualtrics.R")

? read_Qualtrics
Badge
Hi Ant,

Thanks so much for posting this. I've been trying for the past two weeks to use your httr script to import responses into Qualtrics but keep getting a 500 code. If you have a moment, do you see what might be off with this code?

What's weird is that in the headers I keep getting content-type equal to 'application/json' despite specifying the type in the upload_file call. I've also tried using content_type and encode = "form" to get the content-type to be "text/csv" but it doesn't work.

I would sincerely appreciate any help because I've been working on this nonstop but am stuck.

vQualtrics_Upload <- POST(url =
"https://az1.qualtrics.com/API/v3/surveys/SV_5je9vIJAZa7z8Tr/import-responses",
add_headers('X-API-TOKEN' = vQualtrics_Token),
body = list(surveyId = vQualtrics_SurveyID,
file = upload_file("C:/Users/jackman/desktop/API/survey.csv", type = "text/csv")))
Userlevel 1
Badge +1

I know this is awhile back, but I noticed there weren’t any tutorials on YouTube for how to use Qualtrics API and the R “qualtRics” package so I made one.  See link below.  It looks like there’s an equally solid Python module for this as well - so if you’d prefer Python, maybe that’s the way to go.  Finally, I know someone who just used JavaScript and Java to extract their surveys, so it sounds like there’s a ton of possibilities. 

 

 

Leave a Reply