Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 粘贴不';不认识物体_R - Fatal编程技术网

R 粘贴不';不认识物体

R 粘贴不';不认识物体,r,R,我试图运行这个函数,但当我试图编译它时,它说: 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

我试图运行这个函数,但当我试图编译它时,它说:

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 to 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)
      }, finally = {
        if(flag) break
      })
    }

    if( length(grep('INDICATORS', txt, ignore.case = T)) == 0 ) {
      cat('No Data Found for', Symbol, '\n')
      return(all.data)
    }

    # get 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 from this page
    data = extract.table.from.webpage(txt, 'INDICATORS', has.header = T)
    colnames(data) = data[1,]
    rownames(data) = data[,1]
    data = data[,-1,drop=F]

    # only add not already 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 it is time to stop
    if(ncol(all.data) >= n) break
    if(option.value == 0)  break

    # extract option value to go to the 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 only get 5 time periods at a 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)
  }
}
粘贴中的
错误(“http://uk.advfn.com/p.php?pid=financials&symbol=“,符号,:
找不到对象“符号”
fund.data=0){
url=粘贴('http://uk.advfn.com/p.php?pid=financials&symbol=“,符号,”&btn=”,模式[1],“_报告&”,开始日期[模式[1],“=”,option.value,sep=”)
}否则{
url=粘贴('http://uk.advfn.com/p.php?pid=financials&symbol=,符号“&btn=”,模式[1],“_报告”,sep=”)
}
cat('正在下载',url',\n')
#txt=join(读线(url))
对于(i尝试1次:最大尝试次数){
flag=T
tryCatch({
txt=join(读线(url))
},中断=功能(ex){

flag按照您编写代码的方式,您的
dirname()
调用包含整个函数体。后面带括号的块将立即执行,而不是函数的一部分

运行所有代码(并获取您引用的错误)后,这是
fund.data()

如您所见,带括号的块没有作为函数定义的一部分。它是在
fund.data()之后立即执行的
已定义。函数定义仅将紧跟其后的表达式作为主体,尽管该表达式可能包含一个大括号块,允许包含任意数量的语句。正如@RichardScriven在其评论中指出的,在代码中的任何地方都没有对函数的实际调用

因此,您得到确切错误“object'Symbol'not found”的原因是,函数参数
Symbol
不存在于带括号的块中,因为它不是函数体的一部分,并且是自己执行的

要解决您的问题,您需要用一个支撑块围绕整个函数体:

fund.data <- function
(
    Symbol,       # ticker
    n=10,             # number of periods
    mode=c('quarterly','annual'), # periodicity
    max.attempts=5    # maximum number of attempts to download before exiting
) {
    dirname(sys.frame(1)$ofile)
    all.data = c()
    option.value = -1

    ...

}

fund.data同时包含太多的信息和不足的信息。对函数的调用在哪里?
fund.data <- function
(
    Symbol,       # ticker
    n=10,             # number of periods
    mode=c('quarterly','annual'), # periodicity
    max.attempts=5    # maximum number of attempts to download before exiting
) {
    dirname(sys.frame(1)$ofile)
    all.data = c()
    option.value = -1

    ...

}