背景:我在模态对话框中有一个闪亮的DT,其中包含按列显示的单选按钮,我想根据DT中单选按钮的选择进行一些处理
问题:我无法弄清楚如何从DT中提取所选单选按钮的值
下面是相同的可复制示例。
library(shiny)
library(DT)
library(data.table)
library(shinyWidgets)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
actionBttn(inputId = "btnContinue",label = "Continue",style = "material-flat")
),
server = function(input, output, session) {
dtWithRadioButton <- reactiveValues(dt = NULL)
observeEvent(input$btnContinue,{
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, ]
)
}
dt <- data.table(m)
v <- LETTERS[1:12]
dt <- cbind(v,dt)
dtWithRadioButton$dt <- dt # setting reactive value
showModal(modalDialog(
size = "l",easyClose = F,fade = T,
renderDataTable(datatable(dt, selection = "none",escape = FALSE, options = list(dom = 't') , rownames = F)),
footer = tagList(
actionBttn(inputId = "btnCancel",label = "Cancel",style = "float",size="sm",color="warning"),
actionBttn(inputId = "btnProcess",label = "Process",style = "float",size="sm",color="success")
)
))
})
observeEvent(input$btnProcess,{
dt <- dtWithRadioButton$dt # accessing the reactive value
# do some processing based on the radio button selection
})
observeEvent(input$btnCancel,{
removeModal(session)
})
}
)
单击“继续”按钮时,将向用户显示一个包含带DT的闪亮DT的弹出窗口。一旦用户使用单选按钮进行选择。我想点击btnProcess来运行一个进程
答案 0 :(得分:1)
您可以这样做:
library(shiny)
library(DT)
library(shinyWidgets)
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))) {
for(j in seq_len(ncol(m))) {
m[i, j] <- sprintf(
'<input type="radio" name="%s" value="%s" %s/>',
month.abb[i], m[i, j], ifelse(j==1, 'checked="checked"', "")
)
}
}
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
actionBttn(inputId = "btnContinue", label = "Continue",
style = "material-flat")
),
server = function(input, output, session) {
dtWithRadioButton <- reactiveValues(dt = m)
observeEvent(input$btnContinue,{
showModal(modalDialog(
size = "l", easyClose = FALSE, fade = TRUE,
DTOutput("datatable"),
footer = tagList(
actionBttn(inputId = "btnCancel", label = "Cancel",
style = "float", size="sm", color="warning"),
actionBttn(inputId = "btnProcess", label = "Process",
style = "float", size="sm", color="success")
)
))
})
output$datatable <- renderDT(
datatable(dtWithRadioButton$dt, selection = "none", escape = FALSE,
options = list(
dom = 't',
paging = FALSE,
ordering = FALSE
),
callback = JS(
"table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-radiogroup');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());"),
rownames = TRUE),
server = FALSE
)
observeEvent(input$btnProcess,{
dt <- dtWithRadioButton$dt # accessing the reactive value
# do some processing based on the radio button selection
for(month in month.abb){
print(paste0(month, ": ", input[[month]]))
}
})
observeEvent(input$btnCancel,{
removeModal(session)
})
}
)
然后,所选按钮的值在第一行的input$Jan
中,在第二行的input$Feb
中,依此类推。