问题
I want to use Shiny Action buttons in rmarkdown file. Can you help please to rewrite the following code (from https://shiny.rstudio.com/articles/action-buttons.html) into RMarkdown?
# Codes from https://shiny.rstudio.com/articles/action-buttons.html
library(shiny)
ui <- fluidPage(
# 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")
)
server <- function(input, output, session) {
# Pattern 1 - Command
observeEvent(input$do, {
session$sendCustomMessage(type = 'testmessage',
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)
})
}
shinyApp(ui, server)
The related question is also posted here: Understanding why action buttons in Shiny don't work, when using several of them. And I also asked it here: https://community.rstudio.com/t/convert-shiny-app-r-code-to-rmarkdown-shiny-app-code/92876
回答1:
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)
})
来源:https://stackoverflow.com/questions/65670967/convert-shiny-app-r-code-to-rmarkdown-shiny-app-code-with-observeevent-and-even