You can render arbitrary HTML content in the table when you choose not to escape the columns that contain HTML content (escape = FALSE). Here we show an example of embedding radio buttons in table cells. First, we prepare the raw HTML content (a radio button is <input type="radio" /> in HTML):
m = matrix(
as.character(1:5), nrow = 12, ncol = 5, byrow = TRUE,
dimnames = list(month.abb, LETTERS[1:5])
)
for (i in seq_len(nrow(m))) {
m[i, ] = sprintf(
'<input type="radio" name="%s" value="%s"/>',
month.abb[i], m[i, ]
)
}
m[1:3, 1:2]
## A
## Jan "<input type=\"radio\" name=\"Jan\" value=\"1\"/>"
## Feb "<input type=\"radio\" name=\"Feb\" value=\"1\"/>"
## Mar "<input type=\"radio\" name=\"Mar\" value=\"1\"/>"
## B
## Jan "<input type=\"radio\" name=\"Jan\" value=\"2\"/>"
## Feb "<input type=\"radio\" name=\"Feb\" value=\"2\"/>"
## Mar "<input type=\"radio\" name=\"Mar\" value=\"2\"/>"
After we have got the character matrix of HTML code, we can pass it to datatable():
library(DT)
datatable(m, escape = FALSE, options = list(dom = 't', paging = FALSE, ordering = FALSE))
Above is just a static table, which may not be useful since you do not have access to the options that users have selected. We can use this table in Shiny, and there are a few more steps to go. See this live app for the full source code with technical details.