Calling a function from a reactive context and calling a second function from the first one

Hello,

I am having the following problem. A function call happens in a reactive context of the server function - (output@thing <- renderPrint{ result <- functionname(parameter)}-. The definition of the called function is placed between the GUI and the server. functionname <- function(parameter = NULL){
name = NULL,
name = parameter

result2 <- functionname2(name)
#Second function call happens here
}

#Functon name2 is defined in the script:
functionname2 <- function(name = NULL){
thename = NULL
thename = name

#When I want to use this variable in this function. let's say:
for(i in1:length(list1){
for(j in length(list2){
If(i == j){
do something

}

it works the first time but it stops working from then on, meaning I cannot get the expected results any longer. Also I have noticed that simple statements such as list.file(path/filename) that worked well for a while, suddenly stop working.

Question:
Can a shiny app call a function from a reactive context in the server and that function call another function in a separate script and pass the same parameter to the second function?

I have cleaned the environment in all the possible ways. My RStudio IDE is configured so that the .RData file is not uploaded at start time, bu I keep having problems with the variables keeping their values in the function definition of the independent script.

Yes, you can define a function above the ui or between the ui and the server. You can call the function from the server. It works fine as long as you pass it every variable it needs to run. These are called global functions.

The advantage of a global function is that it loads once with the app and doesn't have to be reloaded every time a new session starts, as it would if it lived inside the server function.

However what such a function cannot do is access any variables defined inside the server function; you must pass everything required in parameters to the function call.

Also note that you can also define global variables, but since every session can see and change these, it's almost never what you actually want. App-global functions are ok, app-global variables typically aren't.

Tom

2 Likes