【发布时间】:2021-10-14 22:45:10
【问题描述】:
我正在开发一个 R Shiny 应用程序,我需要开发以下功能 -
我需要从 Excel 中复制多行单元格(从一列开始),然后使用 selectizeInput、textInput 或 textAreaInput 将它们粘贴到 Shiny 中。
数据在 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)
我得到的输出 -
我需要每条记录在单独的行中,而不是在单行中,并且如果可能的话,具有 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