r - 是否可以使用 actionButton 清除 ShinyApp 中显示的输出
问题描述
我正在mtcars 数据上构建一个 shinyApp 。我有 2 个动作按钮(Go & Clear)。Go 按钮用于 在mainPanel 上显示输出,而Clear 按钮用于清除该输出。由于某些不可预见的原因,我的清除按钮无法正常工作。有人可以看看我的代码。我将不胜感激。
library(shiny)
library(DT)
library(dplyr)
library(shinythemes)
library(htmlwidgets)
library(shinyWidgets)
library(shinydashboard)
data_table<-mtcars
#ui
ui = fluidPage(
sidebarLayout(
sidebarPanel (
uiOutput("cyl_selector"),
uiOutput("disp_selector"),
actionButton(inputId = "go", label = "Go"),
actionButton(inputId = "reset", label = "Clear")),
mainPanel(
DT::dataTableOutput('mytable') )))
#server
server = function(input, output, session) {
output$cyl_selector <- renderUI({
selectInput(inputId = "cyl",
label = "cyl:", multiple = TRUE,
choices = c( unique(as.character(data_table$cyl))),
selected = c('4')) })
output$disp_selector <- renderUI({
available <- data_table[c(data_table$cyl %in% input$cyl ), "disp"]
selectInput(
inputId = "disp",
label = "disp:",
multiple = TRUE,
choices = c('All',as.character(unique(available))),
selected = 'All') })
thedata <- eventReactive(input$go,{
data_table<-data_table[data_table$cyl %in% input$cyl,]
if(input$disp != 'All'){
data_table<-data_table[data_table$disp %in% input$disp,]
}
data_table
})
# thedata <- eventReactive(input$reset,{
# data_table<-NULL
# })
output$mytable = DT::renderDataTable({
DT::datatable( filter = "top", rownames = FALSE, escape = FALSE,
options = list(pageLength = 50, autowidth=FALSE,
dom = 'Brtip' ),
{
thedata() # Call reactive thedata()
})
})}
shinyApp(ui = ui, server = server)
解决方案
insertUI()
并且removeUI()
是您可能正在寻找的。
删除元素更容易removeUI()
:
observeEvent(input$reset, {
removeUI("#mytable")
})
为避免您不永久删除它,您可以使用insertUI()
:
observeEvent(input$go, {
insertUI("#placeholder", "afterEnd", ui = DT::dataTableOutput('mytable'))
})
为了正确放置元素,您可以在以下位置使用占位符mainPanel()
:
mainPanel(
tags$div(id = "placeholder")
)
thedata()
然后您可以从输入按钮中删除对的依赖,因为您使用的是insertUI()
现在。(你应该切换到insertUI()
,否则你不能在没有它的情况下重新插入表格,......)
thedata <- reactive({
...
})
完整的例子是:
library(shiny)
library(DT)
library(dplyr)
library(shinythemes)
library(htmlwidgets)
library(shinyWidgets)
library(shinydashboard)
data_table<-mtcars
#ui
ui = fluidPage(
sidebarLayout(
sidebarPanel (
uiOutput("cyl_selector"),
uiOutput("disp_selector"),
actionButton(inputId = "go", label = "Go"),
actionButton(inputId = "reset", label = "Clear")),
mainPanel(
tags$div(id = "placeholder")
)
)
)
#server
server = function(input, output, session) {
output$cyl_selector <- renderUI({
selectInput(inputId = "cyl",
label = "cyl:", multiple = TRUE,
choices = c( unique(as.character(data_table$cyl))),
selected = c('4')) })
output$disp_selector <- renderUI({
available <- data_table[c(data_table$cyl %in% input$cyl ), "disp"]
selectInput(
inputId = "disp",
label = "disp:",
multiple = TRUE,
choices = c('All',as.character(unique(available))),
selected = 'All') })
thedata <- reactive({
input$go
isolate({
data_table<-data_table[data_table$cyl %in% input$cyl,]
if(input$disp != 'All'){
data_table<-data_table[data_table$disp %in% input$disp,]
}
return(data_table)
})
})
observeEvent(input$reset, {
removeUI("#mytable")
})
observeEvent(input$go, {
insertUI("#placeholder", "afterEnd", ui = DT::dataTableOutput('mytable'))
})
output$mytable = DT::renderDataTable({
DT::datatable( filter = "top", rownames = FALSE, escape = FALSE,
options = list(pageLength = 50, autowidth=FALSE,
dom = 'Brtip' ),
{
thedata() # Call reactive thedata()
})
})}
shinyApp(ui = ui, server = server)
)
推荐阅读
- amazon-web-services - SQS 与服务的通信
- android - FastAdapter 项目内视图的单击侦听器
- mysql - 发现 MySQL 触发器创建错误:1064
- flutter - Flutter Radio Button在单击时不改变值/也不改变活动颜色
- java - 面板不显示文字
- selenium - Xpath 不工作,我正在使用 appium 来定位 xpath
- php - 如何解决:已经定义的“路由名称未定义”
- javascript - 如何将单个 [x, y] 坐标转换为 [xstart, xend,ystart,yend] 的范围?
- c - 如何使用 prolog 调用酷图库函数?
- mysql - 当所有表都没有主键时使用“SET”更新多列