Reputation: 1158
I'm trying to get into functional programming which I can get to work with a simple 1 dimensional list, but if I try to pack the list with objects it starts to break. Part of the program is using writing to file with dput and I was unable to get dput to work inside the first function call, so I created another function just to handle the dput call (put_symbols_into_file).
I had an issue with dput in my batch_get_symbols function, so I thought I'd simply nest it, which works by itself, but I'd like to create multiple markets (bonds, stocks, mutual funds) as parameter lists and run lapply on them.
I suppose if I could accomodate dput's fil parameter in batch_get_symbols (such as with a %>%) I'd be good to go, but I'd really like to know how to do this as a nested function for future nesting. If there is a way to do this with a class constructor (I haven't done classes yet), then that would be fine. I find reading classes in R a little confusing.
I know the problem is scope (i.e. doesn't recognize data element). The first function calls the second and the second function has no idea what the parameter terms are for some reason.
library(HelpersMG)
library(BatchGetSymbols)
library(future)
library(data.table)
library(quantmod)
BetaTestCoefficient = .25
wget("ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqtraded.txt")
wget("ftp://ftp.nasdaqtrader.com/SymbolDirectory/mfundslist.txt")
#9 quarters is 5479/8897 61% (60%)
nasdaqTraded <- as.character(head(read.csv("nasdaqtraded.txt",sep="|")$Symbol,-2))
mfunds <- as.character(head(read.csv("nasdaqtraded.txt",sep="|")$Symbol,-2))
put_symbols_into_file <- function(fil,data,size) {
dput(batch_get_symbols(data,size),fil)
}
batch_get_symbols <- function(data,size) {
BatchGetSymbols(tickers = sample(data,size*betaTestCoefficient),
do.parallel = TRUE,
first.date = first.date,
last.date = last.date,
be.quiet = TRUE,
#cache results in "can only subtract from "Date" objects"
#probably due to parallel
do.cache=FALSE)
}
fil_Nasdaq <- c()
fil_Nasdaq <- tempfile()
#mfunds
fil_mfunds <- c()
fil_mfunds <- tempfile()
first.date <- Sys.Date() - 821
last.date <- Sys.Date() - 814
list_nasdaq <- list(fil_Nasdaq,nasdaqTraded,770)
list_mfunds <- list(fil_mfunds,mfunds,324)
mylists <- list(list_nasdaq, list_mfunds)
lapply(mylists, sapply, put_symbols_into_file)
#Or...
lapply(mylists,put_symbols_into_file)
Error
lapply(mylists,put_symbols_into_file)
Error in batch_get_symbols(data, size) :
argument "data" is missing, with no default
this works however, showing me the nesting is somewhat working, but it won't work with lapply
put_symbols_into_file(fil_Nasdaq,nasdaqTraded,770)
Note: I had to do as.character() else when I created my lists, the values (symbol names) were converted to integer's
Upvotes: 0
Views: 114
Reputation: 18653
You can try the following:
lapply(mylists, FUN=function(x) put_symbols_into_file(fil=x[[1]], data=x[[2]], size=x[[3]]))
Or you may prefer to name your list elements first to facilitate the task.
list_nasdaq <- list(fil=fil_Nasdaq, data=nasdaqTraded, size=770)
list_mfunds <- list(fil=fil_mfunds, data=mfunds, size=324)
mylists <- list(list_nasdaq, list_mfunds)
str(mylists)
List of 2
$ :List of 3
..$ fil : chr "C:\\Temp\\RtmpO4ojSu\\file12a8471239eb"
..$ data: chr [1:8897] "A" "AA" "AAAU" "AACG" ...
..$ size: num 770
$ :List of 3
..$ fil : chr "C:\\Temp\\RtmpO4ojSu\\file12a8215c3d94"
..$ data: chr [1:8897] "A" "AA" "AAAU" "AACG" ...
..$ size: num 324
lapply(mylists, FUN=function(x) put_symbols_into_file(fil=x$fil, data=x$data, size=x$size))
I'm wondering if there's another solution using mapply
.
Upvotes: 1