R SHINY:从EXCEL中复制单元格并将其粘贴到SHILY应用程序中,然后使用它们创建数据表 [英] R Shiny: Copying cells from excel and pasting them into Shiny app and then creating a Datatable with them

查看:0
本文介绍了R SHINY:从EXCEL中复制单元格并将其粘贴到SHILY应用程序中,然后使用它们创建数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我需要从Excel复制单元格的行(一开始一次复制一列),然后将它们粘贴到Slight中,可能使用的方法是选择输入、文本输入或文本区域输入。

数据在Excel中的外观-

Image of data in Excel

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

我到目前为止所尝试的-

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, "
"))
  )
  
  output$verb2 <- renderPrint(charToRaw(input$pasted2))
  
  output$split2 <- renderText(
    paste(strsplit(input$pasted2, "
"))
  )
  
  df <- reactive({
    df <- as.data.frame(paste(strsplit(input$pasted2, "
")))
    
  })
  
  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 = "
"))
        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中收到以下错误-

有人能帮我解决这个问题吗?我认为这与的崩溃有关

(&Q)

推荐答案

此部分的简单解决方案:

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

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

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

这篇关于R SHINY:从EXCEL中复制单元格并将其粘贴到SHILY应用程序中,然后使用它们创建数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