How To Select The Text In Verbatimtextoutput By Default In Shiny?
This is a related question to my previous question (Is it possible to have fixed width verbatimTextOutput and have texts change lines in Shiny?). I have the following shiny app (ht
Solution 1:
Thanks for @ismirsehregal's help. Here I shared a workaround of this question. This answer uses textAreaInput in read-only mode, not verbatimTextOutput as I originally asked for. However, I am satisfied with the outcome and final appearance of the textAreaInput.
I learned how to select texts based on this post (https://stackoverflow.com/a/50745110/7669809). I also learned how to set read-only mode for the textAreaInput from this post (Make textarea readonly with jquery). Here is my code.
library(shiny)
ui <- function(request){
  fluidPage(
    column(
      width = 6,
      tags$head(
        tags$script("
      Shiny.addCustomMessageHandler('selectText', function(message) {
        $('#txt_out').select();
        $('#txt_out').prop('readonly', true);
      });
    ")
      ),
      textInput(inputId = "txt", label = "Type in some texts",
                value = paste0(rep(letters, 10), collapse = "")),
      textAreaInput("txt_out", label = "Show the texts", heigh = "300px"),
      br(),
      bookmarkButton()
    )
  )
}
server <- function(input, output, session){
  observeEvent(input$txt, {
    updateTextAreaInput(session = session, inputId = "txt_out", value = input$txt)
  })
  observeEvent(input$txt_out, {
    session$sendCustomMessage("selectText", "select")
  })
}
enableBookmarking("url")
shinyApp(ui, server)
Here is how it looks when the app runs.

Post a Comment for "How To Select The Text In Verbatimtextoutput By Default In Shiny?"