It is because you named your function argument tree
so "tree"
will always exist within the function's scope. Instead, pick an argument name that you are unlikely to be using as a variable name, for example:
buildSpreadTotalDF = function(.argh){
if (exists(as.character(substitute(.argh)))){
ret = "The tree exists"
} else{
ret = "The tree does not exist"
}
ret
}
tree <- 1
buildSpreadTotalDF(tree)
# [1] "The tree exists"
remove(tree)
buildSpreadTotalDF(tree)
# "The tree does not exist"