Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny: Copying Cells from Excel into SelectizeInput

Tags:

r

shiny

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:

Excel

What we want (each row from Excel is its own entry in the text field)

Separated Data

What the app currently does when we paste the rows from Excel (each row from Excel combines into one entry):

Combined Data

Is there a way to copy cells into a SelectizeInput-like text field and somehow separate each cell whwen copied from Excel? Thank you.


Edit 1

Exceptions:

1) When rows have the same values:

enter image description here

only one string shows but what is needed is 4 "Female" labels (if possible)

enter image description here

2) Cells have values with a space:

enter image description here

each label is separated by a space, and repeat words like DAY are only shown once:

enter image description here

Ideally this is what is needed:

enter image description here


Edit 2

When delimiter is set to /n, everything is combined into one label instead rather than separate labels

enter image description here

like image 237
karuno Avatar asked Sep 05 '25 03:09

karuno


1 Answers

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.

enter image description here

like image 89
Brian Avatar answered Sep 07 '25 23:09

Brian