首页 > 解决方案 > 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"))),

标签: javascriptshinyrcloud

解决方案


我改编了来自 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)

我将 JS 保存到我的桌面,然后使用 File Upload GUI 加载,勾选 Upload to notebook 框以创建 RCloud Asset:

创建 Javascript RCloud 资产

单击 RCloud Asset 部分中的链接图标,您将看到加载为 notebook.R 静态资产的 JS 代码:

notebook.R 到 RCloud Asset 的接口

notebook.R 接口(Web 服务)允许您使用笔记本的相对路径调用静态资产……例如,您可以在其他笔记本中创建函数并使用该路径调用它们。


推荐阅读