0

I have a shiny app which lets the user copy cells from Excel and paste in a textAreaInput in order to create a datatable out of the pasted cells. So far my code has worked with reference to this question - R Shiny: Copying cells from excel and pasting them into Shiny app and then creating a Datatable with them

ui <-fluidPage(
 textAreaInput("pasted", "paste text here"), 
 dataTableOutput("table")
)

server <- function(input, output, session) {
    df_table <- reactive({ 
      if (input$pasted != '') {
        df_table <- fread(paste(input$pasted, collapse = "\n"))
        df_table <-as.data.frame(df_table)
        colnames(df_table) <- c("Method")
        df_table
        
      }
      
    })
    
    output$table <- renderDataTable({
      df_table()
    }, filter="top", class = 'hover cell-border stripe', editable= TRUE,extensions= 'Buttons',
    options = list(dom = 'Bfrtip',pageLength =10,
                   buttons = c('copy','csv','excel','pdf','print'), scrollX=TRUE),server=FALSE)

}

shinyApp(ui, server)

This works alright when I am pasting certain text and numbers into the textAreaInput. For example when I paste

enter image description here

I get the desired result- enter image description here

  1. But when I am pasting certain text into the textAreaInput it is not working properly. For example when I paste

enter image description here

I get a wrong result as "a" is missing -

enter image description here

  1. Also, when I paste the following -

enter image description here

I get a wrong result -

enter image description here with a warning in R saying -

enter image description here

I think this problem is because of the dashes (-) which is a special character in this case.

As a bigger picture, I think all these problems have something to do with the collapse ="\n" in paste within fread. Can someone please help me solve these problems?

EDIT Funnily, I am not facing this issue no 2. when I am pasting two columns at the same time - enter image description here

Sky_7
  • 77
  • 1
  • 10
  • 1
    Try with adding `header = FALSE` to `fread`. By default it's set to "auto" and will treat the first row as column names if these are characters. That's why all data shows up if you paste only numerics. – stefan Aug 20 '21 at 16:10
  • Thank you for solving the first issue. It helped a lot. Can you please help me solve the second issue? – Sky_7 Aug 20 '21 at 16:29
  • ... the second issue has to do with the seperators. `fread` treats the space as separator. Hence you end up with 9 column names in the first row but only three columns of data in the second. But not sure how to tackle this issue. Maybe you have to add an option to choose the separator. Or inform the user which character will be used as separator – stefan Aug 20 '21 at 16:39
  • Yes, I got that. Funnily it is working when I am pasting two columns together. I have updated the original post to show this under the Edit section. – Sky_7 Aug 20 '21 at 16:50
  • Not an answer to your question, but just as alternate way of making a table, I like using rhandsontable to copy and paste from excel tables. – Silentdevildoll Aug 20 '21 at 21:08
  • Can you give an example in this context? I can try implementing that instead if that deals with the separator issue. – Sky_7 Aug 23 '21 at 08:22

0 Answers0