【问题标题】:Select text in htmlOutput based on datatable in shiny根据闪亮的数据表在htmlOutput中选择文本
【发布时间】:2017-03-02 04:22:22
【问题描述】:

我有以下闪亮的应用程序:

library(shiny)
ui <- fluidPage(
   titlePanel("Datatable for dynamic text selection"),
   sidebarLayout(
      sidebarPanel(
        dataTableOutput("pairs")
      ),
      mainPanel(
       strong("Sentence"), htmlOutput("content"),
       strong("Selection"),textOutput("selection")
      )
   )
)

server <- function(input, output) {
   output$content <- renderText("A sample sentence for demo purpose")
   df <- data.frame(SrNo=1:5, Pairs=c("A sample", "sample sentence", 
                                      "sentence for", "for demo", "demo purpose"))
   output$pairs <- renderDataTable(datatable(df, selection = "single" ))

   observeEvent(input$pairs_cell_clicked,{
     info = input$pairs_cell_clicked
    if(is.null(info$value)) return()
    output$selection <- renderText(info$value)
   })
   }

shinyApp(ui = ui, server = server)

应用程序在htmlOutput 中显示一个句子,在datatable 中显示相应的一对单词。目前单击数据表中的任何一对单词都会在Selection下显示它。

如何修改代码,使其不显示这对单词,而是在htmlOutput 中显示为选择?

截图

【问题讨论】:

  • 您能否详细说明您希望输出的样子?可以截图吗?
  • 我自己用鼠标选择了sample sentence,如截图所示,我希望通过在数据表中选择对来完成此选择
  • 当你选择2而不是那里的例句时会发生什么?
  • 如果选择了2,则不会发生任何事情,交互仅用于选择一对单词

标签: r datatables shiny


【解决方案1】:

您可以使用gsub 将选定的文本包裹在带有CSS 属性的span 中以更改背景颜色。

在您的 server.R 中,您可以尝试(代码不变的省略号):

server <- function(input, output) {
  sample_text = "A sample sentence for demo purpose";
  output$content <- renderText(sample_text)

  .....

  observeEvent(input$pairs_cell_clicked,{

   .....

    output$content <- renderText(HTML(gsub(info$value,paste0("<span style='background-color:orange'>",info$value,"</span>"),sample_text)))
  })
}

编辑:

要模仿用户使用鼠标选择文本,您可以这样做:

select_text = JS(
                 'table.on("click.td", "tr", function () {
                            contentdiv = document.getElementById("content");
                            var selectedCell=this.lastChild;
                            var sentence = contentdiv.innerHTML;
                            var target = selectedCell.innerHTML;
                            var sentenceIndex = sentence.indexOf(target); 
                            selection = window.getSelection();
                            range = document.createRange();
                            range.setStart(contentdiv.firstChild, sentenceIndex);
                            range.setEnd(contentdiv.firstChild, (sentenceIndex + target.length));
                            selection.removeAllRanges();
                            selection.addRange(range);
                  })'
              )                                              



server <- function(input, output) {
  sample_text = "A sample sentence for demo purpose";
  output$content <- renderText(sample_text)
  df <- data.frame(SrNo=1:5, Pairs=c("A sample", "sample sentence", 
                                     "sentence for", "for demo", "demo purpose"))
  output$pairs <- renderDataTable({datatable(df, selection = "single", callback=select_text)})

  observeEvent(input$pairs_cell_clicked,{
    info = input$pairs_cell_clicked
    if(is.null(info$value)) return()
    output$selection <- renderText(info$value)  
    })
}

JS 灵感来自 this answer

【讨论】:

  • 突出显示无济于事。我真的需要在句子中选择对,因为我进一步计划提供按钮来将选择扩展到对的左侧和右侧。
  • 你能把选择翻译成编程术语吗?您可以使用regexpr 来获取匹配的索引
  • 我认为this post 在某种程度上是相关的。抱歉,我无法提出确切的术语
  • 嗯,好吧,我仍然认为在这里使用span 更容易,您可以通过使用正则表达式将标签向上或向下移动一个单词来轻松扩展span,并将其之间的文本检索为好吧。
  • 我明白了,所以当用户单击数据表时,您想以编程方式使用鼠标模拟单击和拖动。像这样的东西? stackoverflow.com/questions/985272/…
猜你喜欢
  • 2018-09-20
  • 1970-01-01
  • 2020-08-12
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
  • 2018-12-16
  • 1970-01-01
相关资源
最近更新 更多