javascriptshinyrcloud

Saving JS Code into RCloud Asset Book and Calling it in Shiny


How would you put in java script code in an asset book and call it in shiny code? having some trouble getting it to work. I need to find a way to get that in a asset book in RCloud, and be able to call it within Rcloud, without having to reference the code online

#Link to JS Code I am trying to save inside Rcloud

tinymce.cachefly.net/4.0/tinymce.min.js

# Call asset to provide JS Code

tinymce.fn.source <- rcloud.get.asset("tinymce.js", notebook=assetsNotebook) source(textConnection(tinymce.fn.source))

Side Text Panel Code

                      fluidRow(
                        singleton(tags$head(tags$script(src ="tinymce.fn.source"))),
                        column(12, offset = 0,
                               hr(),
                               h4('Side Panel Text'),
                               uiOutput("editor1"))),

Solution

  • I adapted this example from The Dub World:

    library(rcloud.shiny)
    library(shiny)
    
    ui <- shinyUI(
        fluidPage(tags$head(tags$script(src='/notebook.R/6a4819a38814bdad910e837f0c4de702/tinymce.min.js')),
    
       # Application title
       fluidRow(
          titlePanel('tinyMCE Modal Example'),
          br(),
          actionButton('modal', 'Modal Example', icon=icon('paper-plane-o'), class='btn-success', style='margin-left:15px;',`data-toggle`='modal', `data-target`='#modalExample'),
          br(),br(),
          tags$pre(htmlOutput('modalText')),
          ### Modal ###
          tags$div(class='modal fade', id='modalExample', tabindex='-1', role='dialog',`aria-labelledby`='modalExample', `aria-hidden`='true',
             tags$div(class='modal-dialog', role='document',
                 tags$div(class='modal-content',
                     tags$div(class='modal-header', tags$button(type='button', class='close', `data-dismiss`='modal', `aria-label`='Close', tags$span(`aria-hidden`='true', 'x')),tags$h4(class='modal-title', 'HTML Editor in a modal')),
                     tags$div(class='modal-body', tags$form(method='post', tags$textarea(id='modalEditor')),tags$script("tinymce.init({selector:'#modalEditor', theme: 'modern', height: 200});")),
                     tags$div(class='modal-footer',tags$button(type='button', class='btn btn-primary', `data-dismiss`='modal', onclick="Shiny.onInputChange('modalTextArea',tinyMCE.get('modalEditor').getContent());",'Close')))
            )
          )
        )
      )
    )
    
    server <- function(input, output, session) {
      output$modalText <- renderUI({
            req(input$modalTextArea)
            HTML(enc2utf8(input$modalTextArea))
      })
    }
    
    rcloud.shinyApp(ui=ui, server=server)
    

    I saved the JS to my Desktop, then loaded using the File Upload GUI ticking the Upload to notebook box to create an RCloud Asset:

    Create Javascript RCloud Asset

    Click on the link icon in the RCloud Asset section and you will see the JS code loaded as a notebook.R static asset:

    notebook.R interface to RCloud Asset

    The notebook.R interface (web service) allows you to call a static asset using the relative path of your notebook...so you can create functions in other notebooks and call them using that path, for example.