Learn R Programming

DDIwR (version 0.24)

setupfile: Create setup files for SPSS, Stata, SAS and R

Description

Creates a setup file, based on a list of variable and value labels.

Usage

setupfile(
  obj,
  file = "",
  type = "all",
  csv = NULL,
  recode = TRUE,
  OS = "",
  stringnum = TRUE,
  ...
)

Value

A setup file to complement the imported raw dataset.

Arguments

obj

A labelled data frame, a DDI codeBook list returned by getCodebook(), or a path to a metadata file or directory for batch processing

file

Character, the (path to the) setup file to be created

type

The type of setup file, can be: "SPSS", "Stata", "SAS", "R", or "all" (default)

csv

The original dataset, used to create the setup file commands, or a path to the directory where the .csv files are located, for batch processing

recode

Logical, recode missing values to extended .a-.z range

OS

The target operating system, for the eol - end of line character(s)

stringnum

Logical, recode string variables to numeric

...

Other arguments, see Details below

Author

Adrian Dusa

Details

A codeBook object is an R list containing DDI metadata, such as variable labels and value labels. It can be created from a labelled data frame with getCodebook(). A data frame can also be passed directly to setupfile(). The generated commands have a configuration section where input and output paths must be set before running them in the target software. Validation errors do not write blank lines to standard output. Output sinks already active in the caller are retained on success and error.

When a path to a metadata directory is specified for the argument obj, then next argument file is silently ignored and all created setup files are saved in a directory called "Setup Files" that (if not already found) is created in the working directory.

The argument file expects the name of the final setup file being saved on the disk. If not specified, the name of the object provided for the obj argument will be used as a filename.

If file is specified, the argument type is automatically determined from the file's extension, otherwise when type = "all", the function produces one setup file for each supported type.

If batch processing multiple files, the function will inspect all files in the provided directory, and retain only those with the extension .R or .r or DDI versions with the extension .xml or .XML (it will subsequently generate an error if the .R files do not contain an object list, or if the .xml files do not contain a DDI structured metadata file).

If the metadata directory contains a subdirectory called "data" or "Data", it will match the name of the metadata file with the name of the .csv file (their names have to be exactly the same, regardless of their extension).

The csv argument can provide a data frame object produced by reading the .csv file, or a path to the directory where the .csv files are located. If the user doesn't provide something for this argument, the function will check the existence of a subdirectory called data in the directory where the metadata files are located.

In batch mode, the code starts with the argument delim = ",", but if the .csv file is delimited differently it will also try hard to find other delimiters that will match the variable names in the metadata file. At the initial version 0.1-0, the automatically detected delimiters include ";" and "\t".

The argument OS (case insensitive) can be either:
"Windows" (default), or "Win",
"MacOS", "Darwin", "Apple", "Mac",
"Linux".

The end of line character(s) changes only when the target OS is different from the running OS.

Stored formats in spss_metadata and stata_metadata take precedence over legacy format attributes when the input is a data frame. A present format = NULL suppresses legacy fallback and uses the inferred format.

Examples

Run this code
# A small dataset with variable and value labels
survey <- data.frame(
    age = declared(
        c(28, 35, 42),
        label = "Age in years"
    ),
    device = declared(
        c(1, 2, 1),
        label = "Device used",
        labels = c(Desktop = 1, Mobile = 2)
    )
)

# Use a temporary directory for the files created in this example
temp_dir <- tempdir()
csv_file <- file.path(temp_dir, "survey.csv")
admisc::export(undeclare(survey, drop = TRUE), file = csv_file)

# codeBook is an R object containing DDI metadata extracted from survey.
# It describes the variables and their labels; the observations are in the CSV.
codeBook <- getCodebook(survey)

# Create Stata commands to import the CSV and apply the metadata
# Edit the generated file's configuration paths before running it in Stata.
setupfile(codeBook, file = file.path(temp_dir, "survey.do"), csv = csv_file)

# A labelled data frame can also be supplied directly
setupfile(survey, file = file.path(temp_dir, "survey.sps"))

# The file extension selects the software: .do, .sps, .sas or .R.
# A setup file contains import commands, not the observations themselves.

if (FALSE) {
# Alternatively, read metadata from an existing DDI XML file
setupfile("/path/to/metadata.xml", file = "survey.do",
    csv = "/path/to/survey.csv")

# Batch processing: read metadata and CSV files from matching directories
setupfile("/path/to/metadata/directory", csv = "/path/to/csv/directory")
}

Run the code above in your browser using DataLab