R markdown with interactive option of the Chunk - r

I try to have a Markdown document with a verbose option (i.e. show or not the code of the page, to avoid to afraid people :D ) depending on the choice of the user (basically a radio button widget).
I therefore executed the following code:
---
title: "Hello"
author: "Charlotte S."
date: "8 février 2018"
output: html_document
runtime: shiny
---
```{r}
radioButtons("verbose", label = "",
choices = c("Yes", "No"),
selected = "No")
verboseAction <- reactive({
if(!is.null(input$verbose)){
if(input$verbose == "Yes"){
TRUE
} else {
FALSE
}
} else {
FALSE
}
})
output$print <- renderText({
verboseAction()
})
```
Here `r textOutput("print")`
```{r echo = verboseAction()}
2+2
```
the answer of R: "operation not allowed without a reactive context
Ok no problem, I did:
---
title: "Hello"
author: "Charlotte S."
date: "8 février 2018"
output: html_document
runtime: shiny
---
```{r}
radioButtons("verbose", label = "",
choices = c("Yes", "No"),
selected = "No")
verboseAction <- reactive({
if(!is.null(input$verbose)){
if(input$verbose == "Yes"){
TRUE
} else {
FALSE
}
} else {
FALSE
}
})
output$print <- renderText({
verboseAction()
})
```
Here `r textOutput("print")`
```{r echo = observe(verboseAction())}
2+2
```
not working neither
---
title: "Hello"
author: "Charlotte S."
date: "8 février 2018"
output: html_document
runtime: shiny
---
```{r}
radioButtons("verbose", label = "",
choices = c("Yes", "No"),
selected = "No")
verboseAction <- reactive({
if(!is.null(input$verbose)){
if(input$verbose == "Yes"){
TRUE
} else {
FALSE
}
} else {
FALSE
}
})
output$print <- renderText({
verboseAction()
})
```
Here `r textOutput("print")`
```{r global_options, include=FALSE}
option <- reactive({
if(!is.null(verboseAction())){
opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=verboseAction(), warning=FALSE, message=FALSE)
}
})
```
```{r}
2+2
```
Nope nope nope
Do you have an idea how to do that? I could not find any example with a reactive values in the {r}, I am not even sure its possible ... (I also tried with a tcltk but it's worse because no reactivity)...
Thanks in advance and have a good day !
Cha

Overview
You want to declare the code_folding option in your Yet Another Markup Language (YAML) header.
You can set the default code folding to automatically hide (code_folding: hide) or show (code_folding: show) the code chunks from the user.
---
title: "Hello"
author: "Charlotte S."
date: "8 février 2018"
output:
html_document:
code_folding: hide
runtime: shiny
---
```{r}
radioButtons("verbose", label = "",
choices = c("Yes", "No"),
selected = "No")
verboseAction <- reactive({
if(!is.null(input$verbose)){
if(input$verbose == "Yes"){
TRUE
} else {
FALSE
}
} else {
FALSE
}
})
output$print <- renderText({
verboseAction()
})
```
Here `r textOutput("print")`
```{r global_options, include=FALSE}
option <- reactive({
if(!is.null(verboseAction())){
opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=verboseAction(), warning=FALSE, message=FALSE)
}
})
```
```{r}
2+2
```

Related

Creating and showing vector in a reactive environtment in Shiny

I having a problem using the shiny reactive environment. My problem is that the vector is not being completed. Everytime I presss the buttom the output goes to the row bellow, but erase the previous elemnt.
This is my code:
---
title: "teste"
author: "Teste1"
date: "03/01/2022"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
actionButton("action1", "Press Button")
```
```{r, echo= FALSE, message = FALSE}
reactive({
u <- input$action1
w<- u + 2651641684
x <- c()
x[input$action1] <- paste("Texto",format(Sys.time(), "%S"))
print(x)
})
```
Why when I print(x) the vector doesnt store the previous elements when I press the button?
Any help?
Does this give you what you want?
---
title: "teste"
author: "Teste1"
date: "03/01/2022"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
actionButton("action1", "Press Button")
```
```{r, echo= FALSE, message = FALSE}
v <- reactiveValues(x=c(), u=0)
observeEvent(input$action1, {
v$u <- v$u + 1
w <- v$u + 2651641684
v$x[v$u] <- paste("Texto",format(Sys.time(), "%S"))
print(v$x)
})
```

Is there the feature to remove error message

in the below application, is there a way to get rid of red color error message ? like whenever there is a error message, we may require nothing to be printed there (Just blank). Is it possible to achieve.........................
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(DT)
library(dplyr)
library(shiny)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
selectInput("Tic", "", choices = c("", "ALL", as.character(iris$Species)), selected = NULL)
actionButton("Submit", "Submit")
textOutput("Total")
tableOutput("SUMMARY_GENERAL_table")
data2 <- reactiveVal(iris[1:2, ])
observeEvent(input$Submit, {
if (input$Tic == "") {
table_display <- iris[1:2, ]
} else if (input$Tic == "ALL") {
table_display <- iris
} else {
table_display <- iris %>% filter(Species %in% input$Tic)
}
data2(table_display)
})
output$SUMMARY_GENERAL_table <- renderTable({
data2()
})
output$Total <- renderText(
paste0("Sum ", formatC(as.numeric(sum(data2()[(data2()$Species == "virginica"), ]$Species))))
)
```
As smurfit89 mentioned above, it's generally not a good idea to do it. However, there are some situations where it would be nice, such as if you already know it will give one.
If you do insist on doing it, though, an easy way to do it is suppressWarnings(expr)

No able to use click/brush in plotlyOutput

I am working on flexdashboard. I have used the below code for one of the plots. Well, when I try to use click or brush under plotlyOutput it is throwing an error like I have used in the second below. Is there a way to use Click under plotlyOutput? Is there a way?......................................
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
runtime: shiny
source_code: embed
theme: cosmo
---
```{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(tidyverse)
library(lubridate)
library(ggplot2)
library(reshape)
library(shiny)
library(plotly)
```
```{r}
df <- read_excel("E:/Analytics/Freelancing projects/New
folder/df.xlsx")
```
Summary
=================
Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("p1",h5("Select"),choices = c("","Plot"))
selectInput("f1",h5("Components"),choices =
c("ALL",levels(factor(Copy_of_mill_para1$variable))),selected = "ALL")
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart A
```{r}
plotlyOutput("g1")
output$g1 <- renderPlotly({
if (input$p1 == "Plot") {
s_data <- df
}
if (input$p1 == "Plot" && input$f1 != "ALL") {
s_data <- s_data %>% filter(variable %in% input$f1)
}
p1 <- ggplot(s_data,aes(x=Date,y=value,color=variable))+geom_line(size =
.2)+theme(axis.text.y=element_text(angle=0,hjust=1,size=0.1))+theme(axis.title.y=element_blank(),axis.title.x=element_blank())+theme(ax
is.text.x=element_blank())+theme(legend.title = element_blank())+
theme(legend.text=element_text(size=7.5))+theme(legend.position = "none")
print(ggplotly(p1))
})
```
### Table
```{r}
verbatimTextOutput("click")
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)"
})
```

Flexdashboard doesn't work with Shiny URL state

I am trying to combine flexdashboard with Shiny state bookmarking. When used alone (example from the docs) Shiny app works fine, but when put in flexdasboard, url is not updated:
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
shinyApp(
ui=function(req) {
fluidPage(
textInput("txt", "Text"),
checkboxInput("chk", "Checkbox")
)
},
server=function(input, output, session) {
observe({
# Trigger this observer every time an input changes
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
},
enableBookmarking = "url"
)
```
Is this even possible? Compared to standalone execution:
shinyApp(
ui=function(req) {
fluidPage(
textInput("txt", "Text"),
checkboxInput("chk", "Checkbox")
)
},
server=function(input, output, session) {
observe({
# Trigger this observer every time an input changes
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
},
enableBookmarking = "url"
)
it looks like onBookmarked (and similar events like onBookmark, onRestore and onRestored) are never triggered.
Bookmarking isn't supported in Shiny apps embedded in R Markdown documents.
See discussion here: https://github.com/rstudio/shiny/pull/1209#issuecomment-227207713
Sounds like it's technically possible, but tricky to do. For example, what happens if there are multiple apps embedded in the document? Also, apps are embedded as iframes, so there would have to be some wiring up to be done to allow these apps to access/modify their parent window's URL.
However, bookmarking does work with embedded Shiny components (rather than full applications).
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
enableBookmarking("url")
```
```{r, include=FALSE}
observe({
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
output$content <- renderUI({
tagList(
textInput("txt", "Text"),
checkboxInput("chk", "Checkbox")
)
})
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
fluidPage(
uiOutput("content"),
selectInput("sel", label = "Select", choices = c(10, 20, 30), selected = 10)
)
```
You can also use Prerendered Shiny Documents, although bookmarking would not work 100% the same since the UI is pre-rendered. Any static UI would have to be manually restored with bookmarking callbacks, but dynamic UI would be restored just fine.
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
enableBookmarking("url")
```
```{r, context="server"}
observe({
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
# Static inputs are pre-rendered, and must be manually restored
onRestored(function(state) {
updateSelectInput(session, "sel", selected = state$input$sel)
})
# Dynamic inputs will be restored with no extra effort
output$content <- renderUI({
tagList(
textInput("txt", "Text"),
checkboxInput("chk", "Checkbox")
)
})
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
fluidPage(
uiOutput("content"),
selectInput("sel", label = "Select", choices = c(10, 20, 30), selected = 10)
)
```

Creating a tab after an action button has been triggered in shiny

I'm trying to generate a tab panel after a user has pressed an action button in a Rmarkdown document that's using Shiny.
Here's a minimal viable example of what I have so far (not producing the desired results):
---
title: "Test Doc"
output: html_document
runtime: shiny
---
```{r, echo = FALSE, results="hide", message=FALSE, warning=FALSE}
library(shiny)
library(rmarkdown)
```
```{r, echo = FALSE, cache=FALSE}
sidebarPanel(
actionButton("testButton", label="Test!",
icon=icon("search"))
)
```
```{r, echo = FALSE, cache=FALSE}
tab_test_1<-eventReactive(input$testButton, {
output$tab_test<-renderUI({
tabsetPanel(tabPanel("Plot"))
})
})
uiOutput("tab_test")
```
Try with
```{r, echo = FALSE, cache=FALSE}
output$tab_test<-renderUI({
req(input$testButton);
tabsetPanel(tabPanel("Plot"))
})
uiOutput("tab_test")
```

Resources