为什么我需要调用两次使用invisible()返回的对象才能打印?

时间:2018-05-12 10:12:11

标签: r data.table

从文档中我读到了invisible() returns a (temporarily) invisible copy of an object。现在,当我使用隐形时,我总是需要在实际打印之前调用对象两次。

我使用data.table并希望我的函数返回一个对象的不可见副本,因为满足某个条件(即函数的过早中止)。

我注意到,如果在另一个函数中使用了隐形返回的对象,那么“需要double / two调用”的这种行为也适用,使得它的使用看似无法使用。是什么导致这种行为?难道我做错了什么?如何让函数无形地返回,并在第一次调用时打印?

请参阅以下示例代码:

example <- function(DT) {

  if (!(1 %in% DT$RSI.verticalBottom) | !(1 %in% DT$RSI.top)) {

    # abort if there is no buy or sell signal
    DT[, `:=`(pos = NA,
              return = NA
              )]

    return(invisible(DT))
  }

> example(sample.data)
> sample.data
> sample.data
           conm   tic   datadate  cshoq   gind year month yearmon fdateq pdateq fyr fyearq fqtr
1: NS GROUP INC NSS.1 2000-01-31     NA 101010 2000     1  2000_1     NA   <NA>  NA     NA   NA
2: NS GROUP INC NSS.1 2000-02-29     NA 101010 2000     2  2000_2     NA   <NA>  NA     NA   NA
3: NS GROUP INC NSS.1 2000-03-31 21.533 101010 2000     3  2000_3     NA   <NA>   9   2000    2
4: NS GROUP INC NSS.1 2000-04-30     NA 101010 2000     4  2000_4     NA   <NA>  NA     NA   NA
5: NS GROUP INC NSS.1 2000-05-31     NA 101010 2000     5  2000_5     NA   <NA>  NA     NA   NA
6: NS GROUP INC NSS.1 2000-06-30 22.008 101010 2000     6  2000_6     NA   <NA>   9   2000    3
       req epspiq epspxq ajexq   saleq   saley ivncfy  gsubind   dpq ibmiiq    ibq     iby oiadpq
1:      NA     NA     NA    NA      NA      NA     NA       NA    NA     NA     NA      NA     NA
2:      NA     NA     NA    NA      NA      NA     NA       NA    NA     NA     NA      NA     NA
3: -58.396  -0.38  -0.38     1 100.107 186.733  10.77 10101020 5.517     NA -8.231 -21.165 -5.617
4:      NA     NA     NA    NA      NA      NA     NA       NA    NA     NA     NA      NA     NA
5:      NA     NA     NA    NA      NA      NA     NA       NA    NA     NA     NA      NA     NA
6: -63.168  -0.19  -0.23     1  73.652 260.385  20.90 10101020    NA     NA -5.048 -26.213     NA
    oiadpy oibdpq oibdpy xiq   xoprq   cogsy dlcchy wcapchy  QEBIT.adep  YEBIT.adep    QEBIT.bdep
1:      NA     NA     NA  NA      NA      NA     NA      NA          NA          NA            NA
2:      NA     NA     NA  NA      NA      NA     NA      NA          NA          NA            NA
3: -16.924   -0.1  -5.57   0 100.207 177.826 -0.394      NA -0.05610996 -0.09063208 -0.0009989311
4:      NA     NA     NA  NA      NA      NA     NA      NA          NA          NA            NA
5:      NA     NA     NA  NA      NA      NA     NA      NA          NA          NA            NA
6:      NA     NA     NA   0      NA      NA -0.394      NA          NA          NA            NA
     YEBIT.bdep        QEBT       YEBT   f_id I.QSales  IWA.QEBIT   IWA.QEBT I.YSales  IWA.YEBIT
1:           NA          NA         NA     NA       NA         NA         NA       NA         NA
2:           NA          NA         NA     NA       NA         NA         NA       NA         NA
3: -0.000535524 -0.08222202 -0.1133437 2000Q2 19344.53 0.08160277 0.03577741 196223.7 0.08329726
4:           NA          NA         NA     NA       NA         NA         NA       NA         NA
5:           NA          NA         NA     NA       NA         NA         NA       NA         NA
6:           NA -0.06853853 -0.1006702 2000Q3 19798.64 0.10680607 0.06096211 196223.7 0.08329726
     IWA.YEBT   QSales.pc    YSales.pc RSI_QEBIT RSI_QEBT RSI_IWA.QEBIT RSI_IWA.QEBT adj.factor
1:         NA          NA           NA        NA       NA            NA           NA          1
2:         NA          NA           NA        NA       NA            NA           NA          1
3: 0.03875869 0.005174952 0.0009516334  41.45963 32.93934      29.96487     18.23527          1
4:         NA          NA           NA        NA       NA            NA           NA          1
5:         NA          NA           NA        NA       NA            NA           NA          1
6: 0.03875869 0.003720053 0.0013269806  49.83110 34.64800      37.58678     24.75847          1
   dvpsxm  cshtrm curcdm   close    high     low   trfm   trt1m close.unAdj   mktcap close.div
1:     NA 4557500    USD  8.8750 10.1250  6.7500 1.0409 16.3934      8.8750       NA    8.8750
2:     NA 4506100    USD 11.6875 12.1250  8.0625 1.0409 31.6901     11.6875       NA   11.6875
3:     NA 4146200    USD 16.3125 16.8125 11.3750 1.0409 39.5722     16.3125 351.2571   16.3125
4:     NA 3215400    USD 15.8750 16.3750 12.8750 1.0409 -2.6820     15.8750       NA   15.8750
5:     NA 2948800    USD 18.3125 19.3750 16.0625 1.0409 15.3543     18.3125       NA   18.3125
6:     NA 4296100    USD 20.9375 21.0000 17.7500 1.0409 14.3345     20.9375 460.7925   20.9375
   RSI_close RSI.verticalBottom RSI.top return pos
1:        NA                 NA      NA     NA  NA
2:        NA                 NA      NA     NA  NA
3:        NA                 NA      NA     NA  NA
4:        NA                 NA      NA     NA  NA
5:        NA                 NA      NA     NA  NA
6:        NA                 NA      NA     NA  NA

示例数据

> dput(sample.data)
structure(list(conm = c("NS GROUP INC", "NS GROUP INC", "NS GROUP INC", 
"NS GROUP INC", "NS GROUP INC", "NS GROUP INC"), tic = c("NSS.1", 
"NSS.1", "NSS.1", "NSS.1", "NSS.1", "NSS.1"), datadate = structure(c(10987, 
11016, 11047, 11077, 11108, 11138), class = "Date"), cshoq = c(NA, 
NA, 21.533, NA, NA, 22.008), gind = c(101010L, 101010L, 101010L, 
101010L, 101010L, 101010L), year = c(2000, 2000, 2000, 2000, 
2000, 2000), month = c(1, 2, 3, 4, 5, 6), yearmon = c("2000_1", 
"2000_2", "2000_3", "2000_4", "2000_5", "2000_6"), fdateq = c(NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
), pdateq = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), class = "Date"), fyr = c(NA, NA, 9L, NA, 
NA, 9L), fyearq = c(NA, NA, 2000L, NA, NA, 2000L), fqtr = c(NA, 
NA, 2L, NA, NA, 3L), req = c(NA, NA, -58.396, NA, NA, -63.168
), epspiq = c(NA, NA, -0.38, NA, NA, -0.19), epspxq = c(NA, NA, 
-0.38, NA, NA, -0.23), ajexq = c(NA, NA, 1, NA, NA, 1), saleq = c(NA, 
NA, 100.107, NA, NA, 73.652), saley = c(NA, NA, 186.733, NA, 
NA, 260.385), ivncfy = c(NA, NA, 10.77, NA, NA, 20.9), gsubind = c(NA, 
NA, 10101020L, NA, NA, 10101020L), dpq = c(NA, NA, 5.517, NA, 
NA, NA), ibmiiq = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), ibq = c(NA, NA, -8.231, NA, NA, -5.048), iby = c(NA, 
NA, -21.165, NA, NA, -26.213), oiadpq = c(NA, NA, -5.617, NA, 
NA, NA), oiadpy = c(NA, NA, -16.924, NA, NA, NA), oibdpq = c(NA, 
NA, -0.1, NA, NA, NA), oibdpy = c(NA, NA, -5.57, NA, NA, NA), 
    xiq = c(NA, NA, 0, NA, NA, 0), xoprq = c(NA, NA, 100.207, 
    NA, NA, NA), cogsy = c(NA, NA, 177.826, NA, NA, NA), dlcchy = c(NA, 
    NA, -0.394, NA, NA, -0.394), wcapchy = c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), QEBIT.adep = c(NA, 
    NA, -0.0561099623402959, NA, NA, NA), YEBIT.adep = c(NA, 
    NA, -0.0906320789576561, NA, NA, NA), QEBIT.bdep = c(NA, 
    NA, -0.000998931143676266, NA, NA, NA), YEBIT.bdep = c(NA, 
    NA, -0.000535523983441598, NA, NA, NA), QEBT = c(NA, NA, 
    -0.0822220224359935, NA, NA, -0.0685385325585184), YEBT = c(NA, 
    NA, -0.113343651095414, NA, NA, -0.100670161491637), f_id = c(NA, 
    NA, "2000Q2", NA, NA, "2000Q3"), I.QSales = c(NA, NA, 19344.526, 
    NA, NA, 19798.641), IWA.QEBIT = c(NA, NA, 0.0816027748625115, 
    NA, NA, 0.10680606815387), IWA.QEBT = c(NA, NA, 0.0357774080378087, 
    NA, NA, 0.0609621135107203), I.YSales = c(NA, NA, 196223.665, 
    NA, NA, 196223.665), IWA.YEBIT = c(NA, NA, 0.0832972567299668, 
    NA, NA, 0.0832972567299668), IWA.YEBT = c(NA, NA, 0.0387586889685299, 
    NA, NA, 0.0387586889685299), QSales.pc = c(NA, NA, 0.00517495233535316, 
    NA, NA, 0.00372005331072976), YSales.pc = c(NA, NA, 0.000951633433204909, 
    NA, NA, 0.00132698061673652), RSI_QEBIT = c(NA, NA, 41.4596290506163, 
    NA, NA, 49.8310957229999), RSI_QEBT = c(NA, NA, 32.939339100869, 
    NA, NA, 34.6480049470139), RSI_IWA.QEBIT = c(NA, NA, 29.9648696052066, 
    NA, NA, 37.5867809473848), RSI_IWA.QEBT = c(NA, NA, 18.2352737965041, 
    NA, NA, 24.7584711404174), adj.factor = c(1, 1, 1, 1, 1, 
    1), dvpsxm = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), cshtrm = c(4557500, 4506100, 4146200, 3215400, 
    2948800, 4296100), curcdm = c("USD", "USD", "USD", "USD", 
    "USD", "USD"), close = c(8.875, 11.6875, 16.3125, 15.875, 
    18.3125, 20.9375), high = c(10.125, 12.125, 16.8125, 16.375, 
    19.375, 21), low = c(6.75, 8.0625, 11.375, 12.875, 16.0625, 
    17.75), trfm = c(1.0409, 1.0409, 1.0409, 1.0409, 1.0409, 
    1.0409), trt1m = c(16.3934, 31.6901, 39.5722, -2.682, 15.3543, 
    14.3345), close.unAdj = c(8.875, 11.6875, 16.3125, 15.875, 
    18.3125, 20.9375), mktcap = c(NA, NA, 351.2570625, NA, NA, 
    460.7925), close.div = c(8.875, 11.6875, 16.3125, 15.875, 
    18.3125, 20.9375), RSI_close = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), RSI.verticalBottom = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), RSI.top = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), return = c(NA, 
    NA, NA, NA, NA, NA), pos = c(NA, NA, NA, NA, NA, NA)), .Names = c("conm", 
"tic", "datadate", "cshoq", "gind", "year", "month", "yearmon", 
"fdateq", "pdateq", "fyr", "fyearq", "fqtr", "req", "epspiq", 
"epspxq", "ajexq", "saleq", "saley", "ivncfy", "gsubind", "dpq", 
"ibmiiq", "ibq", "iby", "oiadpq", "oiadpy", "oibdpq", "oibdpy", 
"xiq", "xoprq", "cogsy", "dlcchy", "wcapchy", "QEBIT.adep", "YEBIT.adep", 
"QEBIT.bdep", "YEBIT.bdep", "QEBT", "YEBT", "f_id", "I.QSales", 
"IWA.QEBIT", "IWA.QEBT", "I.YSales", "IWA.YEBIT", "IWA.YEBT", 
"QSales.pc", "YSales.pc", "RSI_QEBIT", "RSI_QEBT", "RSI_IWA.QEBIT", 
"RSI_IWA.QEBT", "adj.factor", "dvpsxm", "cshtrm", "curcdm", "close", 
"high", "low", "trfm", "trt1m", "close.unAdj", "mktcap", "close.div", 
"RSI_close", "RSI.verticalBottom", "RSI.top", "return", "pos"
), sorted = c("conm", "tic", "datadate", "cshoq", "gind", "year", 
"month", "yearmon"), class = c("data.table", "data.frame"), row.names = c(NA, 
-6L), .internal.selfref = <pointer: 0x102806978>)

0 个答案:

没有答案