How to select a specific tab in R Markdown? - r

I need to select a tab from a tabset in R Markdown document (with Shiny runtime).
I followed the example in How to select a specific tabPanel in Shiny, and tried to adapt it to R Markdown. I added ids to the tabset / tab, and used them in the updateTabsetPanel() call, but it doesn't seem to work. (I used the names that pop-up when inspecting the individual HTML elements in the resulting dashboard.)
How can I select the "Chart3" tab from the tabset by clicking the button?
EDIT: I need to be able to select a specific tab programmatically (e.g. via observeEvent() call), not just on start-up.
---
title: "Tabset Column"
output: flexdashboard::flex_dashboard
runtime: shiny
---
Column
-------------------------------------
### Chart 1
```{r}
actionButton("switch_tab", "Switch tab", width=200)
```
Column {#mytabset .tabset}
-------------------------------------
### Chart 2
```{r}
```
### Chart 3 {#mytab}
```{r}
observeEvent(input$switch_tab, {
updateTabsetPanel(session, inputId = "section-mytabset", selected = "#section-mytab")
})
```

The {.active} attribute could answer your question when launching the dashboard (static solution), and works with html_document :
---
title: "Active Tabset"
output: html_document
---
Column {.tabset}
-------------------------------------
### Tab 1
Some text
### Tab 2 {.active}
Some other text
Unfortunately, this didn't work with Flexdashboard :
---
title: "Active Tabset"
output: flexdashboard::flex_dashboard
---
Column {.tabset}
-------------------------------------
### Tab 1
Some text
### Tab 2 {.active}
Some other text
The issue has already been signaled here but was closed because of automatic lock.
The waiting period in order to comply with RMarkdown issue guide being over, I filed a new issue with the Minimal Reproducible Example above.
EDIT :
This request has been taken into account, so this should soon work with Shiny Dashboard.

Instead of an observeEvent you could wrap the actionButton itself in an tags$a and link to #section-mytab. Note that you have to add section- before the tab name when using runtime: shiny.
Does this solve your problem or do you need it to work with observeEvent?
---
title: "Tabset Column"
output: flexdashboard::flex_dashboard
runtime: shiny
---
Column
-------------------------------------
### Chart 1
```{r, echo = FALSE}
tags$a(href = "#section-mytab",
shiny::actionButton("btn1", "go to mytab")
)
```
Column {.tabset}
-------------------------------------
### Chart 2
```{r}
```
### Chart 3 {#mytab}
```{r}
```
If needed, the logic above can be combined with observeEvent using {shinyjs} and a non-visible actionButton. The trick is here, that we still use an actionButton to trigger the tab. But the actual button is not shown display: none (it is important, that the button is not set to hidden, since this will prevent it from being clicked). We then create another actionButton which is observed by an observeEvent. This can trigger other calculations etc. and finally a click on the actionButton which is not shown. If you have more pages and want to jump from page 1 to, say, tab 3 on page 2, then we would need two clicks: one changing the page and one activating the tab. But we can all trigger this inside the observeEvent. Its hacky and doesn't look like good code, but on the plus side it works, even without a custom javascript function.
---
title: "Tabset Column"
output:
flexdashboard::flex_dashboard
runtime: shiny
---
```{r global, echo = FALSE}
library(shiny)
library(shinyjs)
useShinyjs(rmd = TRUE)
```
Column
-------------------------------------
### Chart 1
```{r, echo = FALSE}
observeEvent(input$btn1, {
# do some calculations here
click("btn2")})
shiny::actionButton("btn1", "do something")
tags$a(href = "#section-mytab",
# set this button to `display: none;` but *not* to `hidden`
shiny::actionButton("btn2", "go to mytab", style = "display: none")
)
```
Column {.tabset}
-------------------------------------
### Chart 2
```{r}
```
### Chart 3 {#mytab}
```{r}
```

Related

Navigate to tab in Shiny at the same level as parent tab

I am writing a report in R Markdown using shiny runtime. It has four main sections which I want to make accessible through the tabset functionality. However, I would really like to embed buttons in each individual section which allows you to jump to another section.
A minimal example is included below, where I tried to follow this example but it doesn't seem to work outside of the flexdashboard setup. To make things even more complex, I would like to be able to trigger switching sections on an observeEvent-type of way. Specifically, I'm trying to include some simple barplots in some of the sections which, when clicked on, bring the user to another section.
Any help is greatly appreciated!
---
title: "Example Report"
output:
html_document
runtime: shiny
---
# {.tabset .tabset-fade .tabset-pills}
## A. {#mytab1}
```{r, echo = FALSE}
tags$a(href = "#section-mytab2",
shiny::actionButton("btn1", "go to mytab2")
)
```
### Summary
### Methodology
## B. {#mytab2}
### Summary
### Methodology
## C. {#mytab3}
### Summary
### Methodology
## D. {#mytab4}
### Summary
### Methodology

Flexdashboard - hidden navbar tab using reactive values

I can hide a navbar page using the static value of show_hide but I cannot figure out how to do it with the reactive value r_show_hide(). I have also tried using isolate(ifelse...) and then r_show_hide (no parentheses) as well as reactiveVal() to no avail.
There is also an extraneous "> that shows up. Any help would be appreciated.
Update:
I created an issue https://github.com/rstudio/flexdashboard/issues/229
---
title: "-"
output: flexdashboard::flex_dashboard
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
show_hide <- "show" # "hidden"
r_show_hide <- reactive(ifelse(session$clientData$url_hostname == "127.0.0.1", "hidden", "show"))
```
Does work {.`r show_hide`}
=============================
### Should be `r show_hide`
Doesn't work {.`r reactive(r_show_hide())`}
===============================
### Should be `r renderText(r_show_hide())`
Ok, this took me a while figuring out.
The fundamental problem is that r chunks in curly brackets of the flexdashboard navbar evaluate in a non-reactive context, compared to the r chucks that build the content of each page, which are evaluated in a reactive context. For this reason you cannot use a reactive such as r_show_hide() to trigger the argument hidden/show of the navbar page, but you can use r_show_hide() in a renderText() function within the page.
So the actual question is, how to access a reactive value from a non-reactive context. The answer is isolate() and is explained here.
Below I provide an example using your code.
---
title: "-"
output: flexdashboard::flex_dashboard
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
show_hide <- "show" # "hidden"
r_show_hide <- reactive(ifelse(session$clientData$url_hostname == "127.0.0.1", "hidden", "show"))
```
Does work {.`r show_hide`}
=============================
### Should be `r show_hide`
Doesn't work {.`r isolate(r_show_hide())`}
===============================
### Should be `r renderText(r_show_hide())`

Flexdashboard multiple attributes page

I'm learning flexdashboard and trying some different lay-outs for a future app. But I'm having trouble assigning different attributes for the same page.
I want the second page to have a row lay-out, be listed in nav-bar A and have an icon in front of the title.
When I write them like this: {data-orientation=rows, data-navmenu="Menu A", data-icon="fa-list"} none of them is used. When written like this: {data-orientation=rows}, {data-navmenu="Menu A"}, {data-icon="fa-list"} only the last one is executed and the first two are put in the page-title. When using the second one without commas, the same thing happens.
I haven't found any examples of muliple attributes for a page in the examples.
How do I combine them? It must be possible as I can't imagine I have to chose between giving my page a row-format and putting it in a drop-down menu...
This is the code I used:
title: "My flexdash"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
```
Page 1 {data-navmenu="Menu A"}
======================================
Column {data-width=350}
-----------------------------------------------------------------------
### Chart A
```{r}
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart B
```{r}
```
# need these attributes to be working but invisible
Page 2 {data-orientation=rows}, {data-navmenu="Menu A"}, {data-icon="fa-list"}
=============================================
Row {data-width=650}
-----------------------------------------------------------------------
### Chart C
```{r}
```
Row {data-width=350}
-----------------------------------------------------------------------
### Chart D
```{r}
```
Found the answer: I only had to separate them by a space...
Page 2 {data-orientation=rows data-navmenu="Menu A" data-icon="fa-list"}
Thanks - I didn't have the same problem (dropdown + storyboard) but your rmd snippet was just what I needed to solve the problem! That is, just keeping the two layouts parallel instead of shoehorning one layout into another.
Responsable Conception Produit {data-navmenu="Competences par Métier"}
=========================================
<br>
<br>
```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
# Responsable Conception Produit
...
```

Insert image in shiny dashboard: shiny flex dashboard failed

I run shiny in flex dashboard template in markdown.
I want to insert images in tab but it always shows me
Warning in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="figure-html/unnamed-chunk-8-1.mb.png": The system cannot find the path specified
And the dashboard output shows nothing. Has anyone encounters the issue before?
If just want to insert the image no other coding for graphs etc, then it works
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```
Column {data-width=350}
-----------------------------------------------------------------------
![alt](/Users/abc/Desktop/cluster.png)
if add other codes for other graphs , ex , the dashboard shows nothing .but if remove the insert image part , the dashboard is perfect
Column {data-width=500}
-----------------------------------------------------------------------
### Data
```{r}
DT::datatable(table_fm, options = list(pageLength = 10))
```
Column {data-width=350}
-----------------------------------------------------------------------
![alt](/Users/abc/Desktop/cluster.png)

get selected row in a datatable in an interactive document using Rmarkdown and shiny

I am exploring the use of DT:datatable in an interactive document using Rmarkdown and shiny (I have not used datatable before).
I am able to create a document that plots a data table:
---
title: "Test DT"
output: html_document
runtime: shiny
---
```{r echo=FALSE}
datatable(iris)
```
Clicking in a row in the datatable highlights a row. Is there any way to access the selected rows without implementing a shiny server? How?
You have to use an output$id for it to work. Just how you would do it in shiny itself
---
title: "Test DT"
output: html_document
runtime: shiny
---
```{r echo=FALSE}
library(DT)
DT::dataTableOutput('irisTable')
output$irisTable = DT::renderDataTable(iris, selection = 'multiple')
p("By default DT allows multiple row selection. Selected rows are...")
renderPrint(input$irisTable_rows_selected)
```
DT also allows column and cell selection as well as preselection. See documentation

Resources