Using value returned by function as condition in conditional panel


#1

I am trying to create a conditional panel based on the returned value of a utility function that is called from the server function. I have made the returned value variable ‘returnedValue’ global by writing it in a global.R file. It seems that the conditionalPanel is tied to the server function input and output

global.R

returnedValue = TRUE

app.R

server <- function(input, output){
result <- glycoPipe(inFileName)
    returnedValue = result$pass
    returnedText <- result$params
}

the function may return TRUE or FALSE. if it returnedValue is FALSE, i want to display a conditional panel
in the UI

ui <- fluidPage(
conditionalPanel(
condition = "returnedValue == 'FALSE'"
#request input
)

There are not errors or complains when I run the program, but the condition does not work. The conditional panel displays all the time weather the returnedValue is TRUE or FALSE.
The problem is that I am not trying to use in the condition a server input or output, but the return of a function. Is there anyway this can be done?


#2

It is displaying all the time because your code is only being evaluated once when the app is started. If you want the condition to be updated while the app is open then you need to put the code in your server code into reactive expressions. See some tutorials here.


#3

Thank you for your response.


#4

This time I tried the code below, but still does not work. I can see though, that my function returns two values: returedValue and returnedTex so I guess I have to extract returnedValue from returnedFrom function() some how. AM i in the right direction?

conditionalPanel(
                 condition = "returnedFromFunction() == 'FALSE'",
                 helpText("result is false")
                 
                 
               )

and

returnedFromFunction <- reactive({
    result <- glycoPipe(inFileName)
    returnedValue = result$pass
    returnedText <- result$params
    })

#5

I am trying this now, but it does not work either. How could I use the function return value in the condition?

conditionalPanel(
                 condition = "returndFromFunction.returnedValue",
                 helpText("result is false")
                 
                 
               )
server <- function(input, output)
returnedFromFunction$returned <- reactive({
    result <- glycoPipe(inFileName)
    returnedValue = result$pass
    if(returnedValue == FALSE){
      returned = returnedValue
    }else
      return()
    returnedText <- result$params
    })
   # output$returnedValue
   outputOptions(returnedFromFunction, 'returnedValue', suspendWhenHidden = FALSE)
}

#6

Your first attempt at the reactive was closer than your second. Reactives behave like functions (because they essentially are functions) so in your first attempt only the value of returnedText is being returned from the reactive function. You need to explicitly state what you want returned with return(value_to_return) or whatever the last object evaluated is will be returned by default. If you want returnFromFunction to be the value of returnedValue then your reactive statement should look like this:

returnedFromFunction <- reactive({
   result <- glycoPipe(inFileName)
   return(result$pass)
   })

#7

Thanks. I will try that.


#8

I do not know if I understood correctly because this code does not work either

  conditionalPanel(
                 condition = "returnedFromFunction().returnedValue",
                 helpText("result is false")
                 
                 
               )
 returnedFromFunction <- reactive({
      result <- glycoPipe(inFileName)
      returnedValue = result$pass
      returnedText <- result$params
      return(returnedValue)
    })

#9

this should work:

  conditionalPanel(
                 condition = "output.returnedFromFunction",
                 helpText("result is false")
                 
                 
               )
 output$returnedFromFunction <- reactive({
      result <- glycoPipe(inFileName)
      returnedValue = result$pass
      returnedText <- result$params
      return(returnedValue)
    })

outputOptions(output, "returnedFromFunction", suspendWhenHidden = FALSE)

You need to make the value an output. Also, you can not access the values returned by a reactive with the names you assigned them in the reactive unless you return a named list with those elements. This has to do with environments of functions. If you want to dive into that, then you should check out Hadley’s Advanced R


#10

Thanks very much. It works great!