I have a table that needs to be displayed when the user clicks on plus button. But this table is reactive and it depends on the conditions. Is there a way to achieve this. I tried below and was not successful.
The table is reactive here. So need to incorporate inside JS
library(shiny)
library(DT)
library(shinyjs)
ui <- {
fluidPage(
fluidRow(
column(6,
dataTableOutput('table1')
),
column(6,
DTOutput('table2')
)
)
)
}
server <- function(input, output, session) {
if(3 > 1){
x = 2
} else {
x = 1
}
table1 <- reactive({
tags$div(style = "background-color:#cbe6ef;padding:5em",tags$table(tags$thead(tags$tr(tags$th("Number of Reviews",x)))))
})
output$table1<-
DT::renderDataTable({
datatable(cbind(Plus = '+',iris), escape = F,
rownames = F,
selection = 'none',
plugins = 'natural',
extensions = c('Buttons','Responsive'),
callback = JS("
table.column(1).nodes().to$().css({cursor: 'pointer'});
var format = function(d) {
return '/table1/';
};
table.on('click', 'td.details-control', function() {
var td = $(this), row = table.row(td.closest('tr'));
if (row.child.isShown()) {
row.child.hide();
td.html('+');
} else {
row.child(format(row.data())).show();
td.html('−');
}
});"
))
})
}
options(shiny.launch.browser = TRUE)
shinyApp(ui, server)