Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

likert() does not seem to handle tbl_df data #55

Closed
briandk opened this issue May 15, 2016 · 1 comment
Closed

likert() does not seem to handle tbl_df data #55

briandk opened this issue May 15, 2016 · 1 comment

Comments

@briandk
Copy link

briandk commented May 15, 2016

Expected Behavior

If data is of class tbl_df (or inherits from it), likert::likert() should be able to handle it without issue.

Actual Behavior

Handing in data inheriting from class tbl_df produces an error:

Error in likert(test_data) : 
  The items parameter must be a data frame. If trying to subset a data frame to analyze only one column, try: items=mydf[,1, drop=FALSE].
In addition: Warning message:
In if (class(items) != "data.frame") { :
  the condition has length > 1 and only the first element will be used

Steps to Reproduce

# Checkpoint for reproducibility
require(checkpoint)
checkpoint("2016-03-30", R.version = "3.2.4")

require(dplyr)
require(likert)
data("pisaitems")

test_data <- pisaitems %>%
  select(one_of("ST24Q02", "ST24Q01")) %>%
  tbl_df()

# If `test_data` is a `tbl_df`, this call will fail because likert() claims the
#   `items` parameter is not a data frame
likert(test_data)

# Calling `class()` on `test_data` shows it inherits from multiple classes,
#   including `data.frame`. So, I'm not sure what's happening exactly,
#   but I suspect it's related to this source code block: https://github.com/jbryer/likert/blob/4ff9837ffa17085219c5b23f5e8319b0405e2893/R/likert.R#L71-L74
class(test_data)

# Coercing `test_data` back to a data frame fixes the issue,
#   but is there any reason `tbl_df` data should be failing?
as.data.frame(test_data) %>% likert()

Where I suspect the error might be originating

Based on my inspection, the only part of the likert package source code that could produce this error is here:

likert/R/likert.R

Lines 71 to 74 in 4ff9837

if(!('data.frame' %in% class(items))) {
stop(paste0('The items parameter must be a data frame. If trying ',
'to subset a data frame to analyze only one column, try: ',
'items=mydf[,1, drop=FALSE].'))

Which in turn means this conditional check is returning TRUE :

if(!('data.frame' %in% class(items)))

Session Info

R version 3.2.4 (2016-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.4 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] likert_1.3.3      xtable_1.8-2      ggplot2_2.1.0     dplyr_0.4.3       checkpoint_0.3.16

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.4      knitr_1.12.3     magrittr_1.5     mnormt_1.5-4     munsell_0.4.3   
 [6] colorspace_1.2-6 R6_2.1.2         stringr_1.0.0    plyr_1.8.3       tools_3.2.4     
[11] parallel_3.2.4   grid_3.2.4       gtable_0.2.0     psych_1.6.4      DBI_0.3.1       
[16] lazyeval_0.1.10  assertthat_0.1   digest_0.6.9     gridExtra_2.2.1  reshape2_1.4.1  
[21] formatR_1.3      labeling_0.3     stringi_1.0-1    scales_0.4.0    
leeper added a commit to leeper/likert that referenced this issue Jul 7, 2016
jbryer added a commit that referenced this issue Jun 27, 2017
change class checking to inherits() for #55
@jbryer
Copy link
Owner

jbryer commented Jun 27, 2017

I would recommend using as.data.frame before calling likert.

@jbryer jbryer closed this as completed Jun 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants