R Shiny:从 excel 复制单元格并将它们粘贴到 Shiny 应用程序中,然后用它们创建一个数据表

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

我正在开发一个 R Shiny 应用程序,我需要在其中开发以下功能 -

我需要复制 Excel 中的单元格行(每次一列开始)并将它们粘贴到 Shiny 中,也许可以使用 selectizeInput、textInput 或 textAreaInput。

Excel-

中的数据情况

Image of data in Excel

接下来,我需要创建一个渲染数据表,在单个列中包含此值,跨越与 Excel sheet 中一样多的行。到目前为止,我遇到了 个问题作为参考,但是添加到这个问题并不能让我创建数据表,因为它的输出都是单个向量。

到目前为止我尝试了什么 -

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)

我得到的输出 -

Image of Output

我需要每条记录都在单独的行中,而不是在一行中,如果可能的话,数据类型与 Excel 中的一样。有人可以帮我吗

编辑

使用此代码 -

    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)

我无法将 Excel 中的以下文本复制并粘贴到应用程序中 -

文本被分成几个部分,如下图所示-

我在 R 中收到以下错误 -

有人可以帮我解决这个问题吗?我认为它与 collapse = "\n"

有关

这部分的简单解决方案:

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

编辑: 之前的答案是行不通的。这是新的!您可以使用 data.table 包中的 fread() 函数。解决方案是(我试过了):

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