【问题标题】:Convert Shiny App R code to Rmarkdown Shiny App code: with observeEvent and eventReactive将 Shiny App R 代码转换为 Rmarkdown Shiny App 代码:使用 observeEvent 和 eventReactive
【发布时间】: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


【解决方案1】:

我发现,在 rmarkdown 中复制模式 2-4 非常容易 - 只需删除与 output 变量相关的行(rmarkdown 不需要它们 - 它已经根据自己的布局输出,并且还声明变量 input 在您调用 shiny) 时静默),其余部分相同 - 请参见下面的 cde。

但我仍在寻找复制模式 1 的方法(即访问 session 变量)

---
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)
})

【讨论】:

    猜你喜欢
    • 2019-05-20
    • 2019-04-28
    • 2021-10-16
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2023-01-22
    • 1970-01-01
    相关资源
    最近更新 更多