javascript - 如何获取 Shiny 文本区域输入中的光标位置

标签 javascript r shiny

有谁知道我如何在 Shiny 的应用程序中获取 textAreaInput 内的光标位置?

library(shiny)

ui <- fluidPage(
  textAreaInput("hop"
                ,label="textarea",value = "Supercalifragilisticexpialidocious"),
  verbatimTextOutput("out")
)

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

  output$out <- renderText({
    "here I would like to get the cursor position (an interger?) \n inside que textArea"

  })

}

shinyApp(ui, server)

我想我必须使用javascript,但我不知道从哪里开始。

问候

最佳答案

这是我找到的解决方案:

library(shiny)

ui <- fluidPage(tags$head(tags$script(
  'Shiny.addCustomMessageHandler("prout",
  function(NULL) {

   var ctl = document.getElementById("hop");
    var startPos = ctl.selectionStart;
  var endPos = ctl.selectionEnd;
  alert(startPos + ", " + endPos);

  });'
    )),
  textAreaInput("hop"
                ,label="textarea",value = "Supercalifragilisticexpialidocious"),
  verbatimTextOutput("out"),
  actionButton("hop","hop")
)

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

  output$out <- renderText({
    "here I would like to get the cursor position (an interger?) \n inside que textArea"

  })

  observeEvent(input$hop,{
    message("hop")
    session$sendCustomMessage(type="prout",message=list(NULL))
  })
}

shinyApp(ui, server)

关于javascript - 如何获取 Shiny 文本区域输入中的光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50430219/

相关文章:

javascript - 迭代多边形内的每个点

javascript - Ajax 与表单方法作为 Node.js 中的 post

EventListener 中的 JavaScript if 语句无法正常工作

r - 按行计算超过数据框中值的列数

r - 了解 Shiny 中的 react 函数

javascript - ASP.NET MVC 和 WebAPI 使用数据库表插入和检索记录

用 dplyr 无条件替换数据框中的值

r - 缩小绘图宽度以为ggrepel标签腾出更多空间

r - Shiny 的 reactiveUI 挂起,同时对同一条件变量进行多个 uiOutput 调用

r - 如何使用单独的 R 中的输入创建 Shiny 的 react 函数