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

问题描述

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

我需要从 Excel 中复制多行单元格(一次一列开始),然后可能使用 selectizeInput、textInput 或 textAreaInput 将它们粘贴到 Shiny 中。

数据在 Excel 中的样子 -

Excel 中的数据图像

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

到目前为止我尝试过的 -

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)

我得到的输出 -

输出图像

我需要每条记录在单独的行中,而不是在单行中,并且如果可能的话,数据类型与 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 复制并粘贴到应用程序中 -

Excel 文本

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

在此处输入图像描述

我在 R 中收到以下错误 -

在此处输入图像描述

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

标签: rshinyshinydashboard

解决方案


这部分的简单解决方案:

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

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

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

推荐阅读