wfu-dmds / teaching-r-study

3 stars 2 forks source link

Modularizing Demographic Form #2

Closed jdtrat closed 3 years ago

jdtrat commented 4 years ago

Hi @LucyMcGowan,

I thought I would create a separate issue dedicated to getting the demographic questions as shiny inputs. I started messing around with the PDF you gave me, and although this is a specific case, I'm trying to find the best way to format the data so I can pass them into Shiny widgets with functional programming. In the example below, for instance, if type == "MC", the radioButtons input would be created. If type == "check" then selectInput would be.

What I'm unsure of is how to best format the data so each (non-free response) question has all of the possible answers with it. So if the question is "What is the highest level of education you have completed?" then selectInput will have X many selections available.

I'm thinking of a nested tibble perhaps, or a dataframe that utilizes the fill function. Do you have any suggestions? I'm happy to clarify something if I was unclear.

library(textreadr)
library(tidyverse)
library(tidytext)
textreadr::read_pdf('~/Downloads/assessment.pdf') %>% 
  unnest_tokens(output = questions, 
                input = text, 
                token = "lines", 
                to_lower = FALSE) %>% 
  select(questions) %>% 
  # remove first two rows -- title stuff
  filter(!str_detect(questions, "Pre|Dem")) %>%
  # add a category column that is "question" if there is a number 
  # followed by a period signifying a new question and "options" otherwise.
  mutate(category = case_when(str_detect(questions, "[:digit:](?=\\.)") ~ "question",
                              TRUE ~ "options"),
         type = case_when(category == "options" & str_detect(questions, "\\[") ~ "MC",
                          category == "options" & !str_detect(questions, "\\[") ~ "check",
                          TRUE ~ "NA"))
#> Table: [28 x 3]
#> 
#>    questions                                category type 
#> 1      1. What is your age?                 question NA   
#> 2      2. Which best describes your gender? question NA   
#> 3      3. What is the highest level of educ question NA   
#> 4  [ ] Did not attend high school [ ] Some  options  MC   
#> 5  graduate [ ] Some postgraduate work [ ]  options  MC   
#> 6      4. What was your first language?     question NA   
#> 7  Arabic Armenian Chinese English French F options  check
#> 8  Korean Persian Polish Portuguese Russian options  check
#> 9      5. In what language do you read most question NA   
#> 10        German Greek Gujarati Hindi Itali options  check
#> .. ...                                      ...      ...

Created on 2020-09-10 by the reprex package (v0.3.0)

jdtrat commented 3 years ago

Made a lot of progress since this was opened. No longer applicable.