Reputation: 3184
I'm having problems with a nested function and can't figure out why because i placed it before the code inside the other function. If i run the code inside the main function everything is great, but if i call the function i get "could not find function "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)
}
reproductible dataset
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")
Upvotes: 0
Views: 1077
Reputation: 7475
InstanceFromLocation
is only defined within the function MatchLocationsInstances
you can declare it outside the main function
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)
}
Upvotes: 1