library(shiny)
library(htmlwidgets)
shinyServer(function(input,output,session) {
values <- reactiveValues(deleted = character(0))
dat <- reactive({
del <- values$deleted
isolate({
dat <- data.frame(Island = names(islands), Area = unname(islands))
dat$Island <- as.character(dat$Island)
dat <- dat[!(dat$Island %in% del),]
return(dat)
})
})
output$islandList <- DT::renderDataTable({
island <- dat()
validate(
need(island,""),
need(nrow(island)>0,"No islands found.")
)
island$Delete <- paste0('<div><div class="checkbox">
<label><input id="selone',island$Island,'" type="checkbox"/><span></span></label></div></div>')
DT::datatable(island,options = list(paging=TRUE,searching=TRUE,
preDrawCallback = JS('function() {
Shiny.unbindAll(this.api().table().node());}'),
drawCallback = JS('function() {
Shiny.bindAll(this.api().table().node());} '))
, escape = c(1,2), rownames = FALSE)
})
output$preview1 <- renderText({
island <- dat()
return(paste0("Total Area: ",sum(island$Area)))
})
observe({
asdf <- input$delete
if(is.null(asdf) || asdf == 0)
{
return(NULL)
}
isolate({
island <- dat()
ids <- island$Island
toSel <- vapply(ids,function(i){temp <- input[[paste0('selone',i)]]; ifelse(is.null(temp),0,temp)},numeric(1))
values$deleted <- c(values$deleted,ids[toSel==1])
})
})
output$deleteUI <- renderUI({
island <- dat()
if(is.null(island))
{
return(NULL)
}
ids <- island$Island
toSel <- vapply(ids,function(i){temp <- input[[paste0('selone',i)]]; ifelse(is.null(temp),0,temp)},numeric(1))
if(sum(toSel))
{
return(actionButton("delete","Delete"))
}
})
}
)