Can input type checkbox be assigned a value TRUE/FALSE via javascript in a shiny app? For example, I have a reactive data table:
data:vals<-reactiveValues()
vals$Data<-data.table(
Brands=paste0("Brand",1:200000),
Forecasted_Growth=sample(1:200000,200000),
Last_Year_Purchase=round(rnorm(200000,1000,1000)^2),
Contact=paste0("Brand",1:200000,"@email.com"),
YN=sample(rep(c("TRUE","FALSE"),each=100000),200000,replace=FALSE)
)
I would like to assign the value of each YN to a checkbox when the app loads. Later I can manipulate the checked rows.
I've tried to use HTML like this to assign the value of each YN to each checkbox but it doesn't work:
DT[["Select"]]<-paste0('<input type="checkbox" name="row_selected',1:nrow(vals$Data),'" value=',vals$Data$YN,'><br>')
Then I will use this JS to get the values later:
tags$script(HTML('$(document).on("click", "input", function () {
var checkboxes = document.getElementsByName("row_selected");
var checkboxesChecked = [];
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i].value);
}
}
Shiny.onInputChange("checked_rows",checkboxesChecked);
})')),
Is it possible to assign values to checkboxes in that way? I'm trying to modify an example from here:
The reason why your approach does not work is that you assign the boolean represented by
YNto thevalueattribute. Instead (forHTML 5), you have to usecheckedifTRUEand omitcheckedotherwise.So an example
dfcould look like this:And when the app is started, it would look so:
Minimal example: