I am trying to add a button into a row in a table.
The table is in tab1. When the button is clicked, a message is shown.
I am following this tutorial, but it doesn't work.
https://antoineguillot.wordpress.com/2017/03/01/three-r-shiny-tricks-to-make-your-shiny-app-shines-33-buttons-to-delete-edit-and-compare-datatable-rows/
library(shiny)
library(ggplot2) # for the diamonds dataset
library(DT)
library(ROracle)
library(DBI)
library(shinyjs)
library(shinydashboard)
library(data.table)
ui <- fluidPage(
title = "Examples of DataTables",
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("tab 1", DT::dataTableOutput("tab1")),
tabPanel("tab 2", DT::dataTableOutput("tab2")),
tabPanel("tab 2", DT::dataTableOutput("tab3"))
)
),
tags$script("$(document).on('click', '#Main_table button', function () {
Shiny.onInputChange('lastClickId',this.id);
Shiny.onInputChange('lastClick', Math.random())
});")
)
server <- function(input, output) {
vals<-reactiveValues()
vals$Data<-data.table(
Brands=paste0("Brand",1:10),
Forecasted_Growth=sample(1:20,10),
Last_Year_Purchase=round(rnorm(10,1000,1000)^2),
Contact=paste0("Brand",1:10,"@email.com")
)
output$tab1 <- DT::renderDataTable({
DT=vals$Data
DT[["Actions"]]<-
paste0('
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-secondary delete" id=delete_',1:nrow(vals$Data),'>Delete</button>
</div>
')
datatable(DT, escape=F)})
observeEvent(input$lastClick,
{
if (input$lastClickId%like%"delete")
{
print("DELETE CLICKED")
}
}
)
}
shinyApp(ui, server)