如何在 R Plumber 中获取客户端 IP 地址

How to get client IP address in R Plumber

我检查过 GitHub Repo and doc 但仍然无法弄清楚如何在 Plumber 中获取客户端 IP。

这是我尝试过的实现,我想将所有请求的 IP 地址添加到日志文件中,

#' @post /v1/rl
rl_v1 <- function(a, b, c){
  request='rl'
  start_time <- as.numeric(as.POSIXct(Sys.time()))
  log_record <- paste(NULL, Sys.time(), request, "requested", NULL, NULL, 
                  sep=",")
  cat(paste(log_record, "\n", sep=""), file=log_file_name, append=T)

  lhs <- data.frame(a=unlist(a),
                b=unlist(b),
                c=unlist(c))

  pairs <- custom_function(lhs, rhs, m_w = 0.98,
                                ext_blk_field=c(12), international=T,
                                fasterWcoBlock=T, preprocessedData2=T)
  input_records=nrow(lhs)
  matches=nrow(pairs)
  query_time <- as.numeric(as.POSIXct(Sys.time())) - start_time
  status <- data.frame(query_time=query_time, 
                   request=request, 
                   type='POST',
                   api_version=api_version_v1)

  log_record <- paste(NULL, Sys.time(), request, "responded", 
                  round(matches/input_records*100, 2), 
                  paste0(matches, '/', input_records, ' in ', query_time), 
                  sep=",")
  cat(paste(log_record, "\n", sep=""), file=log_file_name, append=T)

  return(list(data=pairs, status=status))
}

非常感谢任何帮助。

为了结束这个问题,我将重申评论:

Since plumber uses httpuv, it's possible you can reach the req$REMOTE_ADDR property of the request handle.