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
Setting session$userData
on the parent session from within a module
#1546
Comments
You can always pass in the real session as an argument to the module, but I understand that may be clutter for you. This works (the reason library(shiny)
modUI <- function(id) {}
mod <- function(input, output, session) {
parentSession <- .subset2(session, "parent")
parentSession$userData$flag <- TRUE
}
ui <- fixedPage(
modUI("modID")
)
server <- function(input, output, session) {
callModule(mod, "modID")
print(session$userData$flag)
}
shinyApp(ui, server) We can't guarantee that this won't change (although that's unlikely)... Given that this is a reasonable thing that an app author may need to do, I think we'll probably add direct support (and documentation) for this at some point. I hope this workaround does the job for now! |
Cool, I didn't think of trying I won't be using this because I want to use it in a package so I prefer not to use something without an explicit API, but it's useful to know, thanks |
Yeah, we don't want people to use it, but not because it violates the isolation of the module. Rather, everything in |
session$userData
on the parent session from within a module
Makes sense, thanks for the answer. I'll leave this issue open in case you want it as a tracker for adding the API you talked about. You can close the issue if you want |
Hi, how are you? @bborgesr I saw your comment about read-only. Do you say that is not recommendable to save data in session$user or I understand bad? Thanks in advance |
@ppagnone: You can save data into
What you can't do is actually overwrite the So, this is okay/good: session$userData$flag <- TRUE But this is not: session$userData <- TRUE Same problem here: session$flag <- TRUE That was not Dean's question. His question was, when using modules, how to store data in the So, by all means, do save whatever variables you want inside |
@bborgesr Thank you! you were very understandable. Excuse me my misunderstanding. |
A semi-related question: how to set |
+1 to allow session$userData$whatever <- TRUE (without "parent-workaround") |
I think there might have been some wrong information in this thread. The |
@jcheng5 I think this should be re-opened. You are right and thanks for the clarification. And sorry for spreading confusion! This really makes a lot of sense. But, fundamentally, the original and remaining problem is that you cannot write into the I.e. you should be able to do this: library(shiny)
modUI <- function(id) {}
mod <- function(input, output, session) {
session$userData$flag <- TRUE
}
ui <- fixedPage(
modUI("modID")
)
server <- function(input, output, session) {
callModule(mod, "modID")
print(session$userData$flag)
}
shinyApp(ui, server) But you have to do this instead: library(shiny)
modUI <- function(id) {}
mod <- function(input, output, session) {
parentSession <- .subset2(session, "parent")
parentSession$userData$flag <- TRUE
}
ui <- fixedPage(
modUI("modID")
)
server <- function(input, output, session) {
callModule(mod, "modID")
print(session$userData$flag)
}
shinyApp(ui, server) |
Fixed #1546: make it possible to write into a module's session$userData non-hackily
…itrary data into a module's session$userData More descriptive NEWS item and added an explanatory comment to the code
Related to #1512
I want to add a flag to the session so that I know if this is the first time or not that a function gets called. I do this using the new
session$userData
env. When modules are not involved, this is fine.If the function happens to be called from inside a module, which means the session object will be a proxy session, I want to be able to grab the parent session object so that I can attach the flag to it instead. I saw that a proxy session has a
$parent()
, but it seems to be returningNULL
to me.The text was updated successfully, but these errors were encountered: