this is my 1st shiny app trying to incorporate Javascript so I know there are nuances Im probably missing here. I have an app where I’m trying to allow users to add rows and then the table goes to the last page where the row is added with an action button. Both actions work independently within separate observeEvents but when I try placing them in the same observeEvent block, the session$sendCustomMessage doesn’t go through. The row is added but the visual doesn’t go to the last page of the table. Code is below:
ui <- fluidPage(theme = shinytheme(“spacelab”),
navbarPage(
“App”,
tabPanel(“Search”,
mainPanel(
fluidRow(
column(3, selectInput("search_type_filter", label = "Type", choices = c("All", "Derm", "Onc"), selected = "All")),
column(3, selectInput("search_active_filter", label = "Active", choices = c("All", "No", "Yes"), selected = "All"))
),
DT::dataTableOutput("Table")
tags$script(HTML("Shiny.addCustomMessageHandler('LastPage', function(message) {
var target = $('#Table .dataTable');
target.DataTable().page('last').draw(false);
});")),
actionButton("updateButton", "Update", class = "btn btn-primary"),
actionButton("addRowButton", "Add Hashtag"),
actionButton("resetButton", "Reset")
)
)))
server <- function(input, output, session) {
HTData <- reactiveValues(data = getDataFromLake(datalake_path, filename, endp))
# Define reactive filtered data object
fData <- reactive({
data <- HTData$data
if (input$search_type_filter != "All") {
data <- data %>% filter(Type == input$search_type_filter)
}
if (input$search_active_filter != "All") {
data <- data %>% filter(Active == input$search_active_filter)
}
return(data)
})
#Render Table
output$Table <- DT::renderDataTable({
datatable(
fData(),
editable = TRUE
)
}, server = TRUE)
# Define function to add a new row to the data frame
add_row <- function(data) {
new_row <- c("", "", "", "") # Create a vector with empty values for each column
names(new_row) <- names(data) # Set the column names of the new row to match the existing data frame
rbind(data, new_row) # Concatenate the new row to the existing data frame and return the result
}
# Both in same block
observeEvent(input$addRowButton, {
HTData$data <- add_row(HTData$data) # Update the reactiveValues
session$sendCustomMessage('LastPage', 'placeholder')
})
# Execute in separate blocks
# observeEvent(input$addRowButton, {
# HTData$data <- add_row(HTData$data) # Update the reactiveValues
# })
#
# observeEvent(input$addRowButton, {
# HTData$data <- add_row(HTData$data) # Update the reactiveValues
# session$sendCustomMessage('LastPage', 'placeholder')
# })
I’ve tried delaying the session$sendCustomMessage but I’m not getting the proper reaction using the shinyjs::delay() function which is another problem. I think delaying is the answer to get them to work in the same observeEvent block but couldn’t configure a proper solution. Foundation for session$sendCustomMessage comes from this page/solution, but my app is configured differently. R Shiny DT navigate to the table’s last page via an action button