我遇到嵌套函数的问题而无法找出原因,因为我把它放在其他函数内的代码之前。 如果我在main函数中运行代码一切都很好,但是如果我调用函数我得到“找不到函数”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")
答案 0 :(得分: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)
}