有什么方法可以在DT Shiny中定义多个JavaScript?我在表格的每一行都有下拉菜单。我想默认将某些下拉菜单选择为“预防”吗?我想在DT中定义以下JS代码。此代码将第5个下拉菜单(从顶部开始)设置为“预防”。但这不起作用。
function setSelectedIndex(s, valsearch)
for (i = 0; i< s.options.length; i++)
{
if (s.options[i].value == valsearch)
{
s.options[i].selected = true;
break;
}
}
return;}
setSelectedIndex(document.getElementById('selecter_5'),'Prevention');
下面的代码是我正在使用的主要Shiny代码-
library(shiny)
library(DT)
mymtcars = mtcars
mymtcars$id = 1:nrow(mtcars)
runApp(list(
ui = basicPage(
h2('The mtcars data'),
DT::dataTableOutput('mytable'),
h2("Selected"),
tableOutput("checked")
)
,
server = function(input, output) {
# datatable with checkbox
output$mytable = DT::renderDataTable({
#Display table with select
DT::datatable(cbind(Pick=paste0('
<select id="selecter_', mymtcars$id, '">
<option disabled selected>Choose Status</option>
<option>Non-Fraud</option>
<option>Fraud</option>
<option>Prevention</option>
</select>',""), mymtcars),
extensions = c('Scroller'),
filter = 'top',
selection=list(mode = 'multiple'),
options = list(
scrollX = TRUE,
scroller = TRUE,
scrollY = "400px",
orderClasses = TRUE,
pageLength = 50,
fixedHeader = TRUE,
dom = 'Bfrtip',
drawCallback= JS(
'function(settings) {
Shiny.bindAll(this.api().table().node());}'
)), escape=F) }, server = T)
# helper function for reading checkbox
shinyValue = function(id, len) {
unlist(lapply(seq_len(len), function(i) {
value = input[[paste0(id, i)]]
if (is.null(value)) NA else value
}))
}
# output read checkboxes
output$checked <- renderTable({
data.frame(selected=shinyValue("selecter_",nrow(mtcars)))
})
}))