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

Internal link function #51

Open
JT-39 opened this issue Sep 25, 2024 · 3 comments
Open

Internal link function #51

JT-39 opened this issue Sep 25, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@JT-39
Copy link

JT-39 commented Sep 25, 2024

Is your feature request related to a problem? Please describe.

With inspiration from dfeshiny::external_link(), what about `dfeshiny::internal_link()?

I want to be able to link to another page (or tab) from within my Shiny app.

Describe the solution you'd like

A simple function/ module to be able to link from one tab to another in a Shiny app.

Describe alternatives you've considered

You need to add an observeEvent() function to the server.R script for any link that navigates within your App.

Additional context

Module to do this (in bslib):

# Internal link UI function
InternalLinkUI <- function(id) {
  ns <- shiny::NS(id) # Namespace the module
  actionLink(ns("internal_link"), "LA Level page")
}


# Internal link server function
InternalLinkServer <- function(id,
                               tabset_id,
                               tab_value,
                               parent_session) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$internal_link, {
      # Switch to the specified tab
      bslib::nav_select(
        id = tabset_id,
        selected = tab_value,
        session = parent_session
      )
    })
  })
}
@cjrace
Copy link
Contributor

cjrace commented Sep 26, 2024

I guess this might be quite specific to how you've set your app up, though given the general push (even from POSIT) towards bslib, we could look at adding this use case for bslib tabs in specifically in as a specific helper that saves a little bit a of code for analysts, agree on having it as a module pair, should be a fairly quick addition to the package 😄

I think the server function would only need id and tabset_id as arguments and you could reuse that main id as the id for tab_value, the ui function would then have id, tabset_id and link_text as arguments? For the link text argument we could add some basic validation to make sure it's not too short or on the 'naughty list' we've got for external_link.

Could also add an example or two for where you have multiple pages you link to, and can then in the server use something like lapply to create all the server modules in a super efficient way.

@JT-39
Copy link
Author

JT-39 commented Oct 8, 2024

Definitely, its quite a niche usage but does push people to using {bslib} which I think is best practice in Shiny apps now.

Oooo, I like the use of id for the tab_value, I had never even thought of using the id of a module for something within it, this has opened a whole new door of possibilities! (Or at least not just wasted id parameter lol!)

I am a bit confused, why would we not need the parent_session parameter?

Yep, really like that example - and shows off lapply() too!

@cjrace
Copy link
Contributor

cjrace commented Oct 9, 2024

Yeah, anything pushing towards bslib is probably a good thing, and ID params in modules can be very nifty! Well worth making use of them to streamline the code.

For the parent_session I'm not 100% sure, though my assumption was it might always be availavle from a global context so doesn't need explicitly passing into the module. Would need to check it when implementing.

One other thing I think whoever implements this should consider (even if it's future me, writing here so I don't forget!) - is how it'll handle ID clashes, for example if you wanted to link to a given tab from multiple places in the app, how would that work, and what would be best practice. Ideally you'd only need one server call, but I don't know how that'd work in practice without playing about with it.

@cjrace cjrace added the enhancement New feature or request label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants