--- title: "Translating the Excel mapping commands to R" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Translating the Excel mapping commands to R} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ```{r setup} library(datadaptor) ``` When a `mapping` object is created, ```{r} mapping_file <- system.file("extdata", "mapping.xlsx", package = "datadaptor") spss_file <- system.file("extdata", "mtcars_labelled.sav", package = "datadaptor") m <- Mapping$new(spss_file, mapping_file) ``` the command blocks are translated to a list structure `m$cmd` containing intermediate results. ## Excel sheets parsed When the `mapping_file` is passed to construct a `Mapping` object `m`, the commands of all sheets whose names start with one of the following strings will be parsed: ```{r} command_block_classes$sheet |> na.omit() |> unique() ``` ## Parse the sheets to an R list object The sheets are parsed to a named list `m$cmd$sheet_data_raw` with dataframes. This is what it looks like for the `"Free1"` sheet of the Excel file: ```{r} m$cmd$sheet_data_raw$Free1 ``` ## Raw command table The whole information in `m$cmd$sheet_data_raw` is then joint to a dataframe ```{r} m$cmd$df_cmd_raw ``` ## Generating `"command_block"`s Each line in `m$cmd$df_cmd_raw` is translated to a `command_block()`. The whole resulting list of all lines comprised in a `command_blocks()` object. These objects have their own printing method. See here for the 3 first elements: ```{r} m$cmd$command_blocks[1:3] ``` But underneath, it is a list where each element has a field `args`. For instance, the third element looks like this: ```{r} names(m$cmd$command_blocks[[3]]) m$cmd$command_blocks[[3]]$args ``` ## Overview command table The whole information is put to `m$cmd_tbl`: ```{r} m$cmd_tbl ``` ## `args` overview table Lets have a look at a table with an example of the types of the objects in `args` for each of the `"command_block"` subclasses: * `x`, `y`: Character strings of variable names in the data set (or to be created). * `varlab` Character string containing a variable label * `v`: numeric() (or character()) vector; value(s) of a value label * `vallab`: character() vector; value label(s) * `id_list`: a list of ids in the data, * valid R character vectors of R expressions: * `ex`: an expression, * `ex_cond`: a conditional expression, * `ex_fun`: an existing function name * `filepath`: a filepath ## Apply command blocks These command blocks can then be applied to the data with ```{r} m$modify_data() ``` Underneath the hood, this function calls `apply_commands()` and passes it the arguments `args` (see above). You can then access the data with ```{r} m$dat_mod ```
Click here to show whole list structure print output of `m$cmd`! ```{r} m$cmd ```