大括号右侧的 magrittr 管道给出错误

magrittr pipe to righthand side in braces gives error

我正在使用 magrittr 管道 %>% 将一个对象通过管道传递给右侧函数中的多个参数,使用大括号强制 :

data_bec <- dds_tmp %>% 
  {sva::ComBat_seq(
    counts = . %>% SummarizedExperiment::assay(), 
    batch = . %>% magrittr::extract2("batch") %>% droplevels(),
    covar_mod = cbind(
      time = . %>% magrittr::extract2("time") %>% droplevels(),
      treatment = . %>% magrittr::extract2("treatment") %>% droplevels()
    )
  )}

这给出了这个错误:

Error in unique.default(x, nmax = nmax) : 
  unique() applies only to vectors

但是,下面的代码有效:

data_bec <- dds_tmp %>% 
  {sva::ComBat_seq(
    counts = SummarizedExperiment::assay(.), 
    batch = magrittr::extract2(., "batch") %>% droplevels(),
    covar_mod = cbind(
      time = magrittr::extract2(., "time") %>% droplevels(),
      treatment = magrittr::extract2(., "treatment") %>% droplevels()
    )
  )}

我无法理解为什么 magrittr::extract2(., "batch") %>% droplevels() 在大括号内的函数内工作,但 . %>% magrittr::extract2("batch") %>% droplevels() 不起作用。有没有一种简单的方法可以理解这种行为?

注意:我的 dds_tmp 对象很大,由 DESeq2::sample_table 生成。这是一个小示例数据集:

