library(crunch) ## Load dataset, set working directory ds <- loadDataset("") getwd() setwd() ## Fill in the example CSV file and save to your working directory tab_spec <- "tab_book_spec_done.csv" ## Read the CSV tb_instructions <- read.csv(tab_spec, stringsAsFactors = FALSE) ## Once you load the CSV, loaded, you can just run all of the below in one go: # Creates and sets the output directory output_directory <- name(ds) dir.create(output_directory) setwd(output_directory) for (iii in seq_len(nrow(tb_instructions))) { vars <- trimws(unlist(strsplit(tb_instructions$rows[iii], ","))) vars_in_vars_catalog <- vars %in% names(variables(ds)) # TODO: get variables from existing multitable? if (any(!vars_in_vars_catalog)) { stop(paste0( "Could not find variable names in dataset: ", paste(vars[!vars_in_vars_catalog], collapse = ", ") )) } mt_str <- tb_instructions$multitable[iii] mt <- multitables(ds)[[mt_str]] if (is.null(mt)) { stop(paste0("Could not find multiable named: ", mt_str)) } filter_str <- tb_instructions$filter[iii] if (!is.na(filter_str) && filter_str != "" ) { filter <- filters(ds)[[filter_str]] if (is.null(filter)) { stop(paste0("Could not find filter named: ", filter_str)) } } else { filter <- NULL } weight_str <- tb_instructions$weight[iii] if (!is.na(weight_str) && weight_str != "" ) { weight <- ds[[weight_str]] if (is.null(weight)) { stop(paste0("Could not find variable named: ", weight_str)) } if (!is.weightVariable(weight)) { stop(paste0("variable named ", weight_str, " is not a weightVariable.")) } } else { weight <- NULL } title_str <- paste0(iii, " - ", tb_instructions$title[iii], ".xlsx") message(paste0("Making tabbook ", title_str)) tabBook( multitable = mt, dataset = ds[, aliases(variables(ds)[vars])], weight = weight, output_format = "xlsx", file = title_str, doc_layout = list(variable_sheets="one_sheet"), filter = filter ) } setwd("..")