Skip to content

Commit

Permalink
a few convenience functions to add check boxes to a table, and obtain…
Browse files Browse the repository at this point in the history
… a logical vector indicating which rows are selected in shiny

close #3, close #19, and close #20
  • Loading branch information
yihui committed Mar 6, 2015
1 parent 93985df commit eda2335
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
export("%>%")
export(JS)
export(appendCheckboxes)
export(checkboxRows)
export(copySWF)
export(dataTableOutput)
export(datatable)
export(formatCurrency)
export(formatPercentage)
export(formatRound)
export(renderDataTable)
export(selectedRows)
export(tableFooter)
export(tableHeader)
importFrom(htmltools,HTML)
Expand Down
18 changes: 18 additions & 0 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ renderDataTable = function(expr, env = parent.frame(), quoted = FALSE, ...) {
if (!quoted) expr = substitute(expr)
htmlwidgets::shinyRenderWidget(expr, dataTableOutput, env, quoted = TRUE)
}

#' @export
appendCheckboxes = function(data, id, after = TRUE) {
check = checkboxRows(data, id)
if (after) cbind(data, ' ' = check) else cbind(' ' = check, data)
}

#' @export
checkboxRows = function(data, id) {
sprintf('<input id="%s%s" type="checkbox"/>', id, seq_len(nrow(data)))
}

#' @export
selectedRows = function(input, data, id) {
sapply(seq_len(nrow(data)), function(i) {
isTRUE(input[[sprintf('%s%s', id, i)]])
})
}

0 comments on commit eda2335

Please sign in to comment.