如何在 R shiny flexdashboard 中创建一个以输入为条件的页面 link?

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

我在下面的例子中有两个输入,第二个以第一个为条件。我想要做的是在第一页上创建一个 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")

#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
=======================================================================

我正在尝试捕获对象 page 中需要进入 link“第 2 页”或“第 3 页”的文本,然后将其传递给link 电话。我在 link 下面添加了 r page 以便查看对象的文本。它看起来是正确的,并且在第一个输入更改时正在更新,但是 link 在单击时不会去任何地方。我还添加了 page2 对象,它没有条件部分但确实使 link 正常工作。

所以,我想知道如何使用我的方法或其他方法使这个条件输入修改 link 以转到不同的页面。在此先感谢您的帮助。

这就是我要做的。下面的解决方案在 renderUI()paste() 中使用 HTML() 来根据选择生成适当的引用。然后就可以用htmlOutput()来显示了。

---
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

output$link <- renderUI({
  req(input$choiceID)
  htmltools::HTML(paste0("Click this <a href=", ifelse(input$choiceID == "Yes", "\"#section-page-3\"", "\"#section-page-2\""), ">link</a>"))
})
#This works
htmlOutput("link")
#```



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

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

您应该创建动态 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
=====================================================================