0

I have the following code:

library(shiny)
library(shinydashboard)
library(rhandsontable)

header <- dashboardHeader(title = "Sample", titleWidth = 375)

sidebar <- dashboardSidebar(width = 270,
                            sidebarMenu(id="mymenu",
                                        menuItem(text = "Home", tabName = "tabCars", icon = icon("home", class="home"))
                            ))

body <- dashboardBody (
  tabItems(
    tabItem(tabName = "tabCars",

        fluidRow(

          column(width = 2,
                 selectInput(
                   inputId = "selected_CarCylinders",
                   label = "Car Cylinders",
                   choices = mtcars$cyl,
                   selectize = TRUE,
                   width = "250px",
                   multiple = FALSE
                 )),

          column(width = 2, style = "margin-top: 25px",
                 actionButton("deleteBtn", "Delete Selected Cylinders")),

          column(width = 1, style = "margin-top: 25px",
                 actionButton("refreshBtn", "Refresh Filter/Chart")),

          rHandsontableOutput("carDT")

        )
    )
  )
)

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output, session) {

  output$carDT <- renderRHandsontable({
    df <- mtcars
    rhandsontable(df, stretchH = "all")
  })

  observeEvent(input$deleteBtn, {
    # need help here
  })

  observeEvent(input$refreshBtn, {
    # need help here    
  })

}

shinyApp(ui, server)

I need help writing what would go into the input$deleteBtn and input$refreshBtn sections of the server side. If you run the code as is, the idea is to select the number of cylinders from mtcars, then click the Delete button to remove all those entries from the table and filter; however, the filter and table would only update after clicking the refresh button.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
lumiukko
  • 249
  • 3
  • 13

1 Answers1

0

While permanently delete screams a SQLite database to me, you could achieve this by using a reactiveVal to store the dataframe and call req to only refresh the table when you click the refreshBtn (in this case, you also have to click it to display the table at the start of the app).

server <- function(input, output, session) {
  # Create a `reactiveVal` and set a value to it
  df <- reactiveVal()

  df(mtcars)

  output$carDT <- renderRHandsontable({
    req(input$refreshBtn)

    rhandsontable(df(), stretchH = "all")
  })

  observeEvent(input$deleteBtn, {
    data <- dplyr::filter(df(), cyl != input$selected_CarCylinders)

    # Update `selectInput` to filter out the choices too (for good measure)
    updateSelectInput(session, "selected_CarCylinders", choices = data$cyl)

    # Update the `reactiveVal` value
    df(data)
  })
}
Gabriel M. Silva
  • 642
  • 4
  • 10
  • Yes, that's exactly what I'm looking for. To add complexity, what steps would need to be taken to add a confirmation (modal) to the deleteBtn letting the user confirm or cancel the delete request? – lumiukko Nov 25 '19 at 16:45
  • You can use a `modalDialog` with a Ok/Cancel buttons and combine it with another `observeEvent`. [shinyWidgets](https://github.com/dreamRs/shinyWidgets) package offers a good looking and ready-to-go dialog with `confirmSweetAlert`. Take a look at [this question](https://stackoverflow.com/questions/31107645/shiny-how-to-create-a-confirm-dialog-box) for a variety of alternatives. – Gabriel M. Silva Nov 25 '19 at 17:43