Exclude tabs Rmarkdown based on parameters - tabs

I know I can include exclude r code chunks using parameters in Rmarkdown.
http://rmarkdown.rstudio.com/developer_parameterized_reports.html
However how can I exclude or include tabs based on the value of a parameter.
Where a tab is indicated as:
## Header {.tabset}
### Tab 1
content Tab 1
### Tab 2
content Tab 2
##
I'm looking for something like
## Header {.tabset}
### Tab 1
content Tab 1
ifelse(param == False) {
### Tab 2
content Tab 2
}
##
Update
I have some troubles with the answer by StatnMap. Using this code, in the first chunk the HTML after the R chunk is still shown as is the R chunk itself. I could fix this by using a separate eval = FALSE for the R chunk, but I would rather restrict myself to a single parameter in a single chunk. Thus, to only set eval = FALSE in the asis chunk.
## HEADER {.tabset .tabset-pills}
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
``` {asis, echo = TRUE, eval = FALSE}
### TEST1
```{r echo=FALSE, warning=FALSE}
library(dplyr)
summary(cars)
```
You can also embed plots, for example:
```
### TEST2
```{r, pressure, echo=FALSE}
plot(pressure)
```

You can include your markdown syntax in a asis chunk:
```{asis, echo=TRUE, eval=param}
### Tab 2
content Tab 2
```

Related

Function to create plot and knit to HTML in RMarkdown not working

I have a function in an R Markdown document. The function takes a "case number" variable, which is used to filter some data frames, produce a plot, and output the plot to a knit html document. Here's the basic idea:
---
title: "Plot"
output:
html_document:
toc: true
toc_float: true
toc_depth: 2
---
```{r echo=FALSE, results='hide',message=FALSE,warning=FALSE}
library(dplyr)
library(ggplot2)
library(scales)
library(lubridate)
library(plotly)
library(vistime)
```
```{r echo = FALSE, warning=FALSE, fig.align='CENTER', fig.width=10, fig.height=5}
data <- read.csv(text="event,group,start,end,color
Phase 1,Project,2016-12-22,2016-12-23,#c8e6c9
Phase 2,Project,2016-12-23,2016-12-29,#a5d6a7
Phase 3,Project,2016-12-29,2017-01-06,#fb8c00
Phase 4,Project,2017-01-06,2017-02-02,#DD4B39
Room 334,Team 1,2016-12-22,2016-12-28,#DEEBF7
Room 335,Team 1,2016-12-28,2017-01-05,#C6DBEF
Room 335,Team 1,2017-01-05,2017-01-23,#9ECAE1
Group 1,Team 2,2016-12-22,2016-12-28,#E5F5E0
Group 2,Team 2,2016-12-28,2017-01-23,#C7E9C0
3-200,category 1,2016-12-25,2016-12-25,#1565c0
3-330,category 1,2016-12-25,2016-12-25,#1565c0
3-223,category 1,2016-12-28,2016-12-28,#1565c0
3-225,category 1,2016-12-28,2016-12-28,#1565c0
3-226,category 1,2016-12-28,2016-12-28,#1565c0
3-226,category 1,2017-01-19,2017-01-19,#1565c0
3-330,category 1,2017-01-19,2017-01-19,#1565c0
1-217.0,category 2,2016-12-27,2016-12-27,#90caf9
4-399.7,moon rising,2017-01-13,2017-01-13,#f44336
8-831.0,sundowner drink,2017-01-17,2017-01-17,#8d6e63
9-984.1,birthday party,2016-12-22,2016-12-22,#90a4ae
F01.9,Meetings,2016-12-26,2016-12-26,#e8a735
Z71,Meetings,2017-01-12,2017-01-12,#e8a735
B95.7,Meetings,2017-01-15,2017-01-15,#e8a735
T82.7,Meetings,2017-01-15,2017-01-15,#e8a735")
data <- as.data.frame(data)
#event_id <- "category 1"
build_plot <- function(event_id) {
data.filtered <- data%>%
filter(group==event_id)
p <- vistime(data.filtered)
pb <- plotly_build(p)
pb #Display plot
rmarkdown::render(input = "stack_overflow_example.Rmd",
output_format = "html_document",
output_file = 'stack_overflow_example.html',
output_dir = "//mydir/myfolder")
}
build_plot("category 1")
```
I'm getting this error, and no document is output:
Error in sink(con, split = debug) : sink stack is full
Running render on a document does the same thing as knit to html. That includes running all the R code in the document.
But you shouldn't put render in the document itself - you've created a recursive loop because you call render on the document, which runs the code on the document, which calls render on the document, which runs the code in the document....
Remove the render call from your document, instead put it in a separate R script. Then, running that script will render the document.

Looking for a simple way to include profvis output in a Markdown file in R

The title pretty much says it all. I want to profile a function in R (with the profvis package) and display the output in an R Markdown file, without manually creating a screenshot and ideally with no additional packages except the ones that are loaded with profvis, e.g. htmlWidgets and htmltools.
To be more specific I imagine something like:
library(profvis)
profvis_output <- profvis(rnorm(1e06))
htmlwidgets::saveWidget(profvis_output, "provis_output.html")
and then include the thereby created html file in Markdown.
I already tried
htmltools::includeHTML("profvis_output.html")
in the Markdown file but that did not work.
NOTE: I also want it to be renderable to a PDF
This seems to work:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(profvis)
```
## profvis
This is an R Markdown document using profvis.
```{r}
profvis({
data(diamonds, package = "ggplot2")
plot(price ~ carat, data = diamonds)
m <- lm(price ~ carat, data = diamonds)
abline(m, col = "red")
})
```

Rmarkdown: Placing static content under a tabbed section

I was hoping for help placing static content under a tabbed section in my R markdown file. It's similar to this question: RMarkdown: Tabbed and Untabbed headings, but the solution doesn't account for blank lines in the table of contents.
Is it possible to end the tabbed section without starting a new section? Here's an example:
---
title: "Mtcars Example"
output:
html_document:
toc: true
toc_float: true
number_sections: true
---
# Mtcars Info
The data was extracted from the 1974 Motor Trend US magazine..
# Dataset Prep
No changes were made to the dataset..
# Plots {.tabset .tabset-pills}
```{r results = 'hide', message = FALSE, fig.height = 5, fig.width = 5, echo = FALSE}
plot1 <- ggplot(data = mtcars) +
geom_point(aes(x = mpg, y = hp))
plot2 <- ggplot(data = mtcars) +
geom_point(aes(x = mpg, y = wt))
```
## mpg vs hp
```{r, echo = FALSE}
plot1
```
## mpg vs wt
```{r, echo = FALSE}
plot2
```
#
The plots above show how mpg is related..
# Analysis
the mtcars dataset is a great exploratory dataset to show..
Section 3 is a tabbed section allowing the user to switch between plots. I'd like to have static text underneath it.
The problem is, without starting a new heading, the content is only visible when the 2nd tab is selected. Not good. I can fix this by starting a new heading and putting the content in there.. but now I have a numbered section 4 in my table of contents that's blank. Also not desirable.
Is there any way to fix this? In the Rmarkdown cheatsheet https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf, you supposedly can end a tabbed section with ###, but that doesn't seem to work either.
I am not sure about any built-in solution. In such cases I usually just go with jQuery. In this case, it is a one-liner.
Reproducible Example:
---
title: "Mtcars Example"
output:
html_document:
number_sections: true
---
<script>
$(document).ready(function() {
$('#myDiv').appendTo('#first-tab-sec');
});
</script>
<div id="myDiv">a
The quick brown fox jumps over the lazy dog.
</div>
# Plots {.tabset .tabset-pills #first-tab-sec}
## mpg vs hp
```{r, echo = FALSE}
plot(mtcars$mpg, mtcars$hp)
```
## mpg vs wt
```{r, echo = FALSE}
plot(mtcars$mpg, mtcars$wt)
```
# Next Section
We created a div element with the id myDiv. Inside that element we store the content, thats supposed to go underneath the tabbed area.
The jQuery (JS) snippet does the following:
As soon as the document finished loading ($(document).ready()), we execute the anonymous function(){...}. Inside that function we grab our div element by its id and append it to the element with the id first-tab-sec.
Multiple Tabbed Sections
If we have another tabbed-section, we just give it a new id, for example second-tab-sec, and add the code
$('#mySecDiv').appendTo('#second-tab-sec');
to the JS chunk. Here we assume that the content is contained within a div with the id mySecDiv.

How to add table of contents in rmarkdown html Shiny app

Based on this link I tried to include a table of contents in an HTML rmarkdown output. This works fine if I just knit it in RStudio, but when I try the same in Shiny, the table of contents doesn't show up. Am I doing something wrong or is this simply not possible? I also tried some custom css but that also doesn't seem to work. I need this because my users need to set some inputs and download an interactive document themselves with a toc. Please find an example below.
server.r
library(shiny)
library(rmarkdown)
library(htmltools)
library(knitr)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$Generate_PDF_Document <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = paste0("Highchart Document",format(Sys.Date(),"%d%m%Y"),".html"),
content = function(file) {
# # Copy the report file to a temporary directory before processing it, in
# # case we don't have write permissions to the current working dir (which
# # can happen when deployed).
tempReport <- normalizePath('Test.Rmd')
file.copy(tempReport, "Test.Rmd", overwrite = FALSE)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
out <- render('Test.Rmd', html_document())
file.rename(out,file)
})
})
ui.r
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
downloadButton('Generate_PDF_Document','Generate a PDF Report')
)
)
))
rmarkdown doc:
---
title: "Test"
output:
html_document:
toc: true
toc_depth: 3
toc_float: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Remove html_document() from render. I have not studied the details, but it looks like it forces override of the yaml front matter.

Can you assign id to tabs in r shiny flexdashboard?

I'm wondering if you can assign id to tabs (and other elements more generally) in r flexdashboard.
I'd like to do this so I can create conditional panels based on which tab is clicked.
I have a very limited knowledge of html.
below is an example code.
---
title: "Example"
output:
flexdashboard::flex_dashboard:
theme: simplex
orientation: rows
vertical_layout: fill
social: menu
source_code: embed
runtime: shiny
---
```{r setup, include=FALSE}
options(xtable.include.rownames=F)
library(flexdashboard)
```
Column
-------------------------------------
### Chart 1
```{r}
```
Column {.tabset}
-------------------------------------
### Chart 2
```{r}
```
### Chart 3
```{r}
```
For me, it works like this:
You attribute a 'name' to your column, for example:
Column 1 or Column mycolumn
You refer this column somewhere else in the flexdashboard, with:
[This is a column](#column-1) or
[Show me the column](#column-mycolumn)
Then, by clicking on whatever is inside the "[ ]", you'll be redirected to whats referenced in the "( )".