Deepak Sadulla
Deepak Sadulla

Reputation: 373

How to trigger JS Script when shiny app loads

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

Upvotes: 1

Views: 2654

Answers (1)

Florian
Florian

Reputation: 25385

To run javascript code on opening the app, you don' t need to put the code in an observer. If you replace your server with the following code, you will see that the log will contain the line "hello, this code has run."

  server <- function(input, output){
    output$textStringToDisplay <- renderUI(
      tags$textarea(id="textBox", name = "Feedback", paste0(sample(letters,15),collapse = "")))

    shinyjs::runjs(
      'console.log("hello, this code has run.");
       var editorR = CodeMirror.fromTextArea(textBox, {
                       mode: "r",
                       lineNumbers: true,
                       smartindent: true});
                       editorR.setOption("theme", "cobalt");
                       editorR.setSize("100%","100%");')
  }

Although the next line in the log is ReferenceError: Can't find variable: CodeMirror, so I guess there is something missing from your code?

Hope this helps!

Upvotes: 2

Related Questions