R shiny: Copying Cells from Excel into SelectizeInput

后端 未结 1 1950
庸人自扰
庸人自扰 2021-01-22 14:41

I want to be able to copy rows of text from Excel and paste it into the text field in our app like this:

Highlight all cells and copy into the Sample labels text field:<

相关标签:
1条回答
  • 2021-01-22 15:39

    Here is a minimal example that works for me.

    library(shiny)
    
    ui <- fluidPage(
      selectizeInput(
        "foo", 
        label = "inputs",
        choices = "", 
        multiple = T,
        options = list(delimiter = " ", create = T)
        ),
      textOutput("results")
    )
    
    server <- function(input, output, session) {
      output$results <- renderText(
        paste(paste("item", input$foo), collapse = " || ")
      )
    }
    
    shinyApp(ui, server)
    

    If you take out the delimiter = " " option, the behavior reverts to the undesired default. When copying from Excel, items are concatenating with spaces, where selectize.js is expecting commas.

    https://shiny.rstudio.com/articles/selectize.html

    https://github.com/selectize/selectize.js/blob/master/docs/usage.md


    Why I think this is the wrong approach:

    library(shiny)
    
    ui <- fluidPage(
      selectizeInput(
        "foo", 
        label = "inputs",
        choices = "", 
        multiple = T,
        options = list(
          delimiter = " ", 
          create = T
          )
      ),
      textOutput("results"),
    
      hr(),
    
      "textInput",
      textInput("pasted1", "paste text here"), 
    
      h5("Raw hex code points (20 is space, 0a is linefeed"),
      textOutput("verb1"), 
      h5("Vector of results from splitting on '\\n'"),
      textOutput("split1"),
    
      hr(),
    
      "textAreaInput",
      textAreaInput("pasted2", "paste text here"), 
    
      h5("Raw hex code points (20 is space, 0a is linefeed"),
      textOutput("verb2"), 
      h5("Vector of results from splitting on '\\n'"),
      textOutput("split2")
    )
    
    server <- function(input, output, session) {
      output$results <- renderText(
        paste(paste("item", input$foo))
      )
    
      output$verb1 <- renderPrint(charToRaw(input$pasted1))
    
      output$split1 <- renderText(
        paste(strsplit(input$pasted1, "\n"))
        )
    
      output$verb2 <- renderPrint(charToRaw(input$pasted2))
    
      output$split2 <- renderText(
        paste(strsplit(input$pasted2, "\n"))
      )
    }
    
    shinyApp(ui, server)
    

    I think that selectizeInput, like textInput, sanitizes all whitespace, including newlines, to be single spaces. If you use textAreaInput as your container, it will copy the pasted text verbatim, and you can do the splitting on newlines yourself, then use that vector wherever you were going to use the choices returned by selectizeInput.

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