How to make a server variable global so that can ve used by all the reactive context in the server and by the GUI


#1

Suppose I have two functions like the ones below. How can I make inFile and inFileName global so that I do not have to repeat inFile <- input$file and inFileName <- inputFileName in all the different renderText context? In other words, How can i calculate the variables so that they can be used repeatedly through the server function?

 output$invalidFile <- renderText({
    inFile <- input$file
    inFileName <- input$file$name
    result <- glycoPipe(inFileName)
    if(result$validFile == FALSE){
    result$invalidParamsFile
    }
  })

  output$pathOutput <- renderText({
    inFile <- input$file
    inFileName <- input$file$name
    result <- glycoPipe(inFileName)
    result$outputPath
    
  })

#2

You can create reactive values with them and then call the reactive values inside of the renderText (or any other render, reactive, or observe functions).

inFile <- reactive({input$file})

inFileName <- reactive({input$file$name})

output$invalidFile <- renderText({
  result <- glycoPipe(inFileName())
  if(result$validFile == FALSE){
    result$invalidParamsFile
  }
})

output$pathOutput <- renderText({
  result <- glycoPipe(inFileName())
  result$outputPath
    
})

It is worth noting that you don’t have to assign those variables in each render function or their own reactive functions, you could just pass input$file$name directly into your glycoPipe functions


#3

Thank you for your response.


#4

When I runt this code,

inFile <- reactive({input$file})

inFileName <- reactive({input$file$name})

I get the following response:
" 1: shiny::runApp
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)"
What does this mean?


#5

Did you add parenthesis when you call inFileName() in the renderText functions? Now that it is inside of a reactive expression you need to call it like a function (because it is essentially a function).


#6

OK. I see. Thanks. I will do that. Could you please send me a little example?


#7

An example of reactive functions? You can look at the shiny tutorials, specifically Part 2.


#8

Thanks. It works fine. Did you see my other question "how to define a function in the server …?


#9

Hi, could you mark your question as solved for anyone else having the same kind of question ? It will appear as so in the list of question too.

Thanks a lot !


#10
inFile <- reactive({input$file})
   inFileName <- reactive({input$file$name}

output$pathOutput <- renderText({
     result <- glycoPipe(inFileName())
      result$outputPath
    
   })

#11

Isn’t that exactly the answer I gave you?


#12

Yes, it is, but misunderstood the request from cderv. I thought he was asking to write my working example. I will make the change. Thank you for telling me.


#13

Tyler is correct that in this instance you should re-write and place inFile in a reactive context, again-this is the correct way to solve this problem. However, there are some exception cases where you can assign using the scoping assignment <<- to do what the question asked.


#14

That is true, although using <<- can lead to unintended consequences that can be hard to diagnose and may cause your code to produce inaccurate results without realizing it.


#15

Tyler, 100% True! I simply wanted to mention it since question was phrased about making a server variable global even though that wasn’t what Giuseppa really needed to do.