How to select the text in verbatimTextOutput by default in Shiny?

前端 未结 1 1500
你的背包
你的背包 2021-01-15 02:08

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

相关标签:
1条回答
  • 2021-01-15 02:40

    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.

    0 讨论(0)
提交回复
热议问题