1

I saw this post already

handling multiple users simulaneously in an R Shiny app

and I am aware that you can define anything inside of the shinyServer() expression to make it private to a single user's session. However, this limits my ability to define variables that depend on a reactive expression outside of a reactive context.

Reproducible example:

Assume I define ui to have a button "do"

server <- function(input, output, session) {

observeEvent(input$do, {
    data<- read.csv("data.csv",check.names=F,row.names=1)  
  })

req(data) # doesn't work
matrix <- data    # PRODUCES ERROR, see below error

})

Error I get when doing this is, and this makes sense because I data is not defined yet:

Error in server(...) : object 'data' not found

I don't want to define matrix in a reactive context, because that would pose a tremendous headache to work with in my code later one as I would have to define MANY variables this way, and would have to access them reactively each time. I really want to avoid doing so if possible.

What I have instead been doing, and it has been working, is define everything (reactive or not) in a reactive context so that I dont have a single line of code outside of a reactive function (not variables, I am trying everything to avoid those). This, however, does not allow me to access variables outside said functions. So what do I do? I use global variables even though its terrible coding practice (I know) but it saves me a big headache and seems to be working.

MY ONLY CONCERN is that users can access each other's data since data is defined globally!!!

However, I have heard from someone that I can allow each user to create their own instance and therefore not share their global variables with anyone else.

If someone can tell me how to do this that would be great. I know this isn't good for scalability, but I am only looking for a temporary solution right now and will probably switch all my variables to be reactive eventually.

Thanks so much

  • 3
    Variables defined in the server function itself should be unique to a user. If you want to create a data based on a user action, I don't see how that data can possibly not be reactive it's not clear to me why you would try to avoid reactive elements in a shiny application. That's kind of the whole point. All your logic doesn't have to go in the server function. You can have helper functions where you can pass in the current reactive value as a parameter so it looks just like a normal value to the function. Some sort of proper. – MrFlick Feb 09 '22 at 19:01

0 Answers0