0

我遇到了嵌套函数的问题,无法弄清楚为什么,因为我将它放在另一个函数内的代码之前。如果我在主函数中运行代码一切都很好,但是如果我调用该函数,我会得到“找不到函数”InstanceFromLocation“

    MatchLocationsInstances = function (resultSheet1) {
    #load required libraries
    require(plyr)
    #declare functions
    InstanceFromLocation = function (v_string) {
    #load libraries
    require(stringr)
    require(gdata)
    #variables
    stringLenth = nchar(v_string)
    #find positions of the ":"
    v_positions = data.frame(str_locate_all(v_string, ':'), stringsAsFactors = F)
    positions_length = length(v_positions[, 1])
    #substract and trim the result if there are entries in v_positions
    if (positions_length != 0) {
      result = trim(substr(v_string, v_positions[positions_length, 1] + 1, stringLenth))
    } else {
      result = ''
    }
    return(result)
    }

    #add a new dataframe with 
    tmpResult = ddply(resultSheet1, .(`Col 1`), transform, FromLocation = toupper(InstanceFromLocation(`Col 1`)))[, 2:9]

    return(tmpResult)
    }

可复制的数据集

                resultSheet1 = structure(list(`File Name` = c("file_name2.txt", 
            "file_name.txt", "file_name.txt", 
            "file_name.txt", "file_name.txt", 
            "file_name.txt"), `Col 1` = c("sometext2", 
            "sometext2", "sometext2", "sometext2", "sometext2", "sometext2"), Hostname = c("sometext", 
            "sometext", "sometext", "sometext", "sometext", 
            "sometext"), `Schema Name` = c("schema", "schema", 
            "schema", "schema", "schema", "schema"), 
                `text Targets` = c("***File Error!***", "sometext_LOCATION / sometext / sometext:port:sometext2 ", 
                "sometext5 / / :port:sometext2 ", "sometext3 / sometext3 / sometext:port:sometext2 ", 
                "sometext4 / textI / sometext:port:sometext2 ", "textI_LOCATION / textI / sometext:port:sometext2 "
                ), `Number of Objects Deployed` = c(963, 963, 963, 963, 963, 
                963), `No. of jobs that were run on this instance` = c(342623, 
                337803, 337803, 337803, 337803, 337803)), .Names = c("File Name", 
            "Col 1", "Hostname", "Schema Name", "text Targets", 
            "Number of Objects Deployed", "No. of jobs that were run on this instance"
            ), row.names = c(NA, 6L), class = "data.frame")
4

1 回答 1

1

InstanceFromLocation仅在函数内定义,MatchLocationsInstances您可以在主函数之外声明它

  InstanceFromLocation = function (v_string) {
    #load libraries
    require(stringr)
    require(gdata)
    #variables
    stringLenth = nchar(v_string)
    #find positions of the ":"
    v_positions = data.frame(str_locate_all(v_string, ':'), stringsAsFactors = F)
    positions_length = length(v_positions[, 1])
    #substract and trim the result if there are entries in v_positions
    if (positions_length != 0) {
      result = trim(substr(v_string, v_positions[positions_length, 1] + 1, stringLenth))
    } else {
      result = ''
    }
    return(result)
    }

MatchLocationsInstances = function (resultSheet1) {
    #load required libraries
    require(plyr)
    #declare functions

    #add a new dataframe with 
    tmpResult = ddply(resultSheet1, .(`Col 1`), transform, FromLocation = toupper(InstanceFromLocation(`Col 1`)))[, 2:9]

    return(tmpResult)
    }
于 2012-06-27T08:37:51.147 回答