访问数据表中的单选按钮值(闪亮)
Accessing Radio Button Values in DataTable (Shiny)
当单选按钮输入值嵌入到闪亮的 DataTable 中时,我无法访问它们。我的代码基于此示例:Radio Buttons in DT
它访问第一遍的值并将它们显示在下方,如以下屏幕截图所示:
First pass screenshot
一旦我更改了反应性重新呈现 table 的输入,单选按钮中的值将不再显示,而是停留在第一个传递值上。这可以在下面的屏幕截图中看到。
Second pass screenshot
第一次通过后,输入不再更新以反映单选按钮选择。原始输入似乎冻结了,我找不到访问当前按钮值的方法。
提前感谢您的帮助!
上面例子的代码如下:
library(shiny)
library(DT)
library(data.table)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
selectInput('questions','Pick',c(1,2)),
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
),
server = function(input, output, session) {
myData = reactiveValues(m = NULL)
temp = month.abb
observeEvent(input$questions,{
m = data.table(
month1 = temp[1:5],
A = '1',
B = '2',
C = '3',
QWE = runif(5)
)
m[, A := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, A]
)]
m[, B := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, B]
)]
m[, C := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, C]
)]
myData$m = m
})
output$foo = DT::renderDataTable({
m = myData$m
print(m)
}
, escape = FALSE, selection = 'none', server = FALSE, rownames=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());")
)
output$sel = renderPrint({
str(sapply(month.abb, function(i) input[[i]]))
})
}
)
那是因为您正在 observeEvent()
中重置 m
。如果您在观察者之外定义它,它会按预期工作。试试这个
server = function(input, output, session) {
myData <- reactiveValues(m = NULL)
temp <- month.abb
m <- data.table(
month1 = temp[1:5],
A = '1',
B = '2',
C = '3',
QWE = runif(5)
)
observeEvent(input$questions,{
m[, A := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, A]
)]
m[, B := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, B]
)]
m[, C := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, C]
)]
myData$m = m
})
output$foo = DT::renderDataTable({
m = myData$m
print(m)
}
, escape = FALSE, selection = 'none', server = FALSE, rownames=FALSE, # editable=TRUE,
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());")
)
output$sel = renderPrint({
str(sapply(month.abb, function(i) input[[i]]))
})
}
当单选按钮输入值嵌入到闪亮的 DataTable 中时,我无法访问它们。我的代码基于此示例:Radio Buttons in DT
它访问第一遍的值并将它们显示在下方,如以下屏幕截图所示:
First pass screenshot
一旦我更改了反应性重新呈现 table 的输入,单选按钮中的值将不再显示,而是停留在第一个传递值上。这可以在下面的屏幕截图中看到。 Second pass screenshot
第一次通过后,输入不再更新以反映单选按钮选择。原始输入似乎冻结了,我找不到访问当前按钮值的方法。
提前感谢您的帮助!
上面例子的代码如下:
library(shiny)
library(DT)
library(data.table)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
selectInput('questions','Pick',c(1,2)),
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
),
server = function(input, output, session) {
myData = reactiveValues(m = NULL)
temp = month.abb
observeEvent(input$questions,{
m = data.table(
month1 = temp[1:5],
A = '1',
B = '2',
C = '3',
QWE = runif(5)
)
m[, A := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, A]
)]
m[, B := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, B]
)]
m[, C := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, C]
)]
myData$m = m
})
output$foo = DT::renderDataTable({
m = myData$m
print(m)
}
, escape = FALSE, selection = 'none', server = FALSE, rownames=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());")
)
output$sel = renderPrint({
str(sapply(month.abb, function(i) input[[i]]))
})
}
)
那是因为您正在 observeEvent()
中重置 m
。如果您在观察者之外定义它,它会按预期工作。试试这个
server = function(input, output, session) {
myData <- reactiveValues(m = NULL)
temp <- month.abb
m <- data.table(
month1 = temp[1:5],
A = '1',
B = '2',
C = '3',
QWE = runif(5)
)
observeEvent(input$questions,{
m[, A := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, A]
)]
m[, B := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, B]
)]
m[, C := sprintf(
'<input type="radio" name="%s" value="%s"/>',
month1, m[, C]
)]
myData$m = m
})
output$foo = DT::renderDataTable({
m = myData$m
print(m)
}
, escape = FALSE, selection = 'none', server = FALSE, rownames=FALSE, # editable=TRUE,
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());")
)
output$sel = renderPrint({
str(sapply(month.abb, function(i) input[[i]]))
})
}