Flexdashboard - hidden navbar tab using reactive values - r

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())`

Related

How to select a specific tab in R Markdown?

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

How to ensure tabset contains the correct data

The goal is to create a a page in Rmarkdown that contains two tabs each displaying different information. After over a dozen different tries I've decided it makes sense to ask since the closest I've gotten as shown in the image is two tabs both showing the same information. Not sure what it is I'm missing. I've searched a couple other questions and none of them address the issue.
This is the code that I have tried so far
---
title: test
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
self_contained: false
---
Page
========================
## Try {.tabset}
### Tryto
```{r}
cat(paste("WORDSSSSS"))
```
### Work
```{r}
cat(paste("WORDSSSSSqqqqqqqqq"))
```
I included the single quotes here since it was messing with how things were displayed in SO so please remove.
Desired out put would have "WORDSSSSS" under the "Tryto" tab and "WORDSSSSqqqqqqqqqq" under "Work".
Thanks!
EDIT:
packages
---
title: "Test"
output: html_document
---
## Try {.tabset}
### Tryto
```{r}
cat(paste("WORDSSSSS"))
```
### Work
```{r}
cat(paste("WORDSSSSSqqqqqqqqq"))
```

Excluding part of the R markdown (html_notebook) from the final html output

I am writing a relatively long report using R Notebook that makes use of R markdown language which combines text and code in the same document and generates html output.
I would like to be able to exclude some of the analysis (both text and R code) from showing in the final HTML. This is very useful if I want to create two versions of the report - a full/detailed version, as well as a shorter version with main graphs and conclusions.
Obviously, I can create separate Rmd file for each type of report (or comment out pieces of the report that need to be excluded for the shorter version), but I was wondering if there is a more elegant way to do it.
Something like this:
if (Version == "full_text"){
Full analysis goes here
```{r}
R code goes here (could be multiple chunks)
```
}
else {
The shorter version goes here
```{r}
R code goes here
```
}
Place the "detailed" part of the report in a knitr child document which you call optionally from the main document.
Detailed content can then be switched on by calling the child document and it can be switched off by setting the variable child_docs to NULL. For example here are a main and a child document below.
Child document
Save this document under knitr-child.Rmd
---
title: "knitr child"
output: html_document
---
# Details from the child document
Hi, there. I'm a child with a plot and as many details as necessary.
```{r test-child}
plot(cars)
```
Main document - full/detailed version
---
title: "Report"
output: html_document
---
# Summary
```{r setup}
child_docs <- c('knitr-child.Rmd')
# child_docs <- NULL
```
```{r test-main, child = child_docs}
```
# Conclusion
Main document shorter version
---
title: "Report"
output: html_document
---
# Summary
```{r setup}
# child_docs <- c('knitr-child.Rmd')
child_docs <- NULL
```
```{r test-main, child = child_docs}
```
# Conclusion

How to render googlevis in flexdashboard

I'm trying to use GoogleVis with the new package FlexDashboard, which is like the intersection between a basic .Rd and shinyDashboards. It's basically a non-shiny dashboard.
Anyway, I'm trying to embed a googleVis object, which doesn't seem to be supported by default, but I can get the html to show up in the output, so there must be a way! Can we come up with some hack? Maybe combining either plot() or renderGvis() combined with some kind of hack? Ormessing with setting op <- options(gvis.plot.tag='chart')?
I have failed, but maybe someone else can figure it out?
Building on the awesome answer from Michal Majka at Conditional reactive logic shiny based flexdashboard you can do something like this
---
title: "Test gvisTable"
output:
flexdashboard::flex_dashboard:
orientation: rows
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(googleVis)
```
Column {data-height=350}
-----------------------------------------------------------------------
### Chart c
```{r}
#create a ui interaction:
uiOutput("dynamic")
#render the ui:
output$dynamic <- renderUI({
htmlOutput("myTable")
})
#here is your server activity:
output$myTable <- renderGvis({
gvisTable(Population )
})
```

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