ropensci / qualtRics

Download ⬇️ Qualtrics survey data directly into R!
https://docs.ropensci.org/qualtRics
Other
215 stars 70 forks source link

fetch_survey and save_dir #314

Closed zaslove closed 1 year ago

zaslove commented 1 year ago

Hallo, I have question. I was looking through the documentation and online vignettes but could not find an answer.

If I use the fetch_survey function to save my survey locally, is there a way to give the survey a name. With one or two surveys this is not an issue but we have some 30 surveys, thus it would be nice to be able to give it an name when saving it locally.

mysurvey <- fetch_survey(surveyID = surveys$id[6], save_dir = "/users/Julia/Desktop/", verbose = TRUE)

best,

Andrej

jmobrien commented 1 year ago

Andrej,

Thanks for this thought. This is actually a good idea given the feature, but not something that had really been considered b/c save_dir has been primarily used for caching downloads (to minimize # of repeated API calls), rather than for saving them for personal use in the typical sense. That said, given the possibility of saving for other purposes, I'm realizing the docs aren't clear enough. We should fix that.

Now, for various reasons, caching itself is likely be removed soon-ish (#300), so it's unclear whether we'll keep integrated saving features inside fetch_survey() or not.

My suggestion for you, then, would be using saving tools from the "tidyverse" package readr. These all return the saved object after saving, so you can do what you need using simple piped structures like the examples below.

If you just want to save a returned response data frame as a .csv:

mysurvey <-
   fetch_survey(surveyID = "[yoursurveyID]") |>
      readr::write_csv(file = "[path]/[filename]")

If you want to save the returned data frame as .rds files (like our caching feature does, preserves some extra attributes like any attached column maps):

mysurvey <- 
   fetch_survey(surveyID = "[yoursurveyID]") |>
      readr::write_rds(file = "[path]/[filename]")

You can also vectorize to get all of your downloads at once saved both externally & in a list. In this example, we add all downloads back to a single dataframe as a new list-column:

surveys <-
   data.frame( 
     id = <vector of survey ids>,
     file = <vector of matching file names, w/paths if needed> 
  )

surveys$responses <-  
  # Calls the API to download each surveyID in turn:
  purrr::map(surveys$id, fetch_survey) |>    
  # Writes each to specified path & file, then returns list(-column for dataframe)
  purrr::map2(surveys$file, write_rds)          

@juliasilge, maybe we should improve the docs to make clear what save_dir is for, and figure out its role once #300 is addressed.

Also, thinking I might stick some or all of the above in the fetch_survey() example code. What's there already implies the use of a data frame containing survey IDs, so this would make that potential while maybe also helping users not get confused by our current setup re: saving.

zaslove commented 1 year ago

Hallo, Many thanks for this! This clear things up and also gives me nice options to work with. I very much appreciate it!

best,

Andrej

juliasilge commented 1 year ago

I am realizing that we should at least make it more clear what fetch_survey() returns (currently no @return section) so folks have a sense of what they can do with it.

My inclination is still that we should do away with the current caching implementation, since its behavior is surprising to many users, and as part of that deprecate the save_dir argument. My opinion is that this would be better than trying to fix save_dir:

Also, thinking I might stick some or all of the above in the fetch_survey() example code.

It wouldn't have to go into the fetch_survey() examples but instead could go to the main vignette, if it gets lengthy.

zaslove commented 1 year ago

If you put the script in the vignette that would be great, I think. It was through the vignette that I started to wonder about saving to a folder.