使用shinydashboard中的操作按钮重置tableoutput

时间:2017-09-12 09:31:24

标签: r shiny

我有一个shinydashboard应用程序,应用程序获得一个过滤器框和一个tabset,显示一个数据类型取决于过滤器。 我有一个重置按钮,重置过滤器whith shinyjs :: reset功能,我想重置表集并显示完整的表或什么也没有。 我也想为valuboxes做这件事。

我的应用是这样的:

对于服务器接口,我有一个基本的:output$tableprint_A <- DT::renderDataRable ({}) ui:

body <- dashboardBody(
  tabItems(
    #### First tab item #####
    tabItem(tabName = "fpc",
            fluidRow(
              infoBoxOutput("kpm_inf", width = 6),
              infoBoxOutput(outputId = "fpc_inf", width = 6)
            ),
            fluidRow(
              box(title = "Variables filter",
                  shinyjs::useShinyjs(),
                  id = "side_panel",
                  br(),
                  background = "light-blue",
                  solidHeader = TRUE,
                  width = 2,
                  selectInput("aaa", "aaa", multiple = T, choices = c("All", as.character(unique(fpc$aaa))))
                  br(),
                  br(),
                  p(class = "text-center", div(style = "display:inline-block", actionButton("go_button", "Search", 
                                                                                            icon = icon("arrow-circle-o-right"))),
                    div(style = "display:inline-block", actionButton("reset_button", "Reset", 
                                                                     icon = icon("repeat")))),
                  p(class = 'text-center', downloadButton('dl_fpc', 'Download Data'))),
              tabBox(
                title = tagList(),
                id = "tabset1",
                width = 10,
                tabPanel(
                  "A \u2030 ",
                  DT::dataTableOutput("tableprint_A"),
                  bsModal(id = 'startupModal', title = 'Update message', trigger = '',
                          size = 'large',
                          tags$p(tags$h2("Last update of A : 01/09/2017",
                                         br(), br(),
                                         "Last update of B : 01/09/2017",
                                         br(), br(),
                                         "Last update of C : 01/09/2017",
                                         style = "color:green", align = "center")))
                ),
                tabPanel(
                  "B % Table",
                  DT::dataTableOutput("tableprint_B")),
                type = "pills"
              )
            ),
            fluidRow(
              # Dynamic valueBoxes
              valueBoxOutput("info_gen", width = 6)
            )

我试过了:

  observeEvent(input$reset_button, {
    output$tableprint_A <- NULL
  })

编辑:

我想要这样的东西,但当我操作搜索按钮时,我希望它再次出现:

 shinyjs::onclick("reset_button",
                   shinyjs::toggle(id = "tableprint_A", anim = TRUE))

1 个答案:

答案 0 :(得分:1)

你应该尝试一下:

output$tableprint_A <- renderDataTable({
  if(input$reset_button == 1) {
    NULL
  }else{
    datatable(...)
  }
})

如果单击该按钮,则不显示任何内容,否则显示datatable

<强> [编辑]

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(selectInput("select", "select", choices = unique(iris$Species), multiple = T),
                 actionButton("go_button", "Search", 
                              icon = icon("arrow-circle-o-right")),
                 actionButton("reset_button", "Reset", 
                              icon = icon("repeat")),
                              DT::dataTableOutput('tbl')),
  server = function(input, output) {

    values <- reactiveValues(matrix = NULL)

    observe({
      if (input$go_button == 0)
        return()
      values$matrix <- iris[iris$Species %in% input$select, ]
    })
    observe({
      if (input$reset_button == 0)
        return()
      values$matrix <- NULL
    })

    output$tbl = DT::renderDataTable({
      datatable(values$matrix, options = list(lengthChange = FALSE))}
    )
  }
)