r - paste doesn't recognize object -
i'm trying run function, when try compile it, says:
error in paste("http://uk.advfn.com/p.php?pid=financials&symbol=", symbol, : object 'symbol' not found fund.data <- function ( symbol, # ticker n=10, # number of periods mode=c('quarterly','annual'), # periodicity max.attempts=5 # maximum number of attempts download before exiting ) dirname(sys.frame(1)$ofile) { all.data = c() option.value = -1 start_date = c('istart_date,start_date') names(start_date) = c('quarterly,annual') repeat { # download quarterly financial report data if(option.value >= 0) { url = paste('http://uk.advfn.com/p.php?pid=financials&symbol=', symbol, '&btn=', mode[1], '_reports&', start_date[mode[1]], '=', option.value, sep = '') } else { url = paste('http://uk.advfn.com/p.php?pid=financials&symbol=', symbol, '&btn=', mode[1], '_reports', sep = '') } cat('downloading', url, '\n') #txt = join(readlines(url)) for(iattempt in 1:max.attempts) { flag = t trycatch({ txt = join(readlines(url)) }, interrupt = function(ex) { flag <<- f sys.sleep(0.1) }, error = function(ex) { flag <<- f sys.sleep(0.1) }, = { if(flag) break }) } if( length(grep('indicators', txt, ignore.case = t)) == 0 ) { cat('no data found for', symbol, '\n') return(all.data) } # title pos = regexpr(pattern = '<title>(.*?)</title>', txt, ignore.case = true, perl = true) if(length(pos) == 1) title = substr(txt, attr(pos, 'capture.start'), attr(pos, 'capture.start') + attr(pos, 'capture.length') - 1) # extract table page data = extract.table.from.webpage(txt, 'indicators', has.header = t) colnames(data) = data[1,] rownames(data) = data[,1] data = data[,-1,drop=f] # add not present data add.index = which( is.na(match( colnames(data), colnames(all.data) )) ) all.data = cbind(data[,add.index,drop=f], all.data) # check if time stop if(ncol(all.data) >= n) break if(option.value == 0) break # extract option value go next page temp = gsub(pattern = '<option', replacement = '<tr>', txt, perl = true) temp = gsub(pattern = '</option>', replacement = '</tr>', temp, perl = true) temp = extract.table.from.webpage(temp, 'all amounts', has.header = t) temp = apply(temp,1,join) index.selected = grep('selected', temp) option.value = 0 if( length(index.selected) ) option.value = as.double( gsub('.*value=\'([0-9]*).*', '\\1', temp[index.selected]) ) if(option.value > 0) { # can 5 time periods @ time option.value = option.value - 5 option.value = max(0, option.value) } else { break } } # remove empty columns all.data = all.data[, colsums(nchar(trim(all.data))) > 0, drop=f] all.data = rbind(all.data, title) rownames(all.data)[nrow(all.data)] = 'htmltitletext' if( ncol(all.data) > n ) { return(all.data[,(ncol(all.data)-n+1):ncol(all.data), drop=f]) } else { return(all.data) } }
the way you've written code, dirname()
call comprises entirety of body of function. braced block follows executed , not part of function.
after running code (and getting error quoted), fund.data()
:
fund.data; ## function ## ( ## symbol, # ticker ## n=10, # number of periods ## mode=c('quarterly','annual'), # periodicity ## max.attempts=5 # maximum number of attempts download before exiting ## ) ## dirname(sys.frame(1)$ofile)
as can see, braced block not taken part of function definition. executed after fund.data()
defined. function definition takes only following expression body, although expression may comprise braced block, allows number of statements subsumed within it. , @richardscriven pointed out in comment, there no actual call function anywhere in code.
so, reason why you're getting exact error "object 'symbol' not found" because function parameter symbol
not exist in braced block, because not part of body of function , executed itself.
to solve problem, need surround entire function body braced block:
fund.data <- function ( symbol, # ticker n=10, # number of periods mode=c('quarterly','annual'), # periodicity max.attempts=5 # maximum number of attempts download before exiting ) { dirname(sys.frame(1)$ofile) all.data = c() option.value = -1 ... }
although it's not clear purpose of dirname()
call is, since return value not used.
Comments
Post a Comment