【发布时间】:2021-12-29 23:37:19
【问题描述】:
请参阅最底部的最终工作 MWE 代码,其中包含 gss 提出的建议!
在运行以下 MWE 代码时,如果用户单击侧边栏面板顶部的“浏览”按钮并访问 csv 文件,则该文件中的数据将被上传并以表格形式显示在主面板中。但是,我还希望将 csv 数据作为矩阵或向量反映在该侧边栏面板中呈现的矩阵中。如底部的图像所示。关于如何做到这一点的任何想法?
在下面的 MWE 代码中,您可以看到我的注释掉的 observeEvent() 由“浏览”触发,我尝试这样做,包括使用 shinyMatrix 包的 updateMatrixInput() 函数。它不起作用。
最终我计划取消表格渲染,因为两次查看相同的数据是没有意义的。但现在为了测试,我正在尝试并行运行它们。
library(shiny)
library(shinyMatrix)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Optionally choose input file (csv)", accept = ".csv"),
matrixInput("matrix1",
value = matrix(c(5), ncol = 1, dimnames = list("Matrix 1 input",NULL)),
cols = list(names = FALSE),
class = "numeric"),
downloadButton("download")
),
mainPanel(
tableOutput("contents"),
)
)
)
server <- function(input, output, session) {
output$contents <- renderTable({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
data <- read.csv(file$datapath, header = TRUE)
data
})
# observeEvent(input$file1,{
# tmpMat <- matrix(c(data), ncol=1)
# updateMatrixInput(session, inputId = "matrix1", value = tmpMat)
# })
output$download <- downloadHandler(
filename = function() {
paste("Inputs","csv",sep=".")
},
content = function(file) {
write.csv(input$matrix1[1,1], file,row.names=FALSE)
}
)
}
shinyApp(ui, server)
基于 gss 解决方案的最终工作 MWE 代码:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Optionally choose input file (csv)", accept = ".csv"),
matrixInput("matrix1",
value = matrix(c(5),
ncol = 1,
dimnames = list("Matrix 1 input",NULL)
),
cols = list(names = FALSE),
class = "numeric"
),
downloadButton("download")
),
mainPanel(
tableOutput("contents"),
)
)
)
server <- function(input, output, session) {
input_file <- reactive({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
read.csv(file$datapath, header = TRUE)
})
output$contents <- renderTable({
input_file()
})
observeEvent(input$file1,{
updateMatrixInput(session,
inputId = "matrix1",
value = matrix(as.matrix(input_file()),
ncol=1,
dimnames = list("Matrix 1 input",NULL)
)
)
})
output$download <- downloadHandler(
filename = function() {
paste("Inputs","csv",sep=".")
},
content = function(file) {
write.csv(input$matrix1[1,1], file,row.names=FALSE)
}
)
}
shinyApp(ui, server)
【问题讨论】:
标签: r shiny upload shiny-reactivity