简体   繁体   中英

How can I use items of a list as input for a function in a loop in R?

I am new to R as well as stackoverflow, so please forgive me my naivite and possible mistakes. My goal is to create a dataframe of transaction details for ethereum transactions. The function I use for a single transaction is:

library(devtools)
library(etherscanr)

devtools::install_github("dirkschumacher/etherscanr")
etherscan_transactions("0x3cd751e6b0078be393132286c442345e5dc49699")

The number in the brackets of the function is the wallet address for which I need the details. So far so good. This works just fine. However, I need to do this for thousands of addresses, which is why I want to loop this function over a list. This is where I am stuck and ask for your help. I tried different approaches including lapply and for loops, looking something like that:

transto <- transaction_sample$to

for(i in 1:length(transto)) {
  transtodetail <- etherscan_transactions(i)
}

This is the error I get: Error in etherscan_transactions(i) : is.character(account) is not TRUE

This is what the list of wallet adrresses looks like:

 head(transto)
[1] "0x3cd751e6b0078be393132286c442345e5dc49699"
[2] "0xdac17f958d2ee523a2206206994597c13d831ec7"
[3] "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599"
[4] "0xdac17f958d2ee523a2206206994597c13d831ec7"
[5] "0xdac17f958d2ee523a2206206994597c13d831ec7"
[6] "0xdac17f958d2ee523a2206206994597c13d831ec7"
    

I am grateful for any type of hints.

  #' The data:
transto <- c("0x3cd751e6b0078be393132286c442345e5dc49699",
             "0xdac17f958d2ee523a2206206994597c13d831ec7",
             "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
             "0xdac17f958d2ee523a2206206994597c13d831ec7",
             "0xdac17f958d2ee523a2206206994597c13d831ec7",
             "0xdac17f958d2ee523a2206206994597c13d831ec7")

Using a modified/fake version of etherscan_transactions tells you what has been happening:

fake_etherscan_transactions <- function(i_arg) print(i_arg)

for(i in 1:length(transto)) {
  transtodetail <- fake_etherscan_transactions(i)
}
#> [1] 1
#> [1] 2
#> [1] 3
#> [1] 4
#> [1] 5
#> [1] 6

You passed the index to etherscan_transactions() instead of the value at each index.

Here are some solutions. Just replace fake_etherscan_transactions with etherscan_transactions . Solution 1: use the index

for(i in 1:length(transto)) {
  transtodetail <- fake_etherscan_transactions(transto[i])
}
#> [1] "0x3cd751e6b0078be393132286c442345e5dc49699"
#> [1] "0xdac17f958d2ee523a2206206994597c13d831ec7"
#> [1] "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599"
#> [1] "0xdac17f958d2ee523a2206206994597c13d831ec7"
#> [1] "0xdac17f958d2ee523a2206206994597c13d831ec7"
#> [1] "0xdac17f958d2ee523a2206206994597c13d831ec7"

Solution 2: use transto as vector in the for loop

for(i in transto) {
  transtodetail <- fake_etherscan_transactions(i)
}
#> [1] "0x3cd751e6b0078be393132286c442345e5dc49699"
#> [1] "0xdac17f958d2ee523a2206206994597c13d831ec7"
#> [1] "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599"
#> [1] "0xdac17f958d2ee523a2206206994597c13d831ec7"
#> [1] "0xdac17f958d2ee523a2206206994597c13d831ec7"
#> [1] "0xdac17f958d2ee523a2206206994597c13d831ec7"

Solution 3: use sapply over transto (as proposed by @Limey).

sapply(transto, fake_etherscan_transactions)

Created on 2022-06-11 by the reprex package (v2.0.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM