knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This vignettes shows a minimal working example for creating a codebook via the eatCodebook package. For illustrative purposes we use the R built in data set iris. We import the data set using the eatGADS package, which is automatically installed when eatCodebook is installed. We also run the checkFormat() function from the eatGADS package which adds SPSS format information to the meta data of the data set.

library(eatCodebook)
dat <- eatGADS::import_DF(iris)
dat <- eatGADS::checkFormat(dat)

Descriptive Statistics

One of the key elements of a codebook are descriptive statistics shortly describing each variable in the data set. What kind of descriptive statistics is reported for each variable depends on the type of the variable. The function createInputForDescriptives() creates a template to provide the information that is needed to calculate the descriptive statistics for an GADSdat object.

inputForDescriptives <- createInputForDescriptives(GADSdat = dat)
head(inputForDescriptives)

The template should be exported to .xlsx, modified and reimported to R.

writeExcel(inputForDescriptives, "inputForDescriptives.xlsx")
inputForDescriptives_edited <- getInputForDescriptives("inputForDescriptives.xlsx")
inputForDescriptives_edited <- inputForDescriptives

This input is then used to calculate descriptive statistics via calculateDescriptives().

# calculate descriptives
descStatistics <- calculateDescriptives(GADSdat = dat, 
                                        inputForDescriptives = inputForDescriptives_edited)
descStatistics[[3]]

Value and Missing Labels

Another important part of a codebook is the documentation of the value labels of valid and missing values. A respective overview is created via createMissings().

missings <- createMissings(dat, inputForDescriptives = inputForDescriptives_edited)
head(missings)

In this case, the resulting object missings has to be written to xlsx and imported via getMissings(). Note that all the getXXX functions perform important cleaning and preparation steps, therefore the exporting to xlsx is obligatory.

writeExcel(missings, "example_miss.xlsx", row.names = FALSE)
miss_final <- getMissings("example_miss.xlsx")
miss_file <- system.file("extdata", "example_iris_miss.xlsx", package = "eatCodebook")
miss_final <- getMissings(miss_file)
head(miss_final)

Variable Information

A key element of the eatCodebook package is that various forms of variable information can be supplied.

varInfo <- createVarInfo(dat, inputForDescriptives = inputForDescriptives_edited)
head(varInfo)
writeExcel(varInfo, "example_varInfo.xlsx", row.names = FALSE)
varInfo_final <- getVarInfo("example_varInfo.xlsx")
varInfo_final2 <- inferLayout(varInfo_final, GADSdat = dat, 
                              inputForDescriptives = inputForDescriptives_edited)
varInfo_file <- system.file("extdata", "example_iris_varInfo.xlsx", package = "eatCodebook")
varInfo_final <- getVarInfo(varInfo_file)
varInfo_final2 <- inferLayout(varInfo_final, GADSdat = dat, inputForDescriptives = inputForDescriptives_edited)
head(varInfo_final)

Structure

A key element of the eatCodebook package is that various forms of variable information can be supplied.

struc <- createStructure(varInfo_final)
head(struc)
writeExcel(struc, "example_struc.xlsx", row.names = FALSE)
struc_final <- getStructure("example_struc.xlsx")
struc_file <- system.file("extdata", "example_iris_struc.xlsx", package = "eatCodebook")
struc_final <- getStructure(struc_file)
head(struc_final)

Scale Information

A key element of the eatCodebook package is that various forms of variable information can be supplied.

scaleInfo <- createScaleInfo(inputForDescriptives_edited)
head(scaleInfo)
writeExcel(scaleInfo, "example_scaleInfo.xlsx", row.names = FALSE)
scaleInfo_final <- getScaleInfo("example_scaleInfo.xlsx")
scaleInfo_file <- system.file("extdata", "example_iris_scaleInfo.xlsx", package = "eatCodebook")
scaleInfo_final <- getScaleInfo(scaleInfo_file)
head(scaleInfo_final)

Meta data

Meta data can be added to the codebook.

meta <- createMetadata()
meta[1, "Title"] <- "Codebook Test"
meta[1, "Author"] <- "Anna Muster"
meta[1, "Keywords"] <- "lsa, education"
meta[1, "Subject"] <- "test"
writeExcel(meta, "example_meta.xlsx", row.names = FALSE)
meta_final <- makeMetadata("example_meta.xlsx")
meta_file <- system.file("extdata", "example_iris_meta.xlsx", package = "eatCodebook")
meta_final <- makeMetadata(meta_file)

Chapters

Create the chapter structure.

chapters <- createChapters(varInfo_final2)
chapters[1, 2] <- "Iris Datensatz"

Codebok

Now we create the actual codebook script via calling the codebook() function.

latex_skript <- codebook(varInfo = varInfo_final2, missings = miss_final, struc = struc_final,
                         scaleInfo = scaleInfo_final, dat = eatGADS::extractData(dat),
                         Kennwertedatensatz = descStatistics, chapters = chapters)

Save the Codebok

The resulting object and the meta data are then separately save to the hard drive. Both objects should be saved into the same folder.

write.table(latex_skript , file = "minimal_example.tex" , fileEncoding="UTF-8" ,
            col.names=FALSE , row.names=FALSE , quote = FALSE )
write.table(meta_final , file = "minimal_example_meta.xmpdata", fileEncoding="UTF-8" ,
            col.names=FALSE , row.names=FALSE , quote = FALSE )

Compilation

The LaTeX script for the codebook has now been saved to the hard drive. This file should now be opened via a Tex-Editor and simply compiled. Sometimes multiple consecutive compilation steps are required for a clean output. Alternatively, the document can be compiled from within R, for example via tools::texi2pdf().



beckerbenj/eatCodebook documentation built on Feb. 24, 2025, 9:36 a.m.