我正在开发一个 R Shiny 应用程序,我需要开发以下功能 -
我需要从 Excel 中复制单元格行(开始时一次一列),然后使用 selectizeInput、textInput 或 textAreaInput 将它们粘贴到 Shiny 中。
数据在 Excel 中的样子 -
Excel 中的数据图像 https://i.stack.imgur.com/bs44B.png
接下来,我需要创建一个渲染数据表,该数据表在单列中包含与 Excel 工作表中一样多的行。到目前为止,我遇到过this https://stackoverflow.com/questions/59773066/r-shiny-copying-cells-from-excel-into-selectizeinput问题作为参考,但添加到此并不能让我创建数据表,因为它的输出都是单个向量。
到目前为止我尝试过的 -
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)
我得到的输出 -
输出图像 https://i.stack.imgur.com/ddcmj.png
我需要每条记录位于单独的行中,而不是位于单个行中,并且如果可能的话,具有与 Excel 中相同的数据类型。有人可以帮帮我吗
EDIT
使用此代码 -
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 中收到以下错误 -
有人可以帮我解决这个问题吗?我认为这与折叠=“\n”