本文介绍了将 Shiny App R 代码转换为 Rmarkdown Shiny App 代码:使用observeEvent 和 eventReactive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 rmarkdown 文件中使用 Shiny Action 按钮.你能帮忙重写以下代码吗(来自 https://shiny.rstudio.com/articles/action-buttons.html) 到 RMarkdown?

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)

相关问题也贴在这里:了解为什么在使用多个操作按钮时 Shiny 中的操作按钮不起作用.我也在这里问过:https://community.rstudio.com/t/convert-shiny-app-r-code-to-rmarkdown-shiny-app-code/92876

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

推荐答案

我发现,在 rmarkdown 中复制模式 2-4 非常容易 - 只需删除与 output 变量相关的行(rmarkdown 不需要它们——它已经根据自己的布局输出了,并且在你调用 Shiny 时也默默地声明了变量 input),其余的都是一样的——见下面的 cde.

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.

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

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

这篇关于将 Shiny App R 代码转换为 Rmarkdown Shiny App 代码:使用observeEvent 和 eventReactive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:06