You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was facing this issue for a long time and finally found a way to reproduce it consistently. I am not sure here is the right place to submit the issue, but since vctrs is throwing the error, I think that it might good place to start.
In short, vctrs:::as.character.vctrs_vctr(y) throws conversion errors when working with labelled data, but only if dplyr (or tidyverse) is currently loaded AND havenis not loaded. If none of these packages are loaded it also works. So the question is: why loading dplyr affects how labelled data is handled?
The following reprex reproduces the issue. Imagine the case where you can start the analysis straight from file 2. In this case, depending on the packages loaded, the call with(df, table(y)) throws an error.
# file 2 (analysis) ----#* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#* restart session rstudioapi::restartSession(clean=TRUE)
sessionInfo()
temp_file<- paste0(tempdir(), "/../", "temp_file.rds")
df<- readRDS(temp_file)
with(df, table(y)) # WORKS
library(tidyverse)
with(df, table(y))
#' throws ERROR ! Can't convert `x` <haven_labelled> to <character>.#' if {tidyverse} is loaded.
library(dplyr)
with(df, table(y))
#' throws ERROR if {dplyr} is loaded (possibly other tidyverse packages as well)haven::as_factor
with(df, table(y))
#' no longer throws error if {haven::..} is called at some point or if #' haven is loaded onto the global environment#* end of file 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unsure if helpful, but running sessionInfo() after running haven::as_factor (without parenthesis) shows that haven namespace is loaded but not attached. So running loadNamespace("haven") would also be a workaround.
The text was updated successfully, but these errors were encountered:
I was facing this issue for a long time and finally found a way to reproduce it consistently. I am not sure here is the right place to submit the issue, but since
vctrs
is throwing the error, I think that it might good place to start.In short,
vctrs:::as.character.vctrs_vctr(y)
throws conversion errors when working withlabelled
data, but only ifdplyr
(ortidyverse
) is currently loaded ANDhaven
is not loaded. If none of these packages are loaded it also works. So the question is: why loadingdplyr
affects how labelled data is handled?I have a hunch that lines 43 and 42 of https://github.com/tidyverse/dplyr/blob/main/R/zzz.R might be the reason
reprex
The following reprex reproduces the issue. Imagine the case where you can start the analysis straight from
file 2
. In this case, depending on the packages loaded, the callwith(df, table(y))
throws an error.The quick solution was to always load
haven
and that's OK, but I think it is worth documenting this issue, because it might affect other people in different ways, as one can see here, where solutions suggested sound more like workaround than proper solutions, such as always loadinghaven
or coercing the columns to factor beforehand.error (backtrace)
notes
Unsure if helpful, but running
sessionInfo()
after runninghaven::as_factor
(without parenthesis) shows thathaven
namespace is loaded but not attached. So runningloadNamespace("haven")
would also be a workaround.The text was updated successfully, but these errors were encountered: