Skip to content

Commit

Permalink
improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kucharssim committed Sep 11, 2023
1 parent e58a541 commit cd4614b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
34 changes: 27 additions & 7 deletions R/column-types.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@
#' @importFrom vctrs vec_ptype2 vec_cast vec_ptype_abbr obj_print_footer
#' @title JASP Column Types
#'
#' @description Columns types in JASP. JASP recognizes 3 main types (Scale, Ordinal, Nominal),
#' with Nominal being further split between basic Nominal and Text.
#' These types roughly correspond to [numeric()], [ordered()], and [factor()].
#' @description Columns types in JASP.
#' @param x object to be coerced or tested.
#' @param values set of possible values (similar to `levels` in [factor()]).
#' @param labels set of labels of the values (similar to `labels` in [factor()]).
#' @param ... not used.
#' @param dataset Data frame or tibble that contains data.
#'
#' @details
#' JASP recognizes 3 main data types (Scale, Ordinal, Nominal),
#' with Nominal being further split between basic Nominal and Text.
#' These types roughly correspond to [numeric()], [ordered()], and [factor()].
#'
#' However, the correspondence between the base R types is not 100%. Thus, when passing a dataset from R to a JASP analysis,
#' JASP converts columns to these JASP types. Information from these columns is used for validating the input of the analysis
#' to ensure that the behavior is identical between R syntax and JASP application.
#'
#' The conversion uses simple heuristics (e.g., [`numeric()`] columns are converted to [`jaspScale()`]). For overriding
#' these heuristics, it is possible to convert a column to a specific JASP type before passing it to an analysis.
#'
#' To make it easier to reason how are these column conversion rules used within JASP, use [`jasp2r()`] and [`r2jasp()`]
#' functions that implement the implicit conversion rules using S3 dispatch.
#' Alternatively, it is possible to use functions [`setDataSet()`], [`getDataSet()`], and [`dataSetColumnSpecification()`],
#' that allow you to explicitly pass the data set to JASP, retrieve it, and check the column meta-data.
#'
#' @example inst/examples/ex-column-types.R
NULL


Expand Down Expand Up @@ -344,16 +362,18 @@ jasp2r.jaspScale <- function(x) {

#' @export
jasp2r.jaspOrdinal <- function(x) {
idx <- vctrs::vec_data(x)
values <- attr(x, "values")
labels <- attr(x, "levels")
ordered(vctrs::vec_data(x), levels = values, labels = labels)
labels <- attr(x, "labels")
ordered(values[idx], levels = values, labels = labels)
}

#' @export
jasp2r.jaspNominal <- function(x) {
idx <- vctrs::vec_data(x)
values <- attr(x, "values")
labels <- attr(x, "levels")
factor(vctrs::vec_data(x), levels = values, labels = labels)
labels <- attr(x, "labels")
factor(values[idx], levels = values, labels = labels)
}

#' @rdname column-types
Expand Down
2 changes: 1 addition & 1 deletion R/readDataSet.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ setDataSet <- function(dataset) {

#' @rdname column-types
#' @export
getDataSet <- function(dataset) {
getDataSet <- function() {
return(.internal[["dataset"]])
}

Expand Down
31 changes: 31 additions & 0 deletions inst/examples/ex-column-types.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# load mtcars
df <- mtcars
str(df)

# by default numeric columns are converted to jaspScale
lapply(df, r2jasp)


# change cyl to an ordinal variable
df$cyl <- jaspOrdinal(df$cyl, values = c(2, 4, 6, 8), labels = c("two", "four", "six", "eight"))
# change vs and am to nominal variables
df$vs <- jaspNominal(df$vs, labels = c("No", "Yes"))
df$am <- jaspNominal(df$am, labels = c("No", "Yes"))
# create a new variable that is a nominal text
df$group <- jaspNominal(rep(LETTERS[1:2], 16))

# factor and ordered will be converted to Nominal and Ordinal, respectively
df$gear <- as.factor(df$gear)
df$carb <- as.ordered(df$carb)

str(df)

# pass the data set to JASP
setDataSet(df)
# and retrieve it back
getDataSet() |> str()
# get the column specification of the set data frame as a list
dataSetColumnSpecification()

# check how are these columns converted back to R types
getDataSet() |> lapply(jasp2r)
54 changes: 52 additions & 2 deletions man/column-types.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd4614b

Please sign in to comment.