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, 9, 7.2, 9.7, 7.01, 1, 7.5, 7.51,
9.7, 16.5, 7.51, 14.5, 14.2, 11.51, 2.5, 8.5, 14.01, 14.2, 9,
14.2, 14, 12, 3, 9.51, 7.51, 14.01, 10.5, 7, 14.01, 9.5, 5, 8.2,
3.5, 16.5, 4, 12.5, 9.01, 14.2, 7.21, 10, 5.01, 6, 13.5, 8.2,
15, 18.5, 9, 14, 7.01, 7.4, 6, 14, 11.7, 7.7, 5.2, 12.5, 18.5,
9.51, 0.4, 3.7, 1.5, 1, 7.4, 8.5, 16.5, 14.5, 4.9, 10.5, 9.2,
10.5, 1.2, 2.9, 7.51, 5.51, 10.5, 9.5, 14, 21, 15, 11.7, 3.7,
3.01, 0.71, 7.2, 1.01, 5.5, 7.21, 7.51, 14.2, 14, 8.2, 7.5, 16,
15, 3.5, 10.5, 9.5, 9, 7.01, 10, 0.51, 7.7, 0.2, 9.7, 7.5, 14.01,
11.7, 0.71, 15, 7.02, 14.2, 11.51, 7.02, 5.2, 5, 9.5, 14.01,
7.51, 0.7, 11.7, 0.7, 10.5, 5, 10.5, 9, 10.5, 7.01, 9.51, 8.01,
8, 9.7, 8, 7.5, 4.7, 14.5, 11.5, 2, 7, 7.21, 9.5, 14.2, 2.51,
3.5, 9.5, 7.7, 7.01, 8.01, 12.5, 1.5, 8.5, 14.01, 8, 7.51, 8.2,
14.2, 7.2, 9.5, 14.01, 3.5, 10, 3.5, 7.7, 2.51, 16, 9.5, 0.71,
8, 7.21, 4.9, 14.01, 1.01, 2.51, 4, 8.01, 2, 9.7, 10.5, 16, 7.51,
3.2, 9.51, 7.7, 2.51, 11.5, 2.5, 0.6, 14, 8.2, 3, 7.21, 8.01,
0.71, 8.5, 4.5, 9.7, 11.5, 3.2, 0.5, 7.02, 10.5, 11.51, 11.5,
9.7, 4.51, 15, 1.01, 7.21, 3.01, 10, 7.51, 11.7, 7.5, 12, 7,
2.51, 2.51, 18.5, 7.01, 8.5, 3.51, 9.7, 8.2, 7, 8, 2.52, 14,
8.01, 11.51, 7.5, 5, 5, 1.01, 14.01, 8.01, 8, 16.5, 21, 7.2,
7.2, 9.51, 9.51, 14.01, 10, 7.01, 8.5, 12, 14, 14, 7.21, 5, 8.01,
2.01, 10.5, 9.5, 7.21, 7.2, 9.7, 9, 7.51, 11.5, 7.2, 7.01, 9,
7.02, 2.5, 2.51, 9.7, 1.02, 1.21, 14.01, 6, 14.2, 9.51, 7.21,
10, 7.2, 3.5, 7.01, 9.01, 9.51, 9.5, 8.5, 2.51, 16.5, 7.5, 15,
12, 8, 1, 8, 8.5, 14.01, 18.5, 0.7, 8, 0.2, 9.5, 13.5, 3, 9.51,
14, 7, 12.5, 0.71)), class = "data.frame", row.names = c(NA,
327L), questiontype = "NumberMulti", question = "Q4.  Frequency numeric"),
    `Q3. Age in years` = structure(c(27, 47, 27, 27, 60, 60,
    52, 37, 70, 47, 47, 60, 60, 33, 70, 27, 33, 52, 37, 22, 52,
    47, 60, 52, 33, 27, 37, 42, 27, 60, 52, 60, 33, 22, 60, 33,
    60, 47, 22, 42, 60, 47, 60, 42, 47, 27, 37, 42, 42, 47, 22,
    60, 37, 27, 42, 33, 60, 33, 70, 22, 37, 42, 22, 47, 22, 33,
    70, 37, 27, 37, 37, 22, 33, 42, 47, 52, 42, 60, 60, 70, 52,
    33, 37, 22, 27, 22, 52, 60, 33, 22, 22, 70, 47, 33, 22, 52,
    60, 60, 37, 60, 22, 22, 47, 37, 22, 33, 22, 22, 33, 70, 52,
    47, 37, 42, 42, 37, 22, 42, 60, 52, 52, 37, 27, 42, 27, 37,
    33, 70, 27, 22, 22, 70, 33, 22, 52, 42, 60, 52, 60, 60, 33,
    60, 22, 22, 33, 70, 42, 22, 42, 22, 22, 70, 37, 47, 37, 22,
    52, 42, 22, 22, 33, 27, 27, 27, 27, 33, 33, 47, 42, 37, 52,
    60, 52, 52, 52, 60, 42, 37, 27, 27, 42, 22, 37, 60, 42, 37,
    42, 27, 27, 27, 60, 27, 60, 27, 22, 22, 47, 33, 42, 60, 22,
    70, 27, 52, 60, 33, 47, 52, 33, 60, 60, 60, 22, 27, 42, 52,
    70, 47, 27, 27, 33, 37, 27, 70, 70, 42, 42, 70, 22, 70, 22,
    52, 42, 60, 22, 60, 60, 52, 60, 37, 60, 27, 60, 22, 37, 27,
    70, 42, 47, 52, 60, 37, 22, 27, 42, 52, 42, 37, 42, 52, 60,
    37, 22, 60, 27, 37, 52, 70, 27, 47, 47, 52, 42, 60, 47, 47,
    37, 52, 47, 52, 60, 22, 27, 70, 47, 52, 33, 70, 60, 70, 27,
    33, 60, 33, 33, 60, 60, 52, 42, 37, 47, 60, 37, 42, 37, 27,
    52, 42, 47, 37, 52, 22, 27, 60, 37, 52, 52, 52, 37, 27, 33,
    33, 52, 33, 22, 33, 52),
    questiontype = "Number", name = "Q3_3", label = "Q3. Age in years",
    question = "Q3. Age in years"))

findInstDirFile <- function(file)
{
    file.path(system.file("testdata", package = "flipData", mustWork = TRUE),
              file)
}

test_that("Mixed question and variable",
{
    result <- SplitFormQuestions(form.data)
    expect_equal(names(result),
                 c("Colas (e.g., Coca Cola, Pepsi Max)?",
                   "Sparkling mineral water",
                   "Coffee", "Q3. Age in years"))
    expect_warning(result <- SplitFormQuestions(form.data,
                                                show.labels = FALSE),
        paste0("Variable names cannot be shown when questions are selected. ",
               "To show variable names, please select the variables from the ",
               "question."))
    expect_equal(names(result),
                 c("Colas (e.g., Coca Cola, Pepsi Max)?",
                   "Sparkling mineral water",
                   "Coffee", "Q3_3"))


})

test_that("Pick Any - Grid",
{
    load(findInstDirFile("Q5.rda"))
    result <- SplitFormQuestions(list(Q5 = Q5), include.grid.flag = TRUE)
    expect_equal(names(result$dat),
                 c("Feminine, Coke", "Feminine, Diet Coke", "Feminine, Coke Zero",
                   "Feminine, Pepsi", "Feminine, Diet Pepsi", "Feminine, Pepsi Max",
                   "Feminine, None of these", "Health-conscious, Coke", "Health-conscious, Diet Coke",
                   "Health-conscious, Coke Zero", "Health-conscious, Pepsi", "Health-conscious, Diet Pepsi",
                   "Health-conscious, Pepsi Max", "Health-conscious, None of these",
                   "Innocent, Coke", "Innocent, Diet Coke", "Innocent, Coke Zero",
                   "Innocent, Pepsi", "Innocent, Diet Pepsi", "Innocent, Pepsi Max",
                   "Innocent, None of these", "Older, Coke", "Older, Diet Coke",
                   "Older, Coke Zero", "Older, Pepsi", "Older, Diet Pepsi", "Older, Pepsi Max",
                   "Older, None of these", "Open to new experiences, Coke", "Open to new experiences, Diet Coke",
                   "Open to new experiences, Coke Zero", "Open to new experiences, Pepsi",
                   "Open to new experiences, Diet Pepsi", "Open to new experiences, Pepsi Max",
                   "Open to new experiences, None of these", "Rebellious, Coke",
                   "Rebellious, Diet Coke", "Rebellious, Coke Zero", "Rebellious, Pepsi",
                   "Rebellious, Diet Pepsi", "Rebellious, Pepsi Max", "Rebellious, None of these",
                   "Sleepy, Coke", "Sleepy, Diet Coke", "Sleepy, Coke Zero", "Sleepy, Pepsi",
                   "Sleepy, Diet Pepsi", "Sleepy, Pepsi Max", "Sleepy, None of these",
                   "Traditional, Coke", "Traditional, Diet Coke", "Traditional, Coke Zero",
                   "Traditional, Pepsi", "Traditional, Diet Pepsi", "Traditional, Pepsi Max",
                   "Traditional, None of these", "Weight-conscious, Coke", "Weight-conscious, Diet Coke",
                   "Weight-conscious, Coke Zero", "Weight-conscious, Pepsi", "Weight-conscious, Diet Pepsi",
                   "Weight-conscious, Pepsi Max", "Weight-conscious, None of these"))

    expect_equal(result$is.grid, rep(TRUE, 63))

    result.2 <- MatchVariableLabelsToQuestion(names(result$dat),
                                            c("feminine] Diet Coke",
                                              "feminine] Coke Zero"),
                                            result$is.grid, "Must include")
    expect_equal(result.2, c(2, 3))

    expect_error(MatchVariableLabelsToQuestion(names(result$dat),
                                              "incorrect_variable_name",
                                              result$is.grid, "Must include"),
                 paste0("The following variable was specified in 'Must include' ",
                        "but could not be matched to those in the list of ",
                        "alternatives: 'incorrect_variable_name'. Either this ",
                        "variable is not present in the set of alternatives, ",
                        "or the variable label does not match any category ",
                        "from the input questions in which case the variables ",
                        "from the question should be selected as alternatives ",
                        "instead."))
})

test_that("form.data with no names; DS-2601",
{
    fd <- unname(form.data)
    result <- SplitFormQuestions(fd)
    expect_equal(names(result),
                 c("Colas (e.g., Coca Cola, Pepsi Max)?",
                   "Sparkling mineral water",
                   "Coffee", "Q3. Age in years"))
})

test_that("Throw error with duplicate names",
{
    fd <- form.data
    attr(fd[[2]], "label") <- "Coffee"
    expect_error(SplitFormQuestions(fd),
                paste0("The data cannot have two columns with same name: ",
                       "'Coffee'. Modify the inputs to avoid this."))
})
NumbersInternational/flipData documentation built on March 2, 2024, 10:52 a.m.