R 下载按钮可通过任何输入更改自动清除
我有两个R 下载按钮可通过任何输入更改自动清除,r,shiny,shiny-reactivity,R,Shiny,Shiny Reactivity,我有两个输入选择和一个操作按钮,用于生成绘图和下载数据。我想在输入选择发生变化时清除输出内容(绘图和下载按钮)。下面的代码只会清除绘图,而不会清除下载按钮。不确定下载处理程序下的反应值是否正确 library(shiny) library(ggplot2) library(openxlsx) ui = fluidPage( textInput("textT", label = "Title", value = ""), textInput("textX", label = "X-Ax
输入选择
和一个操作按钮
,用于生成绘图和下载数据。我想在输入选择发生变化时清除输出内容(绘图和下载按钮)。下面的代码只会清除绘图,而不会清除下载按钮。不确定下载处理程序
下的反应值是否正确
library(shiny)
library(ggplot2)
library(openxlsx)
ui = fluidPage(
textInput("textT", label = "Title", value = ""),
textInput("textX", label = "X-Axis Label", value = ""),
actionButton("Btn", "Run", icon=icon("play-circle")),
plotOutput('plot1'),
conditionalPanel(condition = "input.Btn>0", downloadButton("dwload", "Download"))
)
server = function(input, output, session) {
v <- reactiveValues(clearAll = TRUE)
observeEvent(c(input$textT, input$textX), {
v$clearAll <- TRUE
}, priority = 10)
observeEvent(input$Btn, {
output$plot1 = renderPlot({
if (v$clearAll)
return()
else
ggplot(mtcars, aes(x= gear, y= carb)) + geom_line() +ggtitle(input$textT) + xlab(input$textX)
})
output$dwload <- downloadHandler(
filename = function() {
paste0("Checks-", gsub(" ", "_", gsub(":", ".", Sys.time())), ".xlsx")
},
content = function(file) {
if (v$clearAll)
return()
else
quick_xlsx(mtcars, file=file)
}
)
v$clearAll <- FALSE
}, priority = 10)
}
shinyApp(ui, server)
库(闪亮)
图书馆(GG2)
库(openxlsx)
ui=fluidPage(
textInput(“textT”,label=“Title”,value=”“),
textInput(“textX”,label=“X轴标签”,value=”“),
操作按钮(“Btn”,“运行”,图标=图标(“播放圈”),
plotOutput('plot1'),
conditionalPanel(condition=“input.Btn>0”,下载按钮(“dwload”,“Download”))
)
服务器=函数(输入、输出、会话){
v这里有一个使用renderUI
和req
的解决方案:
library(shiny)
library(ggplot2)
library(openxlsx)
ui <- fluidPage(
textInput("textT", label = "Title", value = ""),
textInput("textX", label = "X-Axis Label", value = ""),
actionButton("Btn", "Run", icon=icon("play-circle")),
uiOutput("widgets")
)
server <- function(input, output, session) {
hideAll <- reactiveVal(TRUE)
observeEvent(list(input$textT, input$textX), {
hideAll(TRUE)
})
observeEvent(input$Btn, {
req(input$textT)
req(input$textX)
hideAll(FALSE)
})
output$plot1 <- renderPlot({
ggplot(mtcars, aes(x= gear, y= carb)) + geom_line() +
ggtitle(input$textT) + xlab(input$textX)
})
output$dwload <- downloadHandler(
filename = function() {
paste0("Checks-", gsub(" ", "_", gsub(":", ".", Sys.time())), ".xlsx")
},
content = function(file) {
quick_xlsx(mtcars, file=file)
}
)
output$widgets <- renderUI({
req(!hideAll())
tagList(
plotOutput('plot1'),
downloadButton("dwload", "Download")
)
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(GG2)
库(openxlsx)
ui你能解释一下上下文吗?那会有帮助。你为什么要删除绘图和按钮?它们是应该被删除,还是可以回来?谢谢Stephane提出的问题。目的是清除所有输出(包括下载按钮)因此,用户在更改输入时不会看到任何输出——这是一个具有多个输入的更复杂的闪亮应用程序的简单例子——但是,所有输入都是通过observeEvent中的“运行”按钮控制的。因此,只要输入发生更改,输出就会被清除,只要用户单击“运行”按钮,输出就会显示出来到目前为止,我们使用的是“绘图”,而不是“下载按钮”。谢谢Stephane。这解决了问题。但是,点击Run
将显示输出,而无需输入。然后我们仍然可以使用req()
进行input$textT
和input$textT
。请编辑代码并添加req(input$textT)
和req(输入$textX)
在observeEvent
中,对于输入$Btn
,我将接受答案。再次感谢您