Last active
July 22, 2023 20:22
-
-
Save ryanbthomas/2cb04edce793c3e148e89370aa129680 to your computer and use it in GitHub Desktop.
using data_index reactive for controlling output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(reactable) | |
ui <- fluidPage( | |
reactableOutput("dummy") | |
) | |
server <- function(input, output, session) { | |
data <- tibble::tibble(x = letters[1:3], y = 1:3, z = c(3, 7, 11)) | |
output$dummy <- renderReactable({ | |
reactable( | |
data, | |
columns = list(z = colDef(show = FALSE)), | |
details = function(index) { | |
tags$div(style = "height: 50px;", actionButton("action", "action")) | |
}, | |
selection = "single", | |
onClick = "select" | |
) | |
}) | |
data_index <- reactive({ | |
getReactableState("dummy", "selected") | |
}) | |
observeEvent(input$action, { | |
message("the value of data_index is ", data_index()) | |
showModal(modalDialog( | |
title = "example", | |
tags$h1(paste0("The secret value is ", data[["z"]][data_index()])), | |
easyClose = FALSE, | |
footer = tagList(actionButton("ok", "ok")) | |
)) | |
}) | |
observeEvent(input$ok, { | |
removeModal() | |
reactable::updateReactable("dummy", selected = NA, expanded = FALSE) | |
}) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment