ropensci / qualtRics

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

survey_questions() not showing labels #259

Closed asadow closed 1 year ago

asadow commented 2 years ago

The function documentation mentions that variable labels are stored as attributes. How can I access them?

library(qualtRics)
df_qualtrics <- all_surveys()
sq <- survey_questions(df_qualtrics$id[2])
sq
#> # A tibble: 32 × 4
#>    qid     qname question                                             force_resp
#>    <chr>   <chr> <chr>                                                <lgl>     
#>  1 QID1008 Q2    "Logo"                                               FALSE     
#>  2 QID1004 Q1    "<div>\n<div style=\"text-align: center;\"><span st… FALSE     
#>  3 QID1005 Q3    "Your Family Code is ${m://ExternalDataReference}"   FALSE     
#>  4 QID4    Q4    "Please confirm how many children do you have in to… TRUE      
#>  5 QID101  Q5    "Please confirm how many children you have enrolled… TRUE      
#>  6 QID1537 Q6    "What is the first name of your<span style=\"font-s… TRUE      
#>  7 QID19   Q7    "How are you related to ${q://QID1537/ChoiceTextEnt… TRUE      
#>  8 QID1591 Q8    "Below we ask questions about your family's behavio… TRUE      
#>  9 QID1593 Q9    "Does ${q://QID1537/ChoiceTextEntryValue/2} have an… TRUE      
#> 10 QID1594 Q10   "b) Videogame system (ex. Xbox, PlayStation) "       TRUE      
#> # … with 22 more rows
attributes(sq)
#> $class
#> [1] "tbl_df"     "tbl"        "data.frame"
#> 
#> $row.names
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#> [26] 26 27 28 29 30 31 32
#> 
#> $names
#> [1] "qid"        "qname"      "question"   "force_resp"

Created on 2022-04-27 by the reprex package (v2.0.1)

juliasilge commented 2 years ago

I don't believe that survey_questions() says anything about variable labels as an attribute, right?

You can get it from fetch_survey():

library(qualtRics)
my_survey <- fetch_survey("SV_3gxxxx", add_column_map = TRUE, force_request = TRUE)
#>   |                                                                              |                                                                      |   0%  |                                                                              |======================================================================| 100%
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   .default = col_character(),
#>   StartDate = col_datetime(format = ""),
#>   EndDate = col_datetime(format = ""),
#>   Progress = col_double(),
#>   `Duration (in seconds)` = col_double(),
#>   Finished = col_logical(),
#>   RecordedDate = col_datetime(format = ""),
#>   RecipientLastName = col_logical(),
#>   RecipientFirstName = col_logical(),
#>   RecipientEmail = col_logical(),
#>   ExternalReference = col_logical(),
#>   LocationLatitude = col_double(),
#>   LocationLongitude = col_double(),
#>   SolutionRevision = col_double()
#> )
#> ℹ Use `spec()` for the full column specifications.
attr(my_survey, "column_map")
#> # A tibble: 24 × 7
#>    qname                 description      main  sub   ImportId timeZone choiceId
#>    <chr>                 <chr>            <chr> <chr> <chr>    <chr>    <lgl>   
#>  1 StartDate             Start Date       Star… ""    startDa… Z        NA      
#>  2 EndDate               End Date         End … ""    endDate  Z        NA      
#>  3 Status                Response Type    Resp… ""    status   <NA>     NA      
#>  4 IPAddress             IP Address       IP A… ""    ipAddre… <NA>     NA      
#>  5 Progress              Progress         Prog… ""    progress <NA>     NA      
#>  6 Duration (in seconds) Duration (in se… Dura… ""    duration <NA>     NA      
#>  7 Finished              Finished         Fini… ""    finished <NA>     NA      
#>  8 RecordedDate          Recorded Date    Reco… ""    recorde… Z        NA      
#>  9 ResponseId            Response ID      Resp… ""    _record… <NA>     NA      
#> 10 RecipientLastName     Recipient Last … Reci… ""    recipie… <NA>     NA      
#> # … with 14 more rows

Created on 2022-04-27 by the reprex package (v2.0.1)

asadow commented 2 years ago

Right, but it says question labels, which has a different definition for Qualtrics @ https://www.qualtrics.com/support/survey-platform/survey-module/editing-questions/formatting-questions/

So what do you mean by question labels?

Thanks for your continued help.

juliasilge commented 2 years ago

Ah OK, that is not something I am as familiar with. It looks like this is returned in the API call made for survey_questions but we are not including it here:

https://github.com/ropensci/qualtRics/blob/e350dd3e4d358e4b9b3f248ab35f55705dd31ec7/R/survey_questions.R#L52-L56

We would need to add something like qlabel = purrr::map_chr(qi, "questionLabel") there but this doesn't work when it is all NULL (like for all my surveys).

Are you interested in working on this and contributing a PR?

jmobrien commented 2 years ago

@adamofthegarden FYI survey_questions() is likely to be retired or at least fundamentally changed at whatever point the move to the v3 endpoint under #267 occurs. I'm personally super grateful for all the work people do on this package but I feel like that's worth mentioning so that people don't end up investing time in something that might be about to be undone.