I have an issue similar to the one described in this post however mine involves a function being called in script 1 from a second script that stores the function. Essentially script1.R
has reference to foo()
in script2.R
and inside foo()
I have a line that prompts for user input from the console. However, when I run foo()
inside script1.R
it doesn't prompt at all. I have tried to enclose the logic of foo()
in brackets, as well as define the function itself (hence reference as foo()
here) and neither work. It is worth noting that script2.R
is a centralized script of created functions, and that foo()
is itself a smaller helper function defined inside of a larger function in script2.R
. So really it's:
#script1.R
dt <-setDT(fread(`imported_data.csv`))
dt <- standardization_function(dt)
and standardization_function()
lives in script2.R
#script2.R
standardization_function <-function(dataset, column_to_mutate) {
#mutations occur
foo <- function() {
disp <- readline(prompt = 'Do you want to see a list of unmatched entries? (y/n)')
disp <- toupper(disp)
if(disp == 'Y'){
cat('The unmatched entries are:\n')
cat(unmatched)
}
}
foo()
}
It seems it's related to the treatment of the entire function as an executable block of code, yet the defining of a function within nor the enclosing of the logic of foo in {}
(without defining a function) was able to overcome that.
The issue was in scoping the secondary function. It needs to be defined outside of the main block of the
foo()
code so that the readline is prompted to the user.