bretauv
bretauv

Reputation: 8587

R ShinyAce : non reactive checkboxInput

I'm writing a Shiny app with ShinyAce in order to display reactive code. I used the first example of https://trestletech.github.io/shinyAce/ as a base for my code but I have a problem concerning reactive checkboxInput.

I would like to reactively display some code : for example, if I tick a box, then some code appears and if I un-tick it, the code goes back to normal. This works with actionButton (cf example on the website) but I can't figure out why it does not with checkboxInput.

Here's a reproducible example :

library(shiny)
library(shinyAce)

init <- "first text"

ui <- shinyUI(
  pageWithSidebar(
    headerPanel(""),
    sidebarPanel(
      actionButton("reset", "Reset"),
      checkboxInput("test", "Test", FALSE)
    ),
    mainPanel(
      aceEditor(
        outputId = "ace",
        selectionId = init
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {

  observe({
    cat(input$ace, "\n")
  })

  observeEvent(input$reset, {
    updateAceEditor(session, "ace", value = init)
  })

  observeEvent(input$test, {
    updateAceEditor(session, "ace", value = "Second text")
  })

})

shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 221

Answers (1)

amrrs
amrrs

Reputation: 6335

This is a slightly modified version of your answer. I'm using the boolean result of the checkbox input to conditionally update the Ace editor.

init <- "first text"

ui <- shinyUI(
  pageWithSidebar(
    headerPanel(""),
    sidebarPanel(
      actionButton("reset", "Reset"),
      checkboxInput("test", "Test", FALSE)
    ),
    mainPanel(
      aceEditor(
        outputId = "ace",
        value = init
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {

  observe({
    cat(input$ace, "\n")
     print(input$test)
  })

  observe({
    if(input$test){
      updateAceEditor(session, "ace", value = "Second text")
      } else {
      updateAceEditor(session, "ace", value = init)
    }})

  observeEvent(input$reset, {
    updateAceEditor(session, "ace", value = init)
   })

  })

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions