我有一个列表列表,我想将它们写入Shiny文件(.txt或.xlsx)中。
C = list(listA = list(1:3, structure(1:9, .Dim = c(3L, 3L)), 4:9),
listB = list(c("t1", "t2", "t3"), structure(c("p1", "p2"), .Dim = 2:1)))
在R中,我可以像这样使用sink
命令:
sink("test.txt")
print(mydata)
sink()
结果是txt文件:
$listA
$listA[[1]]
[1] 1 2 3
$listA[[2]]
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
$listA[[3]]
[1] 4 5 6 7 8 9
$listB
$listB[[1]]
[1] "t1" "t2" "t3"
$listB[[2]]
[,1]
[1,] "p1"
[2,] "p2"
我如何使用Shiny中的此接收器功能为用户提供下载选项,以下载C
?以及如何删除输出中的行索引?
我尝试过print(C,row.names = FALSE)
,但是它不起作用。
我想要的输出应如下所示:
$listA
$listA[[1]]
1 2 3
$listA[[2]]
[,1] [,2] [,3]
1 4 7
2 5 8
3 6 9
$listA[[3]]
4 5 6 7 8 9
$listB
$listB[[1]]
"t1" "t2" "t3"
$listB[[2]]
[,1]
"p1"
"p2"
答案 0 :(得分:1)
使用shiny
下载文件与通常的R方式非常相似。您需要:
sink
功能例如:
library(shiny)
ui <- fluidPage(
# Runs downloadHandler in server part
downloadButton("downloadData", "Download This Data")
)
server <- function(input, output) {
# Data to download
C <- list(listA = list(1:3, structure(1:9, .Dim = c(3L, 3L)), 4:9),
listB = list(c("t1", "t2", "t3"), structure(c("p1", "p2"), .Dim = 2:1)))
# write C to file using sink
output$downloadData <- downloadHandler(
filename = function() {"text.txt"},
content = function(file) {
# Here you change to csv (write.csv) or excel (xlsx::write.xlsx)
sink(file); print(C); sink()
}
)
}
shinyApp(ui, server)