I am trying to implement shiny popup as described in this post on shinyBS popup.
My app is wrapped in an observeEvent()
based on the Enter key and isolate()
to prevent the table from changing as we type the name of cars before pressing Enter key.
The issue is that first time works well and I able to view the popup window, but consecutive searches with different car names and pressing Enter, the pop ups do not work. In fact, after a few attempts, the app greys out.
How to implement these 3 (pop up modal, observe event based on Enter key and isolate to prevent reactivity) in tandem seamlessly?
My code is as below
library(DT)
library(shiny)
library(shinyBS)
library(shinyjs)
library(shinydashboard)
library(tidyverse)
shinyInput <- function(FUN, len, id, ...) {inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))}
inputs
}
mtcarsDf <- mtcars %>%
mutate(car_name = row.names(mtcars)) %>%
select(car_name, cyl, mpg, gear)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Tab1", tabName = "Tab1", icon = icon("dashboard"))
)),
dashboardBody(
tags$script('
$(document).on("keyup", function(e) {
if(e.keyCode == 13){
Shiny.onInputChange("keyPressed", Math.random());
}
});
'),
tabItems(
tabItem(tabName = "Tab1",
div("try typing mazda, ferrari, volvo, camaro,
lotus, maserati, porsche, fiat, dodge, toyota, honda, merc"),
textInput("name", "Car Name"),
uiOutput("popup1"),
DT::dataTableOutput('table1'))
)))
server <- function(input, output, session) {
observeEvent(input[["keyPressed"]], {
data <- reactive({
if (input$name != "") {
reactiveDf <- reactive({
if (input$name != "") {
mtcarsDf <- mtcarsDf %>%
filter(grepl(input$name, car_name, ignore.case = TRUE))
}
})
testdata <- reactiveDf()
as.data.frame(cbind(View = shinyInput(actionButton, nrow(testdata),
'button_', label = "View",
onclick = 'Shiny.onInputChange(\"select_button\", this.id)' ),
testdata))
}
})
isolate(data <- data())#### this is required to avoid the table changing as we type the name
output$table1 <- DT::renderDataTable(data,
selection = 'single',
options = list(searching = FALSE,pageLength = 10),
server = FALSE, escape = FALSE,rownames= FALSE)
SelectedRow <- eventReactive(input$select_button,{
as.numeric(strsplit(input$select_button, "_")[[1]][2])
})
observeEvent(input$select_button, {
toggleModal(session, "modal1", "open")
})
DataRow <- eventReactive(input$select_button,{
data[SelectedRow(),2:ncol(data)]
})
output$popup1 <- renderUI({
bsModal("modal1", paste0("Data for Row Number: ",SelectedRow()), "", size = "large",
column(12,
DT::renderDataTable(DataRow())
))
})
})
}
shinyApp(ui, server)