Search code examples
rshinyflexdashboard

How can I create a page link in R shiny flexdashboard that is conditional on an input?


I have two inputs in the example below, the second one is conditional on the first. What I want to do is create a link on the first page that either goes to the second page or the third page depending on the value of the first input. If the first input is "Yes", triggering the second input, I plan on using the value of the second input in the third page. Here is my functional example.

---
title: "test"
output: 
  flexdashboard::flex_dashboard:
    theme: yeti
    orientation: rows
    vertical_layout: fill
runtime: shiny
---

Page 1
=======================================================================

Sidebar {.sidebar}
-----------------------------------------------------------------------

#```{r}
library(tidyverse)

#First Input
selectInput(inputId='choiceID', label='choice', 
            choices=c("Yes", "No"), selected="No")

#Second Input
uiOutput("cond1")

output$cond1 <- renderUI({
  if(input$choiceID == "Yes") selectInput(inputId='choice2ID', label="choice2", choices=c("foo", "bar"))
})

#Create Page Tag
page <- reactive(if_else(input$choiceID == "Yes", "page-3", "page-2"))

#This works
page2 <- "page-2"

#```

Click on [Link](#`r page`)

`r page`

Click on [Link](#`r page2`)

Page 2
=======================================================================

Page 3
=======================================================================

I am trying to capture the text that needs to go in the link, either "page-2" or "page-3", in the object page and then pass that to the link call. I have added r page below the link in order to see the text of the object. It appears to be correct and is updating when the first input changes, but the link does not go anywhere when clicked. I also added the page2 object which does not have the conditional piece but does make the link work correctly.

So, I'm wondering how I can make this conditional input modify a link to go to a different page, either by using my method or another method. Thanks in advance for any help.


Solution

  • You should create a dynamic link:

    ---
    title: "test"
    output: 
      flexdashboard::flex_dashboard:
        theme: yeti
        orientation: rows
        vertical_layout: fill
    runtime: shiny
    ---
    
    Page 1
    =======================================================================
    
    Sidebar {.sidebar}
    -----------------------------------------------------------------------
    
    ```{r}
    library(tidyverse)
    
    #First Input
    selectInput(inputId='choiceID', label='choice', 
                choices=c("Yes", "No"), selected="No")
    
    #Choose Page
    page <- reactive(if_else(input$choiceID == "Yes", "page-3", "page-2"))
    
    #Dynamic link creation
    output$link <- renderUI({
           url <- a("Dynamic link", href=paste0("#section-",page()))
           tagList("Click on ",url)
        })
    
    #Dynamic link display
    uiOutput("link")
    
    ```
    
    Page 2
    =======================================================================
    
    Page 3
    =====================================================================