Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any simple clean way to attach info to a session? #1512

Closed
daattali opened this issue Dec 15, 2016 · 9 comments
Closed

Any simple clean way to attach info to a session? #1512

daattali opened this issue Dec 15, 2016 · 9 comments
Assignees

Comments

@daattali
Copy link
Contributor

daattali commented Dec 15, 2016

x-post from shiny-discuss

Suppose I have a package with an exported function that can be called from other people's shiny apps' server code. I want to know whether it's the first time they call this function or not. What's the best way to do this?

I can add an attribute to the session object but that sounds like a bad idea. I feel like there should be a simple solution I'm not seeing

Example pseudocode:

# This function would be in a package
myfunc <- function() {
  if (firsttime) {
    ...
  } else {
    ..
  }
}

And any shiny app can call it in server code, potentially multiple times

ui <- fluidPage()
server <- function(input, output, session) {
  mypkg::myfunc()
}

If I was working on a regular R package without a shiny app, I would use a global environment to store this global value. But in this case I want this to be per-session.

This isn't an issue, it's a question that's gotten no response on shiny-discuss. I might be abusing the Issues system, but I thought this would be a bit more considerate than my usual method of personally emailing @jcheng5

@jcheng5
Copy link
Member

jcheng5 commented Dec 15, 2016

Sorry, I've been busier than usual the last few weeks. This is an important capability (we've needed it ourselves a couple of times recently) and so easy to add.

I'm envisioning session$userData$foo <- 10. Only question I have is whether session$userData should be a regular environment, or a reactiveValues? (If the former, if you needed reactivity you could create a reactiveValues object and put it into a slot in the regular environment; but if the common case is needing reactivity it might be nicer to do the latter.)

@daattali
Copy link
Contributor Author

For my specific case a regular environment is good enough, but I would think making it reactiveValues would be useful. I'd vote for the latter.

This wouldn't be backwards compatible though - for older shiny versions, would attaching an attribute to session be the least terrible way of achieving this?

@jcheng5
Copy link
Member

jcheng5 commented Dec 15, 2016

@daattali Huh, does attaching an attribute work? If so, that's a fine hack. 👍

@daattali
Copy link
Contributor Author

Yup, you can't session$foo <- "bar" because of locked environments, but R is perfectly happy with attr(session, "foo") <- "bar".

Wanted to see if you had any reason to discourage me from doing that. I got your thumbs up though so I'm happy

@bborgesr
Copy link
Contributor

I like the idea of having this explicitly as a session element. It seems handy. However, I think I'm missing why exactly this isn't possible as of now without tacking an attribute onto the session object.

The scoping rules for shiny:

# shared across all sessions
a <- "all"

ui <- fluidPage()

server <- function(input, output, session) {
  # defined in each session
  s <- "session"

  output$text <- renderText({
    # defined each time this function is called
    f <- "function"
  })
}

Why isn't s what you need?

In your case:

ui <- fluidPage()
server <- function(input, output, session) {
  wasCalled <- 0
  
 observe({  # or a render func or a reactive
    # the logic / conditionals you need...
    mypkg::myfunc()
    wasCalled <<- wasCalled + 1
  })
}

@daattali
Copy link
Contributor Author

daattali commented Dec 15, 2016 via email

@jcheng5
Copy link
Member

jcheng5 commented Dec 15, 2016

Yep, this is for advanced usage by module and package authors.

@bborgesr
Copy link
Contributor

Got it, thanks!

@wch wch closed this as completed in #1513 Dec 15, 2016
@wch wch removed the review label Dec 15, 2016
@davidlub
Copy link

Hi,

The shinyStore package for storing local data using the HTML 5 storage is easy to use and works well. You could use it to store info per browser (not per session).

David

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants