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

Chapter 18.3 Suggestion for additional note to explain concept #629

Open
jhk0530 opened this issue Mar 14, 2024 · 1 comment
Open

Chapter 18.3 Suggestion for additional note to explain concept #629

jhk0530 opened this issue Mar 14, 2024 · 1 comment

Comments

@jhk0530
Copy link

jhk0530 commented Mar 14, 2024

In internal functions.

There's example code with separate shiny internal function.

before

server <- function(input, output, session) {
  switch_page <- function(i) {
    updateTabsetPanel(input = "wizard", selected = paste0("page_", i))
  }
  
  observeEvent(input$page_12, switch_page(2))
  observeEvent(input$page_21, switch_page(1))
  observeEvent(input$page_23, switch_page(3))
  observeEvent(input$page_32, switch_page(2))
}

after

switch_page <- function(i) {
  updateTabsetPanel(input = "wizard", selected = paste0("page_", i))
}

server <- function(input, output, session) {
  observeEvent(input$page_12, switch_page(2))
  observeEvent(input$page_21, switch_page(1))
  observeEvent(input$page_23, switch_page(3))
  observeEvent(input$page_32, switch_page(2))
}

Changed: switch_page function "moved out" of server function.

And there's explanation like below

"We could of course add session to the arguments of the function:"

However, the code is not much different as explained. (session is not mentioned in code)
Since shiny's updated version (updateTabesetPanel()) use session with getDefaultReactiveDomain() as default parameter.

So I suggest to mention like below and why session parameter didn't used to avoid confusion.


We could of course add session to the arguments of the function:

switch_page <- function(session, i){
  updateTabsetPanel(session, input = "wizard", selected = paste0("page_", i))
}

server <- function(input, output, session) {
  observeEvent(input$page_12, switch_page(session, 2))
  observeEvent(input$page_21, switch_page(session, 1))
  observeEvent(input$page_23, switch_page(session, 3))
  observeEvent(input$page_32, switch_page(session, 2))
}

However, since shiny > 1.6 uses update- function (like updateTabsetPanel) uses session as default pameter,
we can skip the session like below

switch_page <- function(i) {
  updateTabsetPanel(input = "wizard", selected = paste0("page_", i))
}

server <- function(input, output, session) {
  observeEvent(input$page_12, switch_page(2))
  observeEvent(input$page_21, switch_page(1))
  observeEvent(input$page_23, switch_page(3))
  observeEvent(input$page_32, switch_page(2))
}

But this feels weird as the function is still fundamentally coupled to this app because it only affects a control named “wizard” with a very specific set of tabs.


Thanks.

@jhk0530
Copy link
Author

jhk0530 commented Mar 14, 2024

I found simillar issue #431 .

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

1 participant