【问题标题】:R Shiny: Copying cells from excel and pasting them into Shiny app and then creating a Datatable with themR Shiny:从 excel 复制单元格并将它们粘贴到 Shiny 应用程序中,然后用它们创建一个数据表
【发布时间】:2021-10-14 22:45:10
【问题描述】:

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

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

数据在 Excel 中的样子 -

Image of data in Excel

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

到目前为止我所尝试的 -

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"

有关

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    这部分的简单解决方案:

    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"))
        }
      })
    

    【讨论】:

    • 您好,此解决方案仅在粘贴 Excel 中的值后才有助于加载数据表。我需要在单独的行中获取记录,而不是在单行中,如图所示。我还更新了图像以消除任何疑问。 1,2,3,4 需要像在 Excel 中一样位于数据表的单独行中
    • 感谢您的解决方案。我将使用它来开发更复杂的应用程序。请在您的原始答案中更新此内容,以便我对此表示赞同。如果您觉得这个问题有帮助,请点赞!
    • 成功了!谢谢!
    • 虽然,这是可行的,但你能在手动输入时告诉我吗?警告:fread 中的错误:文件“1”不存在或不可读。 getwd()=='D:/文件夹 1/P4'
    • 使用它我无法复制和粘贴诸如“奶昔 - 巧克力牛奶 - 10 公斤盒装”之类的文本,因为它被分成几列并且我收到了警告。我已经用所需的图像编辑了原始问题。请帮忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多