簡體   English   中英

管道工錯誤:throw_if_func_is_not_a_function(private$func)

[英]Plumber Error: throw_if_func_is_not_a_function(private$func)

我正在使用一個簡單的 for 循環來計算一些數學公式。 最終我想在 web 路線上看到結果。 因此,為此我開始使用水管工,但它顯示錯誤。 R 腳本如下所示:

postgres.R

#* Get the progress of the script
#* @get /progress
# #* @param s
# library(plumber)
require(svMisc)

n <- 2 ^ 20
inside <- 0
total <- 0

function(){
  for (i in 1:100){
    x <- runif(n)
    y <- runif(n)
    r <- x ^ 2 + y ^ 2
    inside <- inside + sum(r <= 1)
    total <- total + n
    # print(inside * 4 / total)
    progress(i)
    if(i == 100) message("\nDone!")
  }
  message("function executed!")
}

我正在使用以下命令運行它:

library(plumber)
r<-plumb("postgres.R")

並在 RStudio 控制台中收到此錯誤:

Error in throw_if_func_is_not_a_function(private$func) : 
  `expr` did not evaluate to a function

顯示此錯誤是因為在 API 端點定義行(以 #* 開頭)和 function 定義function() {之間存在代碼。 當我啟動prosgres.R單擊 RStudio 中的“運行 API”按鈕時,以下代碼工作正常

require(svMisc)

n <- 2 ^ 20
inside <- 0
total <- 0

#* Get the progress of the script
#* @get /progress
# #* @param s
function(){
  for (i in 1:100){
    x <- runif(n)
    y <- runif(n)
    r <- x ^ 2 + y ^ 2
    inside <- inside + sum(r <= 1)
    total <- total + n
    # you do not capture this output
    progress(i)
    if(i == 100) message("\nDone!")
  }
  message("function executed!")
  # this is what will be returned by the API 
  list(total = total, indide = inside, check = inside * 4 / total)
}

請注意,我包含了一個端點返回的列表,因此您可以在 web Swagger UI 中看到結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM