I wrote this code to apply the editor class on the textarea with id = "textBox". Applying the editor class needs a javascript code to run and was looking for a way to bind the javascript run to the event of starting the app (or when the app loads). Essentially I would like to run the js when the app loads without the need to bind it to a button press as it is presently implemented. I do not know how to refer to the app load event. The button implementation was the best I could come up with and have spent considerable amount of time before asking.
Refer to this question for details on how to get the code running.
Let me know if you need anymore info on this. Would be happy to oblige. Thanks in advance.
library(shiny)
library(shinyjs)
if (interactive()) {
ui <- shinyUI(
fluidPage(
useShinyjs(),
tags$head(tags$title("Title"),
tags$link(rel = "stylesheet", href = "codemirror.css"),
tags$link(rel = "stylesheet", href = "cobalt.css"),
tags$script(src = "codemirror.js"),
tags$script(src = "r.js")
),
actionButton("btn1","Click to see code"),
uiOutput(outputId = "textStringToDisplay")))
server <- function(input, output){
output$textStringToDisplay <- renderUI(
tags$textarea(id="textBox", name = "Feedback", paste0(sample(letters,15),collapse = "")))
ButtonPressCounter <- 0
observeEvent(input$btn1,
{
ButtonPressCounter <<- ButtonPressCounter + 1 # Need it to happen only once
if(ButtonPressCounter <= 1){
shinyjs::runjs(
'var editorR = CodeMirror.fromTextArea(textBox, {
mode: "r",
lineNumbers: true,
smartindent: true});
editorR.setOption("theme", "cobalt");
editorR.setSize("100%","100%");')
}
})
}
shinyApp(ui = ui, server = server) }