Error in `[.default`(x, i) : invalid subscript type 'list'
taylorreiter opened this issue · 3 comments
I have successfully run the test data:
library(dplyr)
library(ggplot2)
library(purrr)
library(readr)
library(wesanderson)
library(ggupset)
tidy_movies %>%
distinct(title, year, length, .keep_all=TRUE) %>%
ggplot(aes(x=Genres)) +
geom_bar() +
scale_x_upset(n_intersections = 20)
but when I try and use similar code on my own data, and get the error:
Error in `[.default`(x, i) : invalid subscript type 'list'
The code I'm running on my dataset looks like this:
all %>%
distinct(name, .keep_all=TRUE) %>%
ggplot(aes(x=source)) +
geom_bar() +
scale_x_upset()
And head()
of my data looks like this:
# A tibble: 6 x 2
source name
<chr> <chr>
1 iHMP JH590866.1 Lachnospiraceae bacterium 7_1_58FAA genomic scaffold supercont1.1, whole genome shotgun …
2 iHMP HGM_v1.0_all_60664_fna/SRS1719498_9.fna
3 iHMP HGM_v1.0_all_60664_fna/SRS294916_20.fna
4 iHMP HGM_v1.0_all_60664_fna/ERS396297_11.fna
5 iHMP HGM_v1.0_all_60664_fna/SRS1719577_6.fna
6 SRP1 HGM_v1.0_all_60664_fna/SRS1719577_6.fna
Is there an obvious reason for this error that I am missing?
My sessionInfo()
is:
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.2
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
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] ggupset_0.1.0 readr_1.3.1 purrr_0.3.3 ggplot2_3.3.0.9000 dplyr_0.8.5
[6] testthat_2.3.2 devtools_2.2.2 usethis_1.5.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.3 compiler_3.6.2 pillar_1.4.3 prettyunits_1.1.1 remotes_2.1.1
[6] tools_3.6.2 digest_0.6.25 pkgbuild_1.0.6 pkgload_1.0.2 memoise_1.1.0
[11] tibble_2.1.3 lifecycle_0.2.0 gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.5
[16] cli_2.0.2 rstudioapi_0.11 withr_2.1.2 vctrs_0.2.3 hms_0.5.3
[21] desc_1.2.0 fs_1.3.2 rprojroot_1.3-2 grid_3.6.2 tidyselect_1.0.0
[26] glue_1.3.1 R6_2.4.1 processx_3.4.2 fansi_0.4.1 sessioninfo_1.1.1
[31] callr_3.4.2 magrittr_1.5 backports_1.1.5 scales_1.1.0 ps_1.3.2
[36] ellipsis_0.3.0 assertthat_0.2.1 colorspace_1.4-1 munsell_0.5.0 crayon_1.3.4
Hi Taylor,
thanks for the extensive bug report. I can reproduce the problem with the following code:
library(tidyverse)
library(ggupset)
all <- tibble(source = c(rep("iHMP", 5), "SRP1"),
name = LETTERS[1:6])
all
#> # A tibble: 6 x 2
#> source name
#> <chr> <chr>
#> 1 iHMP A
#> 2 iHMP B
#> 3 iHMP C
#> 4 iHMP D
#> 5 iHMP E
#> 6 SRP1 F
all %>%
distinct(name, .keep_all=TRUE) %>%
ggplot(aes(x=source)) +
geom_bar() +
scale_x_upset()
#> Error in `[.default`(x, i): invalid subscript type 'list'
Created on 2020-03-27 by the reprex package (v0.3.0)
I don't know where it's coming from, but I will investigate what could be the root cause. It might be related to the recent update of ggplot2 to version 3.3.0.
Okay, it actually turned out to be a minor problem. You pass a character vector to aes(x = source)
. ggupset, however, expects a list. The solution to fix your problem is
library(tidyverse)
library(ggupset)
all <- tibble(source = c(rep("iHMP", 5), "SRP1"),
name = LETTERS[1:6])
# Informative Error message
all %>%
distinct(name, .keep_all=TRUE) %>%
ggplot(aes(x=source)) +
geom_bar() +
scale_x_upset()
#> Error in f(..., self = self): Error in scale_upset for aesthetic 'x'. 'x' must be of type list. It currently is: character
# Works:
all %>%
distinct(name, .keep_all=TRUE) %>%
mutate(source = as.list(source)) %>%
ggplot(aes(x=source)) +
geom_bar() +
scale_x_upset()
#> geom_path: Each group consists of only one observation. Do you need to adjust
#> the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to adjust
#> the group aesthetic?
Created on 2020-03-29 by the reprex package (v0.3.0)
However, the original error message was really confusing. I have fixed the issue and ggupset
now returns a more helpful explanation what goes wrong if you call scale_x_upset()
with a character vector.
Ah this makes sense...thank you so much, I love the more informative error message!