在 R 中的函数末尾干净地打印列表

Printing a list cleanly at the end of a function in R

这行得通,但正如您从输出中看到的那样,最后有一点垃圾。如何做到这一点以摆脱最后的垃圾,即 [[1]] : 无效的 :

#+tblname: prob-calc
| Price              | 353.02 |
| Target price       | 398.00 |
| IV                 |  241.0 |
| Days to expiration |      1 |

#+begin_src R :var tbl=prob-calc   :results output :exports none :session
price = tbl[1,2]
target_price = tbl[2,2]
iv = tbl[3,2]
days = tbl[4,2]
prob_calc <- function(price, target_price, iv, days)
{
    one_sd_move = (price * iv/100 * sqrt(days)) / sqrt(365)
    prob_price_above_target = 1-pnorm((log(target_price/price))/((iv/100)*sqrt(days/365)))
    prob_price_below_target = pnorm(log(target_price/price)/((iv/100)*sqrt(days/365)))
    one_sd_above = price + one_sd_move
    one_sd_below = price - one_sd_move
    ## This won't work, only the last line will be printed
    ## sprintf("One standard deviation move: %f", one_sd_move)
    ## sprintf("Probability stock price closing above the target: %f", prob_price_above_target)
    ## sprintf("Probability stock price closing below the target: %f", prob_price_below_target)
    ## sprintf("One std dev above the current price: %f", one_sd_above)
    ## sprintf("One std dev below the current price: %f", one_sd_below)
    ## Instead, do this:
    return(list(cat("One standard deviation move: ", one_sd_move, "\n",
        "Probability stock price closing above the target: ", prob_price_above_target, "\n",
        "Probability stock price closing below the target: ", prob_price_below_target, "\n",
        "One std dev above the current price: ", one_sd_above, "\n",
        "One std dev below the current price: ", one_sd_below)))
}
prob_calc(price, target_price, iv, days)
#+end_src

#+RESULTS:
: One standard deviation move:  44.53177 
:  Probability stock price closing above the target:  0.1708762 
:  Probability stock price closing below the target:  0.8291238 
:  One std dev above the current price:  397.5518 
:  One std dev below the current price:  308.4882[[1]]
: NULL

据我所知,这里不需要 list()return() 语句;但要摆脱 [[1]] NULL 只需将表达式包装在 invisible 中:

prob_calc <- function(price, target_price, iv, days) {
  one_sd_move = (price * iv/100 * sqrt(days)) / sqrt(365)
  prob_price_above_target = 1-pnorm((log(target_price/price))/((iv/100)*sqrt(days/365)))
  prob_price_below_target = pnorm(log(target_price/price)/((iv/100)*sqrt(days/365)))
  one_sd_above = price + one_sd_move
  one_sd_below = price - one_sd_move

  invisible(
    cat("One standard deviation move: ", one_sd_move, "\n",
         "Probability stock price closing above the target: ", 
          prob_price_above_target, "\n",
         "Probability stock price closing below the target: ", 
          prob_price_below_target, "\n",
         "One std dev above the current price: ", 
          one_sd_above, "\n",
         "One std dev below the current price: ", one_sd_below))
}
##
R> prob_calc(price, target_price, iv, days)
One standard deviation move:  44.53177 
 Probability stock price closing above the target:  0.1708762 
 Probability stock price closing below the target:  0.8291238 
 One std dev above the current price:  397.5518 
 One std dev below the current price:  308.4882

此外,如果您将所有内容都放在同一个文本字符串中,您 可以 使用 sprintf,使用 paste0 可以更干净地完成 -

prob_calc2 <- function(price, target_price, iv, days) {
  one_sd_move = (price * iv/100 * sqrt(days)) / sqrt(365)
  prob_price_above_target = 1-pnorm((log(target_price/price))/((iv/100)*sqrt(days/365)))
  prob_price_below_target = pnorm(log(target_price/price)/((iv/100)*sqrt(days/365)))
  one_sd_above = price + one_sd_move
  one_sd_below = price - one_sd_move

  txt <- paste0(
    "One standard deviation move: %f\n",
    "Probability stock price closing above the target: %f\n",
    "Probability stock price closing below the target: %f\n",
    "One std dev above the current price: %f\n",
    "One std dev below the current price: %f\n",
    collapse = "")

  cat(sprintf(
    txt, one_sd_move, prob_price_above_target, 
    prob_price_below_target,one_sd_above, one_sd_below))
}
##
R> prob_calc2(price, target_price, iv, days)
One standard deviation move: 44.531766
Probability stock price closing above the target: 0.170876
Probability stock price closing below the target: 0.829124
One std dev above the current price: 397.551766
One std dev below the current price: 308.488234

数据:

price <- 353.02
target_price <- 398.00
iv <- 241.00
days <- 1