Convert Shiny App R code to Rmarkdown Shiny App code: with observeEvent and eventReactive

亡梦爱人 提交于 2021-01-29 05:42:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!