Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...