R shiny: Copying Cells from Excel into SelectizeInput

ε祈祈猫儿з 提交于 2020-06-09 04:53:06

问题


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:

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

2) Cells have values with a space:

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

Ideally this is what is needed:


Edit 2

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


回答1:


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.



来源:https://stackoverflow.com/questions/59773066/r-shiny-copying-cells-from-excel-into-selectizeinput

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!