r - 如何在 Shiny 中填充 tinyMCE 文本区域?

标签 r shiny tinymce

我试图从 R 中填充这个 tinyMCE 示例中的文本区域。

看起来我应该写到 output$textHolder

但是我在 observe() 函数中的声明并没有这样做。

我使用的是来自 tinymce 网站的示例。

我在这方面找不到太多支持。

这是我的服务器代码:

shinyServer(function(input, output, session) {


    observe({
      print ("observe")
      output$textHolder = renderText("XXX")

    })

  output$htmlText <- renderUI({
    req(input$typedText)
    HTML(enc2utf8(input$typedText))
  })

  output$rawText <- renderText({
    req(input$typedText)
    enc2utf8(input$typedText)
  })
})

这是我的 UI 代码:

library(shiny)
library(shinyjs)

shinyUI(
  fluidPage(
    tags$head(
      useShinyjs(),
      tags$script(src='https://cdn.tinymce.com/4/tinymce.min.js')
    ),

    fluidRow(
      titlePanel("tinyMCE Shiny"),
      br(),
      column(width=6,
             tags$form(method="post",
                       tags$textarea(id="textHolder")
             ),
             br(),
             actionButton("fetch", "Get Results!", icon=icon("lightbulb-o"),class="btn-primary",
                          onclick = "Shiny.onInputChange('typedText', tinyMCE.get('textHolder').getContent());"),
             tags$script("tinymce.init({
                         selector:'#textHolder',
                         theme: 'modern',
                         height: 200,
                         plugins: ['advlist autolink link image lists charmap preview hr','wordcount',],
                         menubar: true,
                         toolbar: 'undo redo | bold italic | bullist | link',
                         });")
  ),
  column(width=6,
         tags$style(HTML('pre {height:240px;}')),
         tags$label(`for`="rawText", "Raw String"),
         hr(),
         tags$pre(textOutput("rawText")),
         br(),
         tags$label(`for`="htmlText", "HTML Version"),
         hr(),
         tags$pre(htmlOutput("htmlText"))
  )
      )
  )
)

最佳答案

您可以考虑使用 shinyMCE 包:https://github.com/mul118/shinyMCE . (使用例如 devtools::install_github("mul118/shinyMCE") 进行安装)。

在 ui 端你会使用:

tinyMCE('editor1', 'Click to edit text')

在服务器端,您可以通过 input$editor1 访问 html 代码。

下面我将代码集成到您的应用中。

enter image description here

完整示例如下:

library(shiny)
library(shinyMCE)

server <- function(input, output, session) { 

  output$htmlText <- renderUI({
    req(input$editor1)
    HTML(enc2utf8(input$editor1))
  })

  output$rawText <- renderText({
    req(input$editor1)
    enc2utf8(input$editor1)
  })
}

ui <- shinyUI(
  fluidPage(
    fluidRow(
      titlePanel("tinyMCE Shiny"),
      br(),
      column(width = 6,
             tinyMCE('editor1', 'Click to edit text'),
             br(),
             actionButton("fetch", "Get Results!", icon=icon("lightbulb-o"),class="btn-primary",
                          onclick = "Shiny.onInputChange('typedText', tinyMCE.get('textHolder').getContent());")
  ),
  column(width = 6,
         tags$style(HTML('pre {height:240px;}')),
         tags$label(`for`="rawText", "Raw String"),
         hr(),
         tags$pre(textOutput("rawText")),
         br(),
         tags$label(`for`="htmlText", "HTML Version"),
         hr(),
         tags$pre(htmlOutput("htmlText"))
  )
      )
  )
)


shinyApp(ui, server)

关于r - 如何在 Shiny 中填充 tinyMCE 文本区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53571325/

相关文章:

jquery - 允许用户编辑 CSS

javascript - 如何让TinyMCE显示在jsfpage上?

r - 如何以与为箱线图绘制边框相同的方式在 R 中的条形图周围绘制边框

r - 带有 shinyTable 和 submitButton 的可编辑表格

r - 在Shiny-Server中发布后如何在Rmarkdown中启用表情符号以显示

r - Shiny -点击刷新后如何记住用户输入?

javascript - 在 TinyMCE 中将标签包装在标签内

r - 为什么在包含 R 中的因素的方差分析中删除变量不会降低自由度?

R从带有时间字段的日期开始按小时聚合数据框

r - 优化插入符号的灵敏度似乎仍然优化 ROC