Home > Mobile >  How to create hyperlink on element of R Shiny application?
How to create hyperlink on element of R Shiny application?

Time:10-07

Let see the following application.

When a user sees a table and clicks on the hyperlink "B" I would like to go into "Letters" and have selected the value "B" there.

How it can be done?

library(shiny)
library(DT)
library(data.table)

server <- function(input, output, session) {
  
  output$uo_selector <- renderUI({
    selectizeInput(
      'si_letters', 'Letters', 
      choices = c("A", "B", "C"),
      multiple = FALSE, selected = "A")
  })
  
  df_table <- reactive({
    data.table(
      letters = c(
        paste0("<a href='Go to selector with value A'>", "A", "</a>"),
        paste0("<a href='Go to selector with value B'>", "B", "</a>"),
        paste0("<a href='Go to selector with value C'>", "C", "</a>")),
      numbers = c(1, 2, 3)
    )
    
  })
  
  output$dt_table <- renderDataTable(
    df_table(), escape = FALSE, options = list(pageLength = 5))
  
}

ui <- fluidPage(
  navbarPage('TEST', 
             tabPanel("Table",
                      fluidPage(
                        fluidRow(dataTableOutput("dt_table")))),
             
             tabPanel("Letters",
                      fluidPage(
                        fluidRow(uiOutput("uo_selector"))))
             
  )
)

# Run the application 
shinyApp(ui, server)

Thanks

CodePudding user response:

Please check the following approach:

  1. navbarPage needs an id to select a tab programatically
  2. suspendWhenHidden = FALSEfor your renderUI call to have the selectizeInput ready before the first link is clicked
  3. bindAll shiny tags to receive the link-click via observeEvent - Please see result

  • Related