### Installing the R haven Package Source: https://github.com/tidyverse/haven/blob/main/README.md This snippet demonstrates how to install the 'haven' R package using `install.packages()`. It shows two options: installing the entire tidyverse suite which includes haven, or installing haven directly. It requires an active internet connection and access to CRAN. ```R # The easiest way to get haven is to install the whole tidyverse: install.packages("tidyverse") # Alternatively, install just haven: install.packages("haven") ``` -------------------------------- ### R Package Check Log (Devel Build) Source: https://github.com/tidyverse/haven/blob/main/revdep/failures.md This log shows the result of a package check for the 'RVA' package using the development build. It reports an ERROR because a required package, 'clusterProfiler', is not available, preventing successful installation or checking. ```R * using log directory ‘/tmp/workdir/RVA/new/RVA.Rcheck’ * using R version 4.1.1 (2021-08-10) * using platform: x86_64-pc-linux-gnu (64-bit) * using session charset: UTF-8 * using option ‘--no-manual’ * checking for file ‘RVA/DESCRIPTION’ ... OK * this is package ‘RVA’ version ‘0.0.5’ * package encoding: UTF-8 * checking package namespace information ... OK * checking package dependencies ... ERROR Package required but not available: ‘clusterProfiler’ See section ‘The DESCRIPTION file’ in the ‘Writing R Extensions’ manual. * DONE Status: 1 ERROR ``` -------------------------------- ### Writing XPT file with invalid name (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-sas.md This R snippet attempts to write the `mtcars` dataset to an XPT file using `write_xpt`. It deliberately uses a filename `" temp.xpt"` which contains a leading space, causing the function to fail. This tests the error handling for invalid file paths, expected to produce an error about an illegal character. ```R write_xpt(mtcars, file.path(tempdir(), " temp.xpt")) ``` -------------------------------- ### Reading and Writing Statistical Data in R with haven Source: https://github.com/tidyverse/haven/blob/main/README.md This snippet shows examples of using 'haven' functions to interact with common statistical file formats. It includes loading the package with `library()`, reading SAS (`.sas7bdat`, `.xpt`), SPSS (`.sav`), and Stata (`.dta`) files using `read_` functions, and writing R data frames (`mtcars`) to SAS (`.xpt`), SPSS (`.sav`), and Stata (`.dta`) formats using `write_` functions. Requires existing data files or the 'mtcars' dataset. ```R library(haven) # SAS read_sas("mtcars.sas7bdat") write_xpt(mtcars, "mtcars.xpt") # SPSS read_sav("mtcars.sav") write_sav(mtcars, "mtcars.sav") # Stata read_dta("mtcars.dta") write_dta(mtcars, "mtcars.dta") ``` -------------------------------- ### Printing Tagged NA Values in R Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/tagged_na.md This snippet calls the `print_tagged_na` function with a variable `x` which is expected to contain a mix of standard numeric values and tagged missing values (e.g., from data imported using `haven`). This function requires the `haven` package. The input is a vector `x`, and the output is the formatted console printout showing how tagged NAs are represented (e.g., `NA(a)`, `NA(b)`, etc.). ```R print_tagged_na(x) ``` -------------------------------- ### Displaying Labelled R Vector with Tagged NAs Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled-pillar.md Shows how labelled vectors containing tagged missing values (`tagged_na`) are displayed in a tibble. It includes examples of untagged NA, tagged NA without a label, and tagged NA that corresponds to a defined label. ```R x <- labelled(c(1:8, tagged_na("a"), tagged_na("b"), NA), c(Good = 1, Bad = 8, Refused = tagged_na("b"))) tibble::tibble(x) ``` -------------------------------- ### Testing labelled_spss na_range Type and Length Validation (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled_spss.md Tests the validation of the `na_range` argument, showing that it must be a vector of length two and have the same data type as the input vector `x`. Provides an example using a character string, which is expected to fail. ```R labelled_spss(1:10, na_range = "a") ``` -------------------------------- ### Testing labelled_spss na_values Type Validation (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled_spss.md Demonstrates that the `na_values` argument must have the same data type as the main data vector `x` when constructing a `labelled_spss` object. Provides an example attempting to use a character string for `na_values` with an integer vector `x`, which is expected to fail. ```R labelled_spss(1:10, na_values = "a") ``` -------------------------------- ### Writing XPT with column exceeding user width (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-sas.md This R snippet uses `write_xpt` to write a data frame `df` to a file at `path`. The context indicates this scenario specifically triggers a warning because a column (`b`) has string values longer than an assumed user width of 1. The function is expected to issue a warning and adjust the column width accordingly. ```R write_xpt(df, path) ``` -------------------------------- ### Writing DTA with Long Variable Name (Stata 13) (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-stata.md Attempts to write a Stata .dta file using `haven::write_dta` with a variable name exceeding the Stata length limit, showing the validation error for Stata version 13. This snippet reuses the `df` object from the previous example after modifying its name, along with the `long` string from the first example. ```R names(df) <- long write_dta(df, tempfile(), version = 13) ``` -------------------------------- ### Testing labelled_spss na_range Order Validation (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled_spss.md Validates that the two values provided in the `na_range` argument must be in ascending order to define a valid range of missing values. Shows an example where the values are in descending order, which is expected to throw an error. ```R labelled_spss(1:10, na_range = c(2, 1)) ``` -------------------------------- ### Displaying Labelled R Vectors in Tibble Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled-pillar.md Demonstrates the basic display of labelled vectors of integer, double, and character types within a tibble. It shows how the value and the label are displayed together in the console output format provided by pillar. ```R int <- labelled(1:5, labels = c(good = 1L, bad = 5L)) dbl <- labelled(1:5 / 10, labels = c(good = 0.1, bad = 0.5)) chr <- labelled(letters[1:5], labels = c(good = "a", bad = "e")) tibble(int, dbl, chr) ``` -------------------------------- ### Displaying Labelled Double Vector in R Tibble Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled-pillar.md Demonstrates the display of a labelled double vector containing repeating values and labels, showing how floating-point values and their labels are presented, potentially with rounding in the display. ```R x <- labelled(c(rep(c(1.22352, 1000, -345), each = 3), 35, 35), c(One = 1.22352, Two = 35, Threeeee = 1000)) tibble::tibble(x) ``` -------------------------------- ### Displaying Labelled Integer Vector in R Tibble (Pillar) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled-pillar.md Shows the display of a labelled integer vector with multiple values in a tibble, specifically demonstrating how the pillar format renders labelled values alongside unlabelled values in a longer list. ```R x <- labelled(1:11, c(Good = 1, Bad = 8)) tibble::tibble(x) ``` -------------------------------- ### Displaying Labelled Character Vector with NA in R Tibble Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled-pillar.md Illustrates the display of a labelled character vector including repeating values and an NA value, demonstrating how character labels and standard missing values are shown in the tibble output. ```R x <- labelled(c(rep("A", 3), rep("B", 3), rep("XXXXXX", 4), NA), c(Apple = "A", Banana = "B", Mystery = "XXXXXX")) tibble::tibble(x) ``` -------------------------------- ### Printing labelled_spss Object Output (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled_spss.md Shows the standard console output when printing a `labelled_spss` object with value labels, specific missing values, and a missing range defined. Illustrates the typical format used by the `haven` package for displaying this object type. ```R x ``` -------------------------------- ### Writing DTA with Long File Label (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-stata.md Attempts to write a Stata .dta file using `haven::write_dta` with a file label exceeding the 80-character limit, demonstrating the expected validation error message. ```R long <- paste(rep("a", 100), collapse = "") write_dta(data.frame(x = 1), tempfile(), label = long) ``` -------------------------------- ### Displaying Labelled R Vector with Special Characters in Labels Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled-pillar.md Illustrates how labels containing various special characters such as spaces, tabs, newlines, C0 control characters, quotes, and backslashes are handled and displayed by pillar when used with labelled vectors. ```R x <- labelled(c("spaces", "tabs", "newlines", "c0", "quote", "backslash"), c( `a b` = "spaces", `a\tb` = "tabs", `a\nb` = "newlines", `a\001b` = "c0", `a\"b` = "quote", `a\\b` = "backslash")) tibble::tibble(x) ``` -------------------------------- ### R Package Check Log (CRAN Build) Source: https://github.com/tidyverse/haven/blob/main/revdep/failures.md This log shows the result of a package check for the 'RVA' package using the CRAN build configuration. Similar to the development build check, it reports an ERROR indicating that the required dependency 'clusterProfiler' is not found. ```R * using log directory ‘/tmp/workdir/RVA/old/RVA.Rcheck’ * using R version 4.1.1 (2021-08-10) * using platform: x86_64-pc-linux-gnu (64-bit) * using session charset: UTF-8 * using option ‘--no-manual’ * checking for file ‘RVA/DESCRIPTION’ ... OK * this is package ‘RVA’ version ‘0.0.5’ * package encoding: UTF-8 * checking package namespace information ... OK * checking package dependencies ... ERROR Package required but not available: ‘clusterProfiler’ See section ‘The DESCRIPTION file’ in the ‘Writing R Extensions’ manual. * DONE Status: 1 ERROR ``` -------------------------------- ### Displaying Labelled SPSS Vector with Missing Values in R Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled-pillar.md Demonstrates the display of a vector created with `labelled_spss`, which allows defining specific values as user-defined missing. The output shows how these values are marked as (NA) alongside their potential labels. ```R x <- labelled_spss(c(1:10, NA), c(Good = 1, Bad = 8, Refused = 10), c(9, 10)) tibble::tibble(x) ``` -------------------------------- ### Display Labelled Vector in R Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled.md Evaluates and displays the content and structure of a labelled double vector `x`. Shows the numeric values, special NA types (like NA(x), NA(y), NA(z)), and the associated value labels defined for specific numeric values. ```R x ``` -------------------------------- ### Writing DTA with Invalid Variable Name (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-stata.md Attempts to write a Stata .dta file using `haven::write_dta` with a variable name that is invalid in Stata (contains a space), showing the resulting validation error for Stata version 13. ```R df <- data.frame(1) names(df) <- "x y" write_dta(df, tempfile(), version = 13) ``` -------------------------------- ### Writing DTA with Non-Integer Value Labels (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-stata.md Attempts to write a Stata .dta file using `haven::write_dta` where a variable has value labels defined for non-integer values (1.5), demonstrating the validation error because Stata requires integer value labels. ```R df <- data.frame(x = labelled(c(1, 2.5, 3), c(b = 1.5))) write_dta(df, tempfile()) ``` -------------------------------- ### Displaying Labelled R Vector with Non-ASCII Character in Label Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled-pillar.md Shows how a label containing a non-ASCII character (specifically a high-byte Unicode character represented as `\u0080`) is displayed when using labelled vectors in a tibble. ```R x <- "c1" label <- x names(label) <- "a\u0080b" x <- labelled(x, label) tibble::tibble(x) ``` -------------------------------- ### Writing DTA with Long Variable Name (Stata 14) (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-stata.md Attempts to write a Stata .dta file using `haven::write_dta` with the same overly long variable name as the previous snippet, but explicitly targeting Stata version 14, confirming that the validation error persists across this version. ```R write_dta(df, tempfile(), version = 14) ``` -------------------------------- ### Saving Data with Duplicate Variable Names using haven::write_sav (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-spss.md This snippet creates a data frame with variable names that are identical when case is ignored (`a` and `A`). SPSS treats variable names case-insensitively and does not allow duplicates. The code attempts to save this data frame using `write_sav`, requiring the `haven` package, and is expected to produce an error due to the duplicate variable names. ```R df <- data.frame(a = 1, A = 1, b = 1) write_sav(df, tempfile()) ``` -------------------------------- ### Testing labelled_spss na_values Missing Value Check (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled_spss.md Illustrates that the `na_values` argument cannot contain missing values (like `NA`) when constructing a `labelled_spss` object. Shows an attempt to use `NA_integer_` as a missing value placeholder, which is expected to throw an error. ```R labelled_spss(1:10, na_values = NA_integer_) ``` -------------------------------- ### Saving Data with Long Factor Labels using haven::write_sav (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-spss.md This snippet creates a data frame containing a factor variable whose single level exceeds the SPSS maximum limit of 120 characters. It then attempts to save this data frame to an SPSS `.sav` file using `write_sav`. This test requires the `haven` package and is expected to result in an error indicating the factor label is too long. ```R x <- paste(rep("a", 200), collapse = "") df <- data.frame(x = factor(x)) write_sav(df, tempfile()) ``` -------------------------------- ### Saving Data with Reserved SPSS Variable Names using haven::write_sav (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-spss.md This snippet modifies a data frame's column names to use strings (`ALL` and `eq`) that are reserved keywords in SPSS. Attempting to save a data frame with reserved variable names using `write_sav` (which requires the `haven` package) should trigger an error indicating that the variable names are invalid. ```R names(df) <- c("ALL", "eq", "b") write_sav(df, tempfile()) ``` -------------------------------- ### Testing labelled_spss na_range Length Validation (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled_spss.md Verifies that the `na_range` argument, used to specify a range of missing values, must be a vector of exactly length two. Shows an attempt to use a vector of length three, which is expected to cause an error. ```R labelled_spss(1:10, na_range = 1:3) ``` -------------------------------- ### Saving Data with Invalid SPSS Variable Names using haven::write_sav (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-spss.md This snippet takes an existing data frame and renames its columns to include characters (`$` and `.`) that are invalid for SPSS variable names according to its naming rules. It then attempts to save this data frame using `write_sav`. This requires the `haven` package and is designed to trigger an error for invalid variable names. ```R names(df) <- c("$var", "A._$@#1", "a.") write_sav(df, tempfile()) ``` -------------------------------- ### Saving Data with Long SPSS Variable Names using haven::write_sav (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/haven-spss.md This snippet creates a data frame and assigns variable names that exceed the maximum length permitted by SPSS (typically 64 bytes). It attempts to save this data frame using `write_sav`, requiring the `haven` package. The expected outcome is an error indicating that the variable names are too long. ```R names(df) <- c(paste(rep("a", 65), collapse = ""), paste(rep("b", 65), collapse = ""), "c") write_sav(df, tempfile()) ``` -------------------------------- ### Testing labelled_spss na_range Missing Value Check (R) Source: https://github.com/tidyverse/haven/blob/main/tests/testthat/_snaps/labelled_spss.md Checks that the `na_range` argument, which specifies a range of missing values, cannot contain any missing values (`NA`). Demonstrates a failure when attempting to include `NA` in the range vector. ```R labelled_spss(1:10, na_range = c(2, NA)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.