我遇到了一个bug,map_df似乎没有传递参数

问题描述 投票:0回答:1

我的场景涉及使用 imf.data 包从 IMF API 加载数据。我需要同时提取多个索引,它们的向量长度可能会有所不同。

library(tidyverse)
library(data.table)
library(imf.data)

# for instance, one possible indice vector is like follows
  index <- c(
    # FA - Direct Investment
    "BFDA_BP6_USD", "BFDAD_BP6_USD", "BFDAE_BP6_USD",
    # FA - Portfolio Investment
    "BFPA_BP6_USD", "BFPAD_BP6_USD", "BFPAE_BP6_USD" )

我定义了一个函数来促进提取这些索引:

getFAfromIFS <- function(iso2 = "US") {
  dta_IFS <- imf.data::load_datasets("IFS")
  # iso2 <- "US"
  index <- c(
    # FA - Direct Investment
    "BFDA_BP6_USD", "BFDAD_BP6_USD", "BFDAE_BP6_USD",
    # FA - Portfolio Investment
    "BFPA_BP6_USD", "BFPAD_BP6_USD", "BFPAE_BP6_USD"
  )
  
  each_dta.byeachISO2 <- map_df(index, function(xxx) 
  {
    # xxx <- index[1]
    print(paste0("...loading data from iso2=", iso2, " & index=", xxx))
    each_account <- dta_IFS$get_series(freq = "Q", ref_area = iso2, indicator = xxx) 
    print("... check? ") # <--- I found the function stops here every time!!!
    
    each_account %>%
      setDT() %>%
      setnames(c("time", "value", "Note")) %>%
      select(-Note) %>%
      .[, `:=` (ISO2 = iso2, 
                index = xxx)]
  }) %>%
    dcast( time + ISO2 ~ index, value.var = "value")
}

data1 <- getFAfromIFS(iso2 = "US")

它给了我一个错误,

Error in `map()`:
ℹ In index: 1.
Caused by error:
! object 'iso2' not found
Run `rlang::last_trace()` to see where the error occurred.

我很困惑。我猜这个错误可能来自

each_account <- dta_IFS$get_series(freq = "Q", ref_area = iso2, indicator = xxx) 
行。但我已经定义了
iso2
(来自函数
getFAfromIFS
的参数)和
xxx
(来自函数
map_df
的参数)。

我错过了什么?请帮我。谢谢!

r purrr
1个回答
0
投票

这与

purrr
map. In this case, the 
dta_IFS$get_series` 函数实际上没有任何关系,似乎有一个错误。该函数定义为

function (freq, ref_area, indicator, start_period = NULL, end_period = NULL) 
{
    x <- eval(as_list(match.call()))
    return(get0(x, "IFS"))
}

问题是

eval()
应该是
eval.parent()
。由于这不是您可以轻松更改的内容(包作者必须这样做),因此您可以创建一个解决方案。你可以换线

each_account <- dta_IFS$get_series(freq = "Q", ref_area = iso2, indicator = xxx) 

each_account <- do.call(dta_IFS$get_series, list(freq = "Q", indicator = xxx, ref_area = iso2))

这应该通过在执行之前强制评估函数参数来避免错误。

© www.soinside.com 2019 - 2024. All rights reserved.