I found, It's very easy to replicate Patterns 2-4 in rmarkdown - One just needs to remove the lines related to output
variable (rmarkdown does not need them - it already outputs according to its own layout, and also declares variable input
silently when you call shiny) , the rest is the same - See the cde below.
However I'm still searching for ways to replicate Pattern 1 (i.e. accessing session
variable)
---
title: "Use of Action button in RMarkdown"
output: html_document
runtime: shiny
---
```{r}
# Codes from https://shiny.rstudio.com/articles/action-buttons.html
# Pattern 1 - Command
tags$head(tags$script(src = "message-handler.js"))
actionButton("do", "Click Me")
hr()
# Pattern 2 - Delay reactions
actionButton("go", "Go")
numericInput("n", "n", 50)
# plotOutput("plot2")
hr()
# Pattern 4 - Reset buttons
actionButton("runif", "Uniform")
actionButton("reset", "Clear")
# plotOutput("plot4")
# THIS IS WHAT ONE NEEDS TO WRITE HERE:
# Pattern 1 - Command
observeEvent(input$do, {
session$sendCustomMessage(type = 'testmessage', # <-- THIS STILL DOES NOT WORK
message = 'Thank you for clicking')
} )
# Pattern 2 - Delay reactions
randomVals <- eventReactive(input$go, {
runif(input$n)
})
# output$plot2 <-
renderPlot({
hist(randomVals())
})
# Pattern 4 - Reset buttons
v <- reactiveValues(data = NULL)
observeEvent(input$runif, {
v$data <- runif(100)
})
observeEvent(input$reset, {
v$data <- NULL
})
# output$plot4 <-
renderPlot({
if (is.null(v$data)) return()
hist(v$data)
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…