Sky_7
Sky_7

Reputation: 97

R Shiny: Copying cells from excel and pasting them into Shiny app and then creating a Datatable with them

I am developing an R Shiny application where I need to develop the following capability -

I need to copy rows of cells from Excel (one column at a time to start with) and paste them in Shiny maybe by using selectizeInput, textInput or textAreaInput.

How data looks like in Excel -

Image of data in Excel

Next I need to create a render datatable having this values in a single column spanning over as many rows as there were in the Excel sheet. So far, I have come across this question as reference but adding to this does not let me create the datatable as the output from this are all single vectors.

What I have tried so far -

library(shiny)
library(DT)

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"),
  
  dataTableOutput("table1")
)

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"))
  )
  
  df <- reactive({
    df <- as.data.frame(paste(strsplit(input$pasted2, "\n")))
    
  })
  
  output$table1 <- renderDataTable({
    df()
  }, 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)

Output I am getting -

Image of Output

I need each record to be in separate rows and not be in a single row and if possible have data type as it was in Excel. Can someone please help me

EDIT

Using this code -

    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)

I am not being able to copy and paste the following text from Excel into the app -

Excel text

The text is getting split into several components as show in the following image-

enter image description here

And I am getting the following error in R -

enter image description here

Can someone please help me solve this problem? I think it has got something to do with collapse = "\n"

Upvotes: 3

Views: 1436

Answers (1)

gokhan can
gokhan can

Reputation: 209

Simple solution for this part:

df <- reactive({
    if (input$pasted2 != '') {
      df <- as.data.frame(paste(input$pasted2, collapse = "\n"))
    }
  })

EDIT: The answer before that is not working. Here is the new one! You can use fread() function from data.table package. Solution is (i've tried it):

 df <- reactive({
    if (input$pasted2 != '') {
      df <- fread(paste(input$pasted2, collapse = "\n"))
    }
  })

Upvotes: 1

Related Questions