How to add tab sets into flexdashboard? - r

I am working with flexdashboard and I am trying to add sub tabset into tabset . Below you can see my code:
---
title: "Test APPLICATION"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(data.table)
library(tidyr)
library(dplyr)
library(tidyverse)
library(knitr)
library(DT)
library(rpivotTable)
library(biclust)
data(BicatYeast)
data=data.frame(data(BicatYeast))
```
# Intro
Testing application
Tabset 1
=======================================================================
Row {.tabset}
-----------------------------------------------------------------------
### Notes
### Sub tabset 1
```{r,eval=TRUE}
rpivotTable(
data,
aggregatorName = "Integer Sum",
vals="Total",
cols = "Test",
width="100%",
height="400px",
rendererName = "Table"
)
```
### Sub tabset 2
```{r,eval=TRUE}
rpivotTable(
data,
aggregatorName = "Integer Sum",
vals="Total",
cols = "Test",
width="100%",
height="400px",
rendererName = "Table"
)
```
Tabset 2
=======================================================================
Row {.tabset}
-----------------------------------------------------------------------
### Sub tabset 1
```{r,eval=TRUE}
datatable(data,
caption = "Test",
rownames = T,
filter = "top",
class = 'cell-border stripe',
extensions='Buttons',
options = list(
pageLength = 50,
dom = 'Blfrtip',
buttons=c('copy','csv','excel','pdf','print'),
lengthMenu = list(c(10,25,50,-1),c(10,25,50,"All"))
))
```
### Sub tabset 2
```{r,eval=TRUE}
datatable(data,
caption = "Test",
rownames = T,
filter = "top",
class = 'cell-border stripe',
extensions='Buttons',
options = list(
pageLength = 50,
dom = 'Blfrtip',
buttons=c('copy','csv','excel','pdf','print'),
lengthMenu = list(c(10,25,50,-1),c(10,25,50,"All"))
))
```
Now, I want to put additional tab sets under Sub tabset 1. Can anybody help me on how to solve this problem and add additional tab sets (two or three)?

Related

updatePickerInput() - stop users from selecting more than one item when clicking on a new tab

I am trying to update a picker input in a flexdashboard but can't seem to correctly do it.
When a user clicks on the "ThisTabGetsClicked" tab, I want the sidebar to update and just allow the user to select one item from the drop down menu. When the user exits this tab (by clicking on another tab) I want them to have the options back again.
How can I "disable" the option for users to select more than one item in the dropdown?
Flexdashboard:
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(shinyWidgets)
library(shinyjs)
library(tidyverse)
```
MySidebar {.sidebar data-width=300}
==============================
```{r}
useShinyjs(rmd = TRUE)
items = c("I1", "I2", "I3", "I4", "I5")
shinyWidgets::pickerInput(
inputId = "select",
choices = items,
selected = items[1],
multiple = TRUE,
options = list(
`actions-box`= TRUE,
size = 10,
`selected-text-format` = "count > 3"
)
)
```
# Home {data-orientation=rows}
=====================================
### Box1
1
c2
### Box2.1
2.1
### Box2.2
2.2
# ThisTabGetsClicked {data-orientation=rows}
=====================================
Here, I want to update the side bar dropdown to only display one input
```{r}
only_one_item_allowed = reactive({
input$select[1]
})
```
```{r}
observeEvent(input$apply, {
updatePickerInput(inputId = "select",
choices = only_one_provincia_allowed(),
selected = only_one_provincia_allowed(),
options = list(
size = 10
)
)
})
```

Layout of flexdashboard - having multiple tabsets

I currently have my dashboard which looks like:
What I am trying to do is under the Data tab have additional tabs. For example I want to combine the Table Population tab with the Graphic iris tab so that the table and graphic are on the same page. Then in a new tab have the Another tab-pag (as it currently is).
How can I merge the two tabs to have them on the same page?
I would like this joined tab to look like the Analysis tab:
Dashboard:
---
title: "Analysis"
output:
flexdashboard::flex_dashboard:
#css: "custom_style.css"
theme:
base_font:
google: Prompt
code_font:
google: JetBrains Mono
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(shinyWidgets)
library(shinyjs)
library(gt)
library(gtExtras)
library(plotly)
```
MySidebar {.sidebar}
==============================
```{r}
useShinyjs(rmd = TRUE)
shinyWidgets::pickerInput(
inputId = "select_species",
label = h4("Species"),
choices = unique(iris$Species),
selected = unique(iris$Species[1]),
multiple = TRUE,
options = list(
`actions-box`= TRUE,
size = 10,
`selected-text-format` = "count > 1"
)
)
```
Introduction {data-vertical_layout=scroll}
==============================
Column {data-width=650 .main .tabset}
-----------------------------------------------------------------------
Here is some introduction text
Data {data-vertical_layout=scroll}
==============================
IRIS Species {.tabset .tabset-fade}
-------------------------------------
### Table Population
```{r}
output$irisTable = render_gt({
iris %>%
gt() %>%
gt_add_divider(columns = "Provincias", weight = px(3), color = "lightgrey", include_labels = FALSE)
})
div(style='height:800px; overflow-y: scroll',
gt_output("populationTable")
)
```
### Graphic iris
```{r}
renderPlotly({
ggplotly(
iris %>%
ggplot(aes(x = Species, y = Petal.Length)) +
geom_bar() +
theme_bw()
)
})
```
### Another tab-page
Analysis {data-vertical_layout=scroll}
==============================
Column
-------------------------------------
### Tab 1
some text
### Tab 2
some more text

searchPanes extension in Rmarkdown

I am trying to get the searchPanes extension from DT to work in an rmarkdown file (https://rstudio.github.io/DT/extensions.html). I can get it to work with datatable but not with renderDataTable and reactive statement. Attached is a working example that one tab shows it working and the other tab showing the issue. Any advice/recommendation is appreciate.
My code:
---
title: "Filter_example"
output: flexdashboard::flex_dashboard
runtime: shiny
date: "`r Sys.Date()`"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
library(DT)
library(tidyverse)
```
# Sidebar {.sidebar data-width="200"}
```{r sidebar, echo=FALSE}
selectizeInput('color', label ='color', choices = diamonds$color ,
multiple = TRUE,
options = list(maxItems = 5,
placeholder = 'Search Here...'))
```
# Diamonds Are Forever
```{r echo=FALSE}
diamonds %>%
datatable(
rownames = F,
extensions = c("SearchPanes", "Select", "Buttons"),
options = list(
language = list(searchPanes = list(collapse = "Filter Rows")),
scrollX= "30vw",
scrollY = "45vh",
dom = "Blfrtip",
buttons = list("searchPanes")
),
selection = 'none'
)
```
# Diamonds Are Forever renderDataTable
```{r echo=FALSE}
diamonds_dd <- reactive({
if (is.null(input$color)){
diamonds
}
else if(!is.null(input$color)) {
diamonds %>%
filter(color %in% input$color)
}
})
DT::renderDataTable(diamonds_dd(),extensions = c("SearchPanes", "Select", "Buttons"),
options = list(
scrollX= "30vw",
scrollY = "45vh",
dom = "Blfrtip",
buttons = list("searchPanes"),
columnDefs = list(
list(searchPanes = list(show = FALSE), targets = 3:5),
list(searchPanes = list(controls = FALSE), targets = 0:2),
list(className = "dt-center", targets = 0:5)
)
),
selection = 'none'
)
```
You need server = FALSE.
DT::renderDataTable(
diamonds_dd(), extensions = c("SearchPanes", "Select", "Buttons"),
options = list(
scrollX= "30vw",
scrollY = "45vh",
dom = "Blfrtip",
buttons = list("searchPanes"),
columnDefs = list(
list(searchPanes = list(show = FALSE), targets = 3:5),
list(searchPanes = list(controls = FALSE), targets = 0:2),
list(className = "dt-center", targets = 0:5)
)
),
selection = 'none',
server = FALSE
)
However, this is not recommended for a large dataset like diamonds. It sends all rows to users which is unnecessary and takes a long time.

Rmarkdown Download data with download button for that row

Update
renderdatatable doesnt show actionbutton, renderDT shows but not able to just download the table although i can see the actionbutton being triggered with cat statement
I'm new to markdown trying to build a markdown application which needs to download data depending on the action/download button. In my example below, id like to have a downloadButton or downloadLink to download the row contents of the download button row, if i click on the first action button then id like to download mtcars 1st row values for mpg, cyl, disp to a csv or excel.
I have a fairly large subset in the actual application so i can filter accordingly.
The problem is i am not getting an action button but just raw html in my DT, not sure if i am missing small details.
---
title: "Download data with download button"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
library(downloadthis)
library(dplyr)
```
```{r, echo=FALSE}
shinyInput <- function(FUN, n, id, ...) {
vapply(seq_len(n), function(i){
as.character(FUN(paste0(id, i), ...))
}, character(1))
}
downloadButtonRmd <- function (outputId, label = "Download", class = NULL, ...) {
tags$a(id = outputId, class = paste("btn btn-default shiny-download-link",
class), href = "", target = "_blank", download = NA,
icon("download"), label, ...)
}
tab <- data.frame(head(mtcars[1:3]))
tab <- tab %>% mutate(
dl1 = shinyInput(actionButton, nrow(.), 'button_', label = "Download", onclick = 'Shiny.onInputChange(\"select_button\", this.id)' ),
dl2 = shinyInput(downloadButtonRmd, nrow(.), 'button_', label = "Download",onclick = 'Shiny.onInputChange(\"select_button1\", this.id)' ))
# renderDataTable({
# tab %>%
# datatable(extensions = 'Buttons',
# options = list(dom = 'Blfrtip',
# buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
# lengthMenu = list(c(10,25,50,-1),
# c(10,25,50,"All"))))
# })
renderDT({
datatable(tab,
options = list(pageLength = 25,
dom = "rt"),
rownames = FALSE,
escape = FALSE)})
observeEvent(input$select_button1, {
selectedRow <<- as.numeric(strsplit(input$select_button1, "_")[[1]][2])
cat(input$select_button1)
downloadHandler(filename = "Academic Report.csv",
content = function(file) {write.csv(tab[selectedRow,1:3], file, row.names = FALSE)},
contentType = "text/csv")
})
```
I've read through these links and many others but i'm unable to get what I intended
RShiny Download Button Within RMarkdown
R Shiny: Handle Action Buttons in Data Table
Thanks
Here is a way with the downloadthis package.
---
title: "DT download row"
author: "Stéphane Laurent"
date: "21/03/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(DT)
library(downloadthis)
htmltools::tagList( # for the icons
rmarkdown::html_dependency_font_awesome()
)
```
```{r}
dat <- mtcars
dat[["Download"]] <- vapply(1L:nrow(mtcars), function(i){
as.character(
download_this(
.data = mtcars[i, ],
output_name = paste0("mtcars - row ", i),
output_extension = ".csv",
button_label = "Download",
button_type = "primary",
icon = "fa fa-save",
csv2 = FALSE,
self_contained = TRUE
)
)
}, character(1L))
```
```{r}
datatable(
dat,
escape = FALSE,
options = list(
columnDefs = list(
list(targets = ncol(dat), orderable = FALSE),
list(targets = "_all", className = "dt-center")
)
)
)
```
Of course that won't work if you edit the table.

Why are my DT Tables not generating tabBox/tabPanel?

This is a problem I can't seem to solve and I'm not sure why its happening.
My goal is to use the tabBox to put various tables in a single "flexdashboard area" and using {.tabset} is not an option. But it seems that when I start using tabPanels - only the first panel will generate my DT table and the second will not. The headers of the table are generating but I can't figure our why the actual data will not.
I'll be grateful for any help.
Here is what I see:
Tab 1
Tab2
Here is my code
---
title: "Example Demo Dash"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
# Tab 1
```{r setup, include=FALSE}
library(readr)
library(DT)
library(dplyr)
library(rsconnect)
require(flexdashboard)
library(shiny)
library(shinydashboard)
require(plotly)
```
Column
-----------------------------------------------------------------------
### Tab 1 Graph
# Tab 2
Column {data-width=450}
-----------------------------------------------------------------------
### Cars Tab 1
```{r}
cars <- datatable(mtcars,
rownames = FALSE,
options = list(
dom = 't',
paging = FALSE,
info = FALSE,
scrollY = FALSE,
sort = FALSE
)
)
iris <- datatable(mtcars,
rownames = FALSE,
options = list(
dom = 't',
paging = FALSE,
info = FALSE,
scrollY = FALSE,
sort = FALSE
)
)
```
```{r}
tabBox(width=18, height = "500px" ,
#Dec 2020
tabPanel("Cars 1", cars),
#Jan 2021
tabPanel("Iris", iris))
```
### Col 2 Row 2
Column {data-width=200}
-----------------------------------------------------------------------
### Tab 2 Col 2 Graph 1
### Tab 2 Col 2 Graph 1
### Tab 2 Col 2 Graph 3
### Tab 2 Col 2 Graph 3
Column {data-width=350}
-----------------------------------------------------------------------
### Col 3 graph 1
### Col 3 graph 2
# tab 3

Resources