krassowski / complex-upset

A library for creating complex UpSet plots with ggplot2 geoms
MIT License
469 stars 28 forks source link

Error in 'vec_slice()' #202

Open clemencerapicault opened 5 months ago

clemencerapicault commented 5 months ago

Objective Hello, I have used a code to generate ComplexUpset graphics that works on my PC. I also work in a secure bubble (CAS-D), I tried to import and reproduce the same analysis in the bubble but I get an error even though the format of my data is exactly the same. I've checked the package updates but it still doesn't work and I don't understand the error, as the format of my data is the same as on my usual PC.

This is the error I get: Error in 'vec_slice()' : ! Can't subset elements with 'i'. Subscript 'i' must be a simple vector, not a matrix. Run 'rlang::last_trace()' to see where the error occurred.

Thank you in advance for your help.

Code

set_size = function(w, h, factor=1.5) {
  s = 1 * factor
  options(
    repr.plot.width=w * s,
    repr.plot.height=h * s,
    repr.plot.res=100 / factor,
    jupyter.plot_mimetypes='image/png',
    jupyter.plot_scale=1
  )
}

set_size(8, 3)
size <- get_size_mode('exclusive_intersection')
expos <- c("BorgVenn",
           "C_AQ_EXPOACT_Trrepet_01",
           "C_AQ_EXPOACT_TrOutVibr_01",
           "C_AQ_EXPOACT_TrTordPoignet_01",
           "C_AQ_EXPOACT_TrPincer_01")

BDD_finale_CC_UpsetBiomeca_hommes[expos] <- BDD_finale_CC_UpsetBiomeca_hommes[expos] == 1 | BDD_finale_CC_UpsetBiomeca_hommes[expos] == "Oui"

# Copiez le dataframe pour éviter de modifier l'original
BDD_finale_CC_UpsetBiomeca_hommes_mod <- BDD_finale_CC_UpsetBiomeca_hommes

# Renommez les colonnes pour l'affichage
colnames(BDD_finale_CC_UpsetBiomeca_hommes_mod)[colnames(BDD_finale_CC_UpsetBiomeca_hommes_mod) %in% expos] <- c(
  "Repetitive hand movements",
  "Hand-transmitted vibrations", 
  "Awkward wrist postures", 
  "Repetitive pinching",
  "High physical perceived exertion (RPE ≥12)")

expos <- c("High physical perceived exertion (RPE ≥12)", 
           "Repetitive hand movements", 
           "Hand-transmitted vibrations", 
           "Awkward wrist postures", 
           "Repetitive pinching")

# Fonction pour calculer les tailles et les pourcentages des intersections
calculate_intersection_sizes <- function(data, sets) {
  upset_data <- data %>%
    dplyr::select(all_of(sets)) %>%
    mutate(across(everything(), as.logical)) %>%
    group_by(across(everything())) %>%
    summarise(size = n(), .groups = 'drop') %>%
    ungroup()

  total_size <- sum(upset_data$size)
  upset_data <- upset_data %>%
    mutate(percentage = size / total_size * 100) %>%
    mutate(text_label = paste0(round(percentage, 1), "%\n(", size, ")"))

  return(upset_data)
}

# Calculez les tailles et les pourcentages des intersections
intersection_sizes <- calculate_intersection_sizes(BDD_finale_CC_UpsetBiomeca_hommes_mod, expos)

# Vérifiez que les pourcentages sont calculés correctement
print(intersection_sizes)

# Fonction pour générer les labels d'annotations
generate_labels <- function(size, data) {
  match_indices <- match(size, data$size)
  return(data$text_label[match_indices])
}

# Graphique avec ComplexUpset
upset_plot <- ComplexUpset::upset(
  BDD_finale_CC_UpsetBiomeca_hommes_mod, 
  expos, 
  name = 'Exposure', 
  width_ratio = 0.1, 
  min_size = 30,
  min_degree = 1,
  base_annotations = list(
    'Intersection size' = intersection_size(
      text_mapping = aes(
        label = generate_labels(!!size, intersection_sizes),
        colour = ifelse(!!size > 300, 'on_bar', 'on_background'),
        y = ifelse(!!size > 300, !!size - 300, !!size)
      )
    )
  ),
  set_sizes = (
    upset_set_size()
    + geom_text(aes(label = ..count..), hjust = 0, stat = 'count', color = 'white')
    + theme(axis.text.x = element_text(angle = 90),
            axis.ticks.x = element_line())
  )
)

# Affichez le graphique
print(upset_plot)

Context (required)

ComplexUpset version: 1.3.3

R version details ```R $platform [1] "x86_64-w64-mingw32" $arch [1] "x86_64" $os [1] "mingw32" $crt [1] "ucrt" $system [1] "x86_64, mingw32" $status [1] "" $major [1] "4" $minor [1] "4.0" $year [1] "2024" $month [1] "04" $day [1] "24" $`svn rev` [1] "86474" $language [1] "R" $version.string [1] "R version 4.4.0 (2024-04-24 ucrt)" $nickname [1] "Puppy Cup" ```
R session information ```R R version 4.4.0 (2024-04-24 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows Server 2019 x64 (build 17763) Matrix products: default locale: [1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 [4] LC_NUMERIC=C LC_TIME=French_France.1252 time zone: Europe/Paris tzcode source: internal ```