【发布时间】:2021-04-16 15:19:00
【问题描述】:
我想在 rmarkdown 文件中使用 Shiny Action 按钮。你能帮忙把下面的代码(来自https://shiny.rstudio.com/articles/action-buttons.html)改写成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)
相关问题也发布在这里:Understanding why action buttons in Shiny don't work, when using several of them。 我也在这里问过:https://community.rstudio.com/t/convert-shiny-app-r-code-to-rmarkdown-shiny-app-code/92876
【问题讨论】:
-
我发现在community.rstudio.com/t/… 有一个相关的讨论,Hadley 和 daattali 正在回答这个问题,但是现在已经关闭了
标签: r shiny r-markdown shinyapps flexdashboard