我试图从 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 代码。
下面我将代码集成到您的应用中。
完整示例如下:
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/