Search code examples
rshinytinymce

Alternate the renderUI state of TinyMCE editor in R Shiny


I am trying to alternate the presence of a TinyMCE editor in R Shiny.

I can load the editor, then remove it with the respective actionButtons. However, upon attempting to load it more than once, only a textAreaInput-type interface is rendered:

library(shiny)
library(ShinyEditor)
ui <- fluidPage(
  use_editor("API-KEY"),
     uiOutput("tiny"),
      actionButton("load", "Load TinyMCE"), 
  actionButton( "remove", "Remove TinyMCE" ))
server <- function(input, output, session) {
  observeEvent(input$load, {
 output$tiny = renderUI( editor('textcontent'))})
    observeEvent(input$remove, {
 output$tiny = renderUI( NULL)})
}
shinyApp(ui = ui, server = server)

How would it be possible to reload it? Thank you.


Solution

  • I would try that:

    library(shiny)
    library(ShinyEditor)
    
    ui <- fluidPage(
      use_editor("API-KEY"),
      uiOutput("tiny"),
      actionButton("btn", "Load/Remove TinyMCE"), 
    )
    
    server <- function(input, output, session) {
      output$tiny <- renderUI({
        if(input$btn %% 2 == 0) {
          editor('textcontent')
        } else {
          NULL
        }
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    And if that doesn't work I would hide it instead of removing it:

    library(shiny)
    library(ShinyEditor)
    
    ui <- fluidPage(
      use_editor("API-KEY"),
      conditionalPanel(
        condition = "input.btn %% 2 == 0",
        uiOutput("tiny")
      ),
      actionButton("btn", "Load/Remove TinyMCE"), 
    )
    
    server <- function(input, output, session) {
      output$tiny <- renderUI({
        editor('textcontent')
      })
    }
    
    shinyApp(ui = ui, server = server)