dds_tmp <- new(
  "DESeqDataSet",
  design = ~ 1,
  dispersionFunction = function ()
    NULL,
  rowRanges = new(
    "CompressedGRangesList",
    unlistData = new(
      "GRanges",
      seqnames = new(
        "Rle",
        values = structure(integer(0), .Label = character(0), class = "factor"),
        lengths = integer(0),
        elementMetadata = NULL,
        metadata = list()
      ),
      ranges = new(
        "IRanges",
        start = integer(0),
        width = integer(0),
        NAMES = NULL,
        elementType = "ANY",
        elementMetadata = NULL,
        metadata = list()
      ),
      strand = new(
        "Rle",
        values = structure(integer(0), .Label = c("+",
                                                  "-", "*"), class = "factor"),
        lengths = integer(0),
        elementMetadata = NULL,
        metadata = list()
      ),
      seqinfo = new(
        "Seqinfo",
        seqnames = character(0),
        seqlengths = integer(0),
        is_circular = logical(0),
        genome = character(0)
      ),
      elementMetadata = new(
        "DFrame",
        rownames = NULL,
        nrows = 0L,
        listData = structure(list(), .Names = character(0)),
        elementType = "ANY",
        elementMetadata = NULL,
        metadata = list()
      ),
      elementType = "ANY",
      metadata = list()
    ),
    elementMetadata = new(
      "DFrame",
      rownames = NULL,
      nrows = 50L,
      listData = structure(list(), .Names = character(0)),
      elementType = "ANY",
      elementMetadata = new(
        "DFrame",
        rownames = NULL,
        nrows = 0L,
        listData = list(type = character(0), description = character(0)),
        elementType = "ANY",
        elementMetadata = NULL,
        metadata = list()
      ),
      metadata = list()
    ),
    elementType = "GRanges",
    metadata = list(),
    partitioning = new(
      "PartitioningByEnd",
      end = c(
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
      ),
      NAMES = c(
        "0610005C13Rik", "0610007C21Rik", "0610007L01Rik", "0610007N19Rik",
        "0610007P08Rik", "0610007P14Rik", "0610007P22Rik", "0610008F07Rik",
        "0610009B14Rik", "0610009B22Rik", "0610009D07Rik", "0610009L18Rik",
        "0610009O20Rik", "0610010B08Rik", "0610010F05Rik", "0610010K14Rik",
        "0610010O12Rik", "0610011F06Rik", "0610011L14Rik", "0610012G03Rik",
        "0610012H03Rik", "0610030E20Rik", "0610031J06Rik", "0610031O16Rik",
        "0610037L13Rik", "0610037P05Rik", "0610038B21Rik", "0610038L08Rik",
        "0610039K10Rik", "0610040B10Rik", "0610040F04Rik", "0610040J01Rik",
        "0610043K17Rik", "0910001L09Rik", "100043387", "1100001G20Rik",
        "1110001A16Rik", "1110001J03Rik", "1110002B05Rik", "1110002L01Rik",
        "1110002N22Rik", "1110003E01Rik", "1110004E09Rik", "1110004F10Rik",
        "1110005A03Rik", "1110006O24Rik", "1110007C09Rik", "1110008F13Rik",
        "1110008J03Rik", "1110008L16Rik"
      ),
      elementType = "ANY",
      elementMetadata = NULL,
      metadata = list()
    )
  ),
  colData = new(
    "DFrame",
    rownames = c(
      "IM_MR30_S30_L001", "IM_MR31_S31_L001", "IM_MR32_S32_L001",
      "IM_MR33_S33_L001", "IM_MR34_S34_L001", "IM_MR35_S35_L001",
      "IM_MR36_S36_L001", "IM_AR_30", "IM_AR_31", "IM_AR_32", "IM_AR_33"
    ),
    nrows = 11L,
    listData = list(
      batch = structure(
        c(1L, 1L,
          1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L),
        .Label = c(
          "190701_K00242_0579_AH7GFLBBXY",
          "180810_K00242_0453_AHVJTJBBXX-IM-AR-RS21+180814_K00242_0456_AHW5LYBBXX-IM-AR-RS21+180817_K00242_0458_AHW57TBBXX-IM-AR-RS21",
          "180406_K00242_0385_AHTCCHBBXX-IM-AR-RS8+180412_K00242_0388_AHT7NYBBXX-IM-AR-RS8",
          "180814_K00242_0456_AHW5LYBBXX-IM-AR-RS21+180814_K00242_0456_AHW5LYBBXX-IM-AR-RS21+180817_K00242_0458_AHW57TBBXX-IM-AR-RS21",
          "190322_K00242_0534_AHYLVWBBXX_IM-AM-RS8-dT"
        ),
        class = "factor"
      ),
      time_label = c( "D8", "D10", "D10", "D10", "D15", "D15", "D15", "D6",
      "D6", "D6", "D6"
      ),
      time_value = c(8L, 10L,
                     10L, 10L, 15L, 15L, 15L, 6L, 6L, 6L, 6L),
      user = c("MR",
               "MR", "MR", "MR", "MR", "MR", "MR", "AR", "AR", "AR",
               "AR"),
      treatment = structure(
        c(1L, 1L, 1L, 1L, 1L, 1L,
          1L, 2L, 2L, 2L, 1L),
        .Label = c("ctrl", "OE"),
        class = "factor"
      ),
      group = structure(
        c(10L, 11L, 11L, 11L, 12L, 12L, 12L,
          13L, 13L, 13L, 14L),
        .Label = c(
          "M_2i_ctrl", "M_D0_ctrl", "M_D1_ctrl", "M_D2_ctrl", "M_D3_ctrl",
          "M_D4_ctrl", "M_D5_ctrl", "M_D6_ctrl", "M_D7_ctrl", "M_D8_ctrl",
          "M_D10_ctrl", "M_D15_ctrl", "A_D6_OE", "A_D6_ctrl", "A_D7_ctrl",
          "A_D7_OE", "A_D8_ctrl", "A_D8_OE", "A_D11_ctrl", "A_D11_OE"
        ),
        class = "factor"
      ),
      group_pca_label = c(
        NA, "M_D10_ctrl", NA, NA, "M_D15_ctrl", NA, NA, "A_D6_OE", NA, NA,
        "A_D6_ctrl"
      ),
      condition = structure(
        c(10L,
          11L, 11L, 11L, 13L, 13L, 13L, 14L, 14L, 14L, 8L),
        .Label = c(
          "2i_ctrl", "D0_ctrl", "D1_ctrl", "D2_ctrl", "D3_ctrl", "D4_ctrl",
          "D5_ctrl", "D6_ctrl", "D7_ctrl", "D8_ctrl", "D10_ctrl", "D11_ctrl",
          "D15_ctrl", "D6_OE", "D7_OE", "D8_OE", "D11_OE"
        ),
        class = "factor"
      ),
      time = structure(
        c(10L, 11L, 11L,
          11L, 13L, 13L, 13L, 8L, 8L, 8L, 8L),
        .Label = c(
          "2i", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D10",
          "D11", "D15"
        ),
        class = "factor"
      )
    ),
    elementType = "ANY",
    elementMetadata = new(
      "DFrame",
      rownames = NULL,
      nrows = 9L,
      listData = list(
        type = c(
          "input", "input", "input", "input", "input", "input", "input",
          "input", "input"
        ),
        description = c("",
                        "", "", "", "", "", "", "", "")
      ),
      elementType = "ANY",
      elementMetadata = NULL,
      metadata = list()
    ),
    metadata = list()
  ),
  assays = new(
    "SimpleAssays",
    data = new(
      "SimpleList",
      listData = list(counts = structure(
        c(
          0L, 221L, 582L, 60L, 392L, 521L, 88L, 0L, 0L, 99L, 405L, 3L, 345L,
          0L, 221L, 151L, 4L, 63L, 227L, 117L, 11L, 376L, 406L, 0L, 143L,
          255L, 5L, 0L, 0L, 7L, 5L, 1L, 0L, 182L, 8L, 0L, 87L, 20L, 381L, 71L,
          58L, 331L, 56L, 589L, 36L, 2L, 73L, 254L, 55L, 95L, 0L, 381L, 691L,
          80L, 353L, 397L, 126L, 0L, 0L, 151L, 434L, 6L, 374L, 0L, 242L, 134L,
          43L, 113L, 232L, 130L, 23L, 325L, 458L, 0L, 165L, 358L, 4L, 0L, 0L,
          10L, 0L, 0L, 4L, 225L, 7L, 0L, 100L, 52L, 486L, 115L, 73L, 512L,
          86L, 639L, 50L, 3L, 108L, 273L, 64L, 105L, 0L, 261L, 588L, 65L,
          334L, 367L, 88L, 0L, 0L, 102L, 389L, 8L, 333L, 0L, 216L, 158L, 38L,
          87L, 254L, 87L, 24L, 302L, 434L, 0L, 159L, 292L, 4L, 0L, 2L, 7L, 1L,
          1L, 0L, 197L, 12L, 0L, 86L, 39L, 482L, 99L, 58L, 408L, 67L, 657L,
          43L, 1L, 73L, 242L, 63L, 68L, 0L, 271L, 579L, 88L, 298L, 383L, 80L,
          0L, 0L, 95L, 358L, 3L, 271L, 0L, 195L, 143L, 23L, 64L, 200L, 82L,
          27L, 334L, 382L, 0L, 170L, 296L, 4L, 0L, 0L, 4L, 0L, 1L, 1L, 171L,
          14L, 0L, 122L, 26L, 365L, 86L, 62L, 343L, 67L, 589L, 35L, 1L, 59L,
          251L, 39L, 71L, 0L, 332L, 566L, 112L, 330L, 282L, 82L, 0L, 0L, 107L,
          318L, 1L, 349L, 0L, 218L, 119L, 122L, 68L, 143L, 111L, 26L, 413L,
          540L, 0L, 124L, 315L, 0L, 0L, 1L, 6L, 0L, 1L, 3L, 235L, 13L, 0L,
          88L, 66L, 508L, 99L, 71L, 510L, 73L, 532L, 46L, 1L, 145L, 342L, 49L,
          60L, 0L, 297L, 598L, 100L, 283L, 278L, 58L, 0L, 0L, 115L, 306L, 4L,
          368L, 0L, 208L, 109L, 105L, 81L, 190L, 113L, 13L, 406L, 563L, 0L,
          115L, 280L, 5L, 0L, 0L, 4L, 0L, 1L, 2L, 238L, 22L, 0L, 78L, 75L,
          532L, 54L, 53L, 484L, 79L, 510L, 35L, 1L, 154L, 361L, 46L, 45L, 0L,
          413L, 724L, 102L, 352L, 325L, 77L, 0L, 0L, 87L, 364L, 8L, 398L, 0L,
          201L, 130L, 94L, 63L, 227L, 116L, 16L, 398L, 594L, 0L, 127L, 312L,
          4L, 0L, 0L, 8L, 0L, 0L, 0L, 285L, 12L, 0L, 99L, 61L, 638L, 56L, 44L,
          562L, 80L, 604L, 32L, 3L, 178L, 394L, 51L, 81L, 0L, 342L, 1147L,
          136L, 887L, 568L, 197L, 0L, 0L, 192L, 644L, 13L, 653L, 0L, 601L,
          286L, 36L, 98L, 478L, 190L, 8L, 706L, 694L, 0L, 229L, 306L, 11L, 0L,
          1L, 9L, 0L, 6L, 10L, 221L, 39L, 0L, 132L, 109L, 1132L, 350L, 133L,
          567L, 115L, 931L, 61L, 5L, 70L, 561L, 130L, 133L, 0L, 337L, 1162L,
          135L, 927L, 578L, 182L, 0L, 0L, 242L, 665L, 21L, 702L, 0L, 674L,
          307L, 35L, 122L, 475L, 176L, 8L, 653L, 714L, 0L, 251L, 312L, 11L,
          0L, 3L, 15L, 0L, 9L, 10L, 209L, 31L, 0L, 168L, 111L, 1231L, 383L,
          150L, 552L, 129L, 980L, 72L, 4L, 52L, 686L, 142L, 130L, 1L, 284L,
          1159L, 145L, 861L, 529L, 168L, 0L, 0L, 202L, 688L, 3L, 670L, 0L,
          654L, 298L, 27L, 122L, 465L, 197L, 15L, 707L, 657L, 0L, 223L, 284L,
          6L, 0L, 1L, 13L, 0L, 3L, 10L, 199L, 31L, 0L, 125L, 88L, 1142L, 397L,
          121L, 593L, 128L, 888L, 60L, 7L, 54L, 595L, 117L, 150L, 0L, 224L,
          1228L, 68L, 894L, 674L, 213L, 0L, 0L, 197L, 798L, 12L, 758L, 0L,
          446L, 285L, 21L, 75L, 458L, 185L, 5L, 738L, 456L, 0L, 261L, 339L,
          7L, 0L, 0L, 9L, 0L, 0L, 1L, 180L, 32L, 0L, 110L, 98L, 919L, 249L,
          134L, 387L, 188L, 1055L, 64L, 3L, 52L, 630L, 110L, 175L
        ),
        .Dim = c(50L, 11L),
        .Dimnames = list(
          c(
            "0610005C13Rik", "0610007C21Rik", "0610007L01Rik",
            "0610007N19Rik", "0610007P08Rik", "0610007P14Rik",
            "0610007P22Rik", "0610008F07Rik", "0610009B14Rik",
            "0610009B22Rik", "0610009D07Rik", "0610009L18Rik",
            "0610009O20Rik", "0610010B08Rik", "0610010F05Rik",
            "0610010K14Rik", "0610010O12Rik", "0610011F06Rik",
            "0610011L14Rik", "0610012G03Rik", "0610012H03Rik",
            "0610030E20Rik", "0610031J06Rik", "0610031O16Rik",
            "0610037L13Rik", "0610037P05Rik", "0610038B21Rik",
            "0610038L08Rik", "0610039K10Rik", "0610040B10Rik",
            "0610040F04Rik", "0610040J01Rik", "0610043K17Rik",
            "0910001L09Rik", "100043387", "1100001G20Rik", "1110001A16Rik",
            "1110001J03Rik", "1110002B05Rik", "1110002L01Rik",
            "1110002N22Rik", "1110003E01Rik", "1110004E09Rik",
            "1110004F10Rik", "1110005A03Rik", "1110006O24Rik",
            "1110007C09Rik", "1110008F13Rik", "1110008J03Rik", "1110008L16Rik"
          ),
          c(
            "IM_MR30_S30_L001", "IM_MR31_S31_L001", "IM_MR32_S32_L001",
            "IM_MR33_S33_L001", "IM_MR34_S34_L001", "IM_MR35_S35_L001",
            "IM_MR36_S36_L001", "IM_AR_30", "IM_AR_31", "IM_AR_32", "IM_AR_33"
          )
        )
      )),
      elementType = "ANY",
      elementMetadata = NULL,
      metadata = list()
    )
  ),
  NAMES = NULL,
  elementMetadata = new(
    "DFrame",
    rownames = NULL,
    nrows = 50L,
    listData = structure(list(), .Names = character(0)),
    elementType = "ANY",
    elementMetadata = NULL,
    metadata = list()
  ),
  metadata = list(version = structure(
    list(c(1L, 30L, 1L)), class = c("package_version",
                                    "numeric_version")
  ))
)

我们可以使用

data_bec <- dds_tmp %>% 
  {sva::ComBat_seq(
    counts = {.} %>% SummarizedExperiment::assay(), 
    batch = {.} %>% magrittr::extract2("batch") %>% droplevels(),
    covar_mod = cbind(
      time = {.} %>% magrittr::extract2("time") %>% droplevels(),
      treatment = {.} %>% magrittr::extract2("treatment") %>% droplevels()
    )
  )}