I'm starting to work with modules using Rhino, and I want to get the input from a button that is inside a table when it is clicked.
This is the module that creates the table and generate HTML buttons for each line item.
# app/logic/fluxogramaProcesso.R
box::use(
glue[glue],
dplyr[filter, select, mutate],
reactable[reactable, colDef],
DBI[dbConnect, dbDisconnect, dbGetQuery],
)
box::use(
app/logic/connectdb,
app/logic/funcTrim
)
#' @export
cadastroProdutos <- function() {
con <- connectdb$create_con()
produtos_query <- glue("SELECT ITEM_CODE FROM TABLE")
produtos_tbl <- dbGetQuery(con, produtos_query) |>
funcTrim$trimDataChar() |>
as.data.frame() |>
mutate(
view_item = glue::glue('<button class="btn" id="vw_item" onclick="Shiny.onInputChange(\'vw_item\', \'{ITEM_CODE}\')"> <i class="fa-solid fa-eye"></i> </button>')
) |>
reactable(
columns = list(
view_item = colDef(html = TRUE)
)
)
dbDisconnect(con)
return(produtos_tbl)
}
**The table generated by this module, goes to a module that generate the server and ui of this table. **
# app/view/table.R
box::use(
reactable[reactableOutput, renderReactable],
shiny[moduleServer, NS, observeEvent],
)
#' @export
ui <- function(id) {
ns <- NS(id)
reactableOutput(ns("table"))
}
#' @export
server <- function(id, data) {
moduleServer(id, function(input, output, session){
output$table <- renderReactable({
data()
})
})
}
And then finnaly go to my main.R
# app/main.R
box::use(
shiny[bootstrapPage, moduleServer, NS, reactive, icon, textInput, renderText, observeEvent, showModal, modalDialog],
shinydashboard[dashboardPage, dashboardHeader, dashboardBody, dashboardSidebar, sidebarMenu, menuItem, tabItems, tabItem],
)
box::use(
app/view/table,
app/view/dataTable,
app/logic/dataProduct,
app/logic/fluxogramaProcesso,
app/logic/cadastroProdutos,
)
#' @export
ui <- function(id) {
ns <- NS(id)
dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Fluxograma de Processos",
tabName = "fluxproc",
icon = icon("sitemap"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "fluxproc",
table$ui(ns("cadastroProd"))
)
)
)
)
}
#' @export
server <- function(id) {
moduleServer(id, function(input, output, session) {
dataProdutos <- reactive(cadastroProdutos$cadastroProdutos())
table$server("cadastroProd", dataProdutos)
observeEvent(input$vw_item, {
showModal(
modalDialog(
h2("IT WORKS !!!!")
)
)
}
)
})
}
I would like to when the button on the table was clicked it generates for example a showModal. I couldn't retrieve the input from the button.
I'm not familiar with Rhino and box so I'm not sure of me.
This is a server module:
Therefore, if I'm not mistaken, the observer will actually listen to
input[[ns("vw_item")]]and then the button click will never be detected because it triggersShiny.onInputChange("vw_item".So I would try:
and:
Edit: possible alternative
A possible alternative is to use the reactable.extras package to make the buttons. Unfortunately, I have not been able to render an icon inside these buttons (but I opened an issue on the reactable.extras Github repo about this problem).