downloadHandler在tabPanel的其他面板执行失败
downloadHandler fails to execute in other panels of tabPanel
我闪亮的应用程序的目的是让用户导入一个 excel 文件,该应用程序将显示 table(在一些数据操作之后)。用户还可以下载 table 并将其保存为他们提供的任何名称。
代码运行良好。但是,当我决定添加另一个 tabPanel 时,下载按钮在第一个面板中工作正常,但只下载第二个面板中的 HTML 而不是导入的 table。如果不清楚,可以复制代码ad试一下运行。第二个面板只下载页面的HTML。
为什么它在第二个面板中下载了 HTML,但相同的代码在第一个面板中运行得很好?
UI:
ui <- fluidPage(
tabsetPanel(
tabPanel("Panel1",
sidebarLayout(
sidebarPanel(fileInput("df1", "Import File", accept = ".xlsx"),
textInput('df1sheet', 'Sheet Name (Case-Sensitive)'),
actionButton("update", "Update")),
mainPanel(tableOutput("table1"),
useShinyjs(),
actionButton("download", "Download Table"),
conditionalPanel(
"false",
downloadButton("downloadtable")
),
useShinyalert())
)),
tabPanel("Panel2",
sidebarLayout(
sidebarPanel(fileInput("df2", "Import File", accept = ".xlsx"),
textInput('df2sheet', 'Sheet Name (Case-Sensitive)'),
actionButton("update2", "Update")),
mainPanel(tableOutput("table2"),
useShinyjs(),
actionButton("download2", "Download Table"),
conditionalPanel(
"false",
downloadButton("downloadtable2")
),
useShinyalert())
))
)
)
服务器:
server <- function(input, output) {
## Code for Panel 1
df1 <- eventReactive(input$update, {
input$df1$datapath
})
df1sheet <- eventReactive(input$update, {
input$df1sheet
})
data1 <- reactive({
req(input$df1)
req(df1sheet())
read_excel(df1(), sheet = df1sheet())
})
output$table1 <- renderTable({data1()})
## User can press on actionButton (id: download), and shinyalert will pop up, asking
## for a filename input. After pressing confirmButton, runjs will click on
## downloadButton and trigger downloadHandler.
observeEvent(input$download, {
shinyalert("Save as:",
type = "input",
size = "xs",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
showConfirmButton = TRUE,
showCancelButton = TRUE,
confirmButtonText = "Save",
confirmButtonCol = "#0075B8",
animation = TRUE)
})
output$downloadtable <- downloadHandler(
filename = function () {paste(input$shinyalert, ".csv", sep = "")},
content = function(file) {write.csv(data1(), file)}
)
observeEvent(input$shinyalert, {
if (input$shinyalert != FALSE) {
shinyjs::runjs("$('#downloadtable')[0].click();")
}
})
## Code for Panel 2
df2 <- eventReactive(input$update2, {
input$df2$datapath
})
df2sheet <- eventReactive(input$update2, {
input$df2sheet
})
data2 <- reactive({
req(input$df2)
req(df2sheet())
read_excel(df2(), sheet = df2sheet())
})
output$table2 <- renderTable({data2()})
## User can press on actionButton (id: download), and shinyalert will pop up, asking
## for a filename input. After pressing confirmButton, runjs will click on
## downloadButton and trigger downloadHandler.
observeEvent(input$download2, {
shinyalert("Save as:",
type = "input",
size = "xs",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
showConfirmButton = TRUE,
showCancelButton = TRUE,
confirmButtonText = "Save",
confirmButtonCol = "#0075B8",
animation = TRUE,
inputId = "shinyalert2")
})
output$downloadtable2 <- downloadHandler(
filename = function () {paste(input$shinyalert2, ".csv", sep = "")},
content = function(file) {write.csv(data2(), file)}
)
observeEvent(input$shinyalert2, {
if (input$shinyalert2 != FALSE) {
shinyjs::runjs("$('#downloadtable2')[0].click();")
}
})
}
您在 conditionalPanel
中的第二个 downloadButton
似乎是问题所在。
替换
conditionalPanel(
"false",
downloadButton("downloadtable2")
),
有了这个
downloadButton("downloadtable2", "Download", style = "visibility: hidden;")
我闪亮的应用程序的目的是让用户导入一个 excel 文件,该应用程序将显示 table(在一些数据操作之后)。用户还可以下载 table 并将其保存为他们提供的任何名称。
代码运行良好。但是,当我决定添加另一个 tabPanel 时,下载按钮在第一个面板中工作正常,但只下载第二个面板中的 HTML 而不是导入的 table。如果不清楚,可以复制代码ad试一下运行。第二个面板只下载页面的HTML。
为什么它在第二个面板中下载了 HTML,但相同的代码在第一个面板中运行得很好?
UI:
ui <- fluidPage(
tabsetPanel(
tabPanel("Panel1",
sidebarLayout(
sidebarPanel(fileInput("df1", "Import File", accept = ".xlsx"),
textInput('df1sheet', 'Sheet Name (Case-Sensitive)'),
actionButton("update", "Update")),
mainPanel(tableOutput("table1"),
useShinyjs(),
actionButton("download", "Download Table"),
conditionalPanel(
"false",
downloadButton("downloadtable")
),
useShinyalert())
)),
tabPanel("Panel2",
sidebarLayout(
sidebarPanel(fileInput("df2", "Import File", accept = ".xlsx"),
textInput('df2sheet', 'Sheet Name (Case-Sensitive)'),
actionButton("update2", "Update")),
mainPanel(tableOutput("table2"),
useShinyjs(),
actionButton("download2", "Download Table"),
conditionalPanel(
"false",
downloadButton("downloadtable2")
),
useShinyalert())
))
)
)
服务器:
server <- function(input, output) {
## Code for Panel 1
df1 <- eventReactive(input$update, {
input$df1$datapath
})
df1sheet <- eventReactive(input$update, {
input$df1sheet
})
data1 <- reactive({
req(input$df1)
req(df1sheet())
read_excel(df1(), sheet = df1sheet())
})
output$table1 <- renderTable({data1()})
## User can press on actionButton (id: download), and shinyalert will pop up, asking
## for a filename input. After pressing confirmButton, runjs will click on
## downloadButton and trigger downloadHandler.
observeEvent(input$download, {
shinyalert("Save as:",
type = "input",
size = "xs",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
showConfirmButton = TRUE,
showCancelButton = TRUE,
confirmButtonText = "Save",
confirmButtonCol = "#0075B8",
animation = TRUE)
})
output$downloadtable <- downloadHandler(
filename = function () {paste(input$shinyalert, ".csv", sep = "")},
content = function(file) {write.csv(data1(), file)}
)
observeEvent(input$shinyalert, {
if (input$shinyalert != FALSE) {
shinyjs::runjs("$('#downloadtable')[0].click();")
}
})
## Code for Panel 2
df2 <- eventReactive(input$update2, {
input$df2$datapath
})
df2sheet <- eventReactive(input$update2, {
input$df2sheet
})
data2 <- reactive({
req(input$df2)
req(df2sheet())
read_excel(df2(), sheet = df2sheet())
})
output$table2 <- renderTable({data2()})
## User can press on actionButton (id: download), and shinyalert will pop up, asking
## for a filename input. After pressing confirmButton, runjs will click on
## downloadButton and trigger downloadHandler.
observeEvent(input$download2, {
shinyalert("Save as:",
type = "input",
size = "xs",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
showConfirmButton = TRUE,
showCancelButton = TRUE,
confirmButtonText = "Save",
confirmButtonCol = "#0075B8",
animation = TRUE,
inputId = "shinyalert2")
})
output$downloadtable2 <- downloadHandler(
filename = function () {paste(input$shinyalert2, ".csv", sep = "")},
content = function(file) {write.csv(data2(), file)}
)
observeEvent(input$shinyalert2, {
if (input$shinyalert2 != FALSE) {
shinyjs::runjs("$('#downloadtable2')[0].click();")
}
})
}
您在 conditionalPanel
中的第二个 downloadButton
似乎是问题所在。
替换
conditionalPanel(
"false",
downloadButton("downloadtable2")
),
有了这个
downloadButton("downloadtable2", "Download", style = "visibility: hidden;")