r Shiny #r #shiny中突出显示的文本输入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r Shiny #r #shiny中突出显示的文本输入相关的知识,希望对你有一定的参考价值。

library(shiny)

ui <- fluidPage(

  h2("Highlight Input Example"),
  
  # sample text for selection
  p("The insured received an examination of the large intestine in 2014. 
    As a result, everything was normal and there was no need to go back 
    to the follow-up consultation and any examination and treatment."),
  
  # shiny text input
  textInput("mytext1", "mytext1"),
  textInput("mytext2", "mytext2"),
  textInput("mytext3", "mytext3"),
  
  # submit
  actionButton("submit", "Submit"),
  hr(),
  
  # display submited inputs
  verbatimTextOutput("results"),
  
  # javascript code to send data to shiny server
  tags$script('
              function getSelectionText() {
                var text = "";
                if (window.getSelection) {
                  text = window.getSelection().toString();
                } else if (document.selection) {
                  text = document.selection.createRange().text;
                }
                return text;
              }
              
              document.onmouseup = document.onkeyup = document.onselectionchange = function() {
                var selection = getSelectionText();
                Shiny.onInputChange("selected_text", selection);
              }
              
              document.onclick = function() {
                var focusedElement = document.activeElement.id;
                Shiny.onInputChange("focus_input", focusedElement);
              }
              ')
  )

server <- function(input, output, session) {
  
  # update text input
  observe({
    updateTextInput(session, input$focus_input, value = input$selected_text)
  })
  
  # initiate reactive values
  values <- reactiveValues(
    results = NULL
  )
  
  # store submited inputs
  observeEvent(input$submit, {
    values$results <- c(input$mytext1, input$mytext2, input$mytext3)
  })
  
  # render submitted inputs
  output$results <- renderPrint({
    values$results
  })
  
}

shinyApp(ui, server)

以上是关于r Shiny #r #shiny中突出显示的文本输入的主要内容,如果未能解决你的问题,请参考以下文章

给定十六进制代码的查找,如何有条件地格式化 Shiny 中的文本?

“下一步”按钮使用 Bootstrap 显示为 R Shiny DataTable 的文本

R Shiny DataTable 选定的行颜色

让日文字符在 R Shiny 中显示

R/Shiny 图不显示在浏览器中

R Shiny:如何构建动态 UI(文本输入)