2017-12-16 3 views
1

id = "textBox"인 텍스트 영역에 편집기 클래스를 적용하려면이 코드를 작성했습니다. 편집기 클래스를 적용하려면 실행할 자바 스크립트 코드가 필요하며 자바 스크립트 실행을 앱 시작 이벤트 (또는 앱로드시)에 바인딩하는 방법을 찾고있었습니다. 본질적으로 나는 그것이 현재 구현되어 있기 때문에 단추 누르기에 바인딩 할 필요없이 응용 프로그램이로드 될 때 js를 실행하고 싶습니다. 앱로드 이벤트를 참조하는 방법을 모르겠습니다. 버튼 구현은 내가 생각해 내고 묻기 전에 상당한 시간을 들였습니다.반짝이는 응용 프로그램이로드 될 때 JS 스크립트를 실행하는 방법

코드를 실행하는 방법에 대한 자세한 내용은 this 질문을 참조하십시오.

더 자세한 정보가 필요하면 알려주세요. 간절히 기뻐할 것입니다. 미리 감사드립니다.

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

답변

1

앱을 열 때 자바 스크립트 코드를 실행하려면 관찰자에게 코드를 입력 할 필요가 없습니다. 다음 코드를 사용하여 서버를 교체하는 경우에는 로그 라인 로그에 다음 줄이 ReferenceError: Can't find variable: CodeMirror하지만 "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%");') 
    } 

를 포함 할 것을 볼 수 있습니다, 그래서 당신의 코드에없는 뭔가가 생각?

희망이 도움이됩니다.

+0

참조 오류는 앱의 www 폴더에 몇 개의 javascript 및 css 파일이 없기 때문에 발생합니다. 그들을 추가하고 다시 시도해 주시겠습니까. 난 당신의 솔루션을 시도하고 runjs가 자동으로 실행되지 않습니다. 나는 이것을 전에 시도했다. 위의 질문에서 링크의 파일을 찾을 수 있습니다. 감사!! –

관련 문제