javascript - 从 Shiny DT 中的单选按钮中提取用户输入值到数据框或列表中
问题描述
我正在构建一个带有数据表的闪亮应用程序,该数据表使用一些 javascript 回调,用户可以在其中为每一行进行选择(是/否/也许),然后在应用程序的后期阶段我需要用户输入的形式列表或表格。未预定义确切的行数。理想情况下,我想总结一下每个用户选择了多少“是”/“否”/“可能”以及如何选择哪些行为否。我可以将值打印到 R 终端,但这还不够,需要将值保存为对象。
这是迄今为止我所拥有的代码的简短示例(基于Shiny Datatable 上的 Radio Buttons,带有 data.frame / data.table和Extracting values of selected radio buttons in shiny DT)
library(shiny)
library(DT)
library(shinyWidgets)
my_table <- tibble(
rowid = letters[1:7],
val_1 = round(runif(7, 0, 10), 1),
val_2 = round(rnorm(7), 2),
Yes = "Yes",
No = "No",
Maybe = "Maybe"
) %>%
mutate(
Yes = sprintf('<input type="radio" name="%s" value="%s"/>', rowid , Yes),
No = sprintf('<input type="radio" name="%s" value="%s"/>', rowid , No),
Maybe = sprintf('<input type="radio" name="%s" value="%s"/>', rowid , Maybe)
)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
DT::dataTableOutput("datatable"),
actionBttn(
inputId = "btnProcess",
label = "Process",
style = "float",
size = "sm",
color = "success"
),
actionBttn(
inputId = "btnCancel",
label = "Cancel",
style = "float",
size = "sm",
color = "warning"
)#,
#verbatimTextOutput('sel')
),
server = function(input, output, session) {
dtWithRadioButton <- reactiveValues(dt = my_table)
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 = F
),
server = FALSE
)
# this did not work
#list_results <- eventReactive(input$btnProcess,{
observeEvent(input$btnProcess, {
dt <- dtWithRadioButton$dt # accessing the reactive value
# do some processing based on the radio button selection
list_values <- list()
for (i in unique(my_table$rowid)) {
list_values[[i]] <- paste0(i, ": ", input[[i]])
}
print(list_values)
})
# This did noy work
# output$sel = renderPrint({
# list_results()
# })
#
observeEvent(input$btnCancel, {
removeModal(session)
})
}
)
对于许多奖励积分,拥有一些 .css 代码来更改依赖于单选按钮的行的颜色会很棒(比如红色表示否,绿色表示是,黄色表示可能)。
解决方案
您可以在 a 中进行计算,reactive
然后reactive
在 an中调用它observeEvent
,并使用您选择的任何输出方法将其显示为文本或 DT 表。
library(shiny)
library(DT)
library(shinyWidgets)
my_table <- tibble(
rowid = letters[1:7],
val_1 = round(runif(7, 0, 10), 1),
val_2 = round(rnorm(7), 2),
Yes = "Yes",
No = "No",
Maybe = "Maybe"
) %>%
mutate(
Yes = sprintf('<input type="radio" name="%s" value="%s"/>', rowid , Yes),
No = sprintf('<input type="radio" name="%s" value="%s"/>', rowid , No),
Maybe = sprintf('<input type="radio" name="%s" value="%s"/>', rowid , Maybe)
)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
DT::dataTableOutput("datatable"),
actionBttn(
inputId = "btnProcess",
label = "Process",
style = "float",
size = "sm",
color = "success"
),
actionBttn(
inputId = "btnCancel",
label = "Cancel",
style = "float",
size = "sm",
color = "warning"
),
verbatimTextOutput('sel')
),
server = function(input, output, session) {
dtWithRadioButton <- reactiveValues(dt = my_table)
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 = F
),
server = FALSE
)
list_results <- reactive({
list_values <- list()
for (i in unique(my_table$rowid)) {
list_values[[i]] <- paste0(i, ": ", input[[i]])
}
list_values
})
observeEvent(input$btnProcess, {
output$sel = renderPrint({
list_results()
})
})
observeEvent(input$btnCancel, {
removeModal(session)
})
}
)
推荐阅读
- ios - 在 .htaccess 中重写文件的 MIME 类型不起作用
- asp.net-core - 将自定义模型传递给 Razor 页面中的 Partial
- react-native - 使用 dd/mm/yyyy 的预定义格式反应原生创建文本字段
- vuejs3 - antdvue 组件使用 createVNode 无法解决?
- python - 将除第一行之外的所有内容复制到新的csv文件python中
- javascript - 元素在传递给函数 JavaScript 时变得未定义
- java - Java:Mysql时间戳夏令时错误
- google-cloud-platform - 如何使用 Data Fusion/Cloud Composer 在 GCP 上安排 Dataproc PySpark 作业
- node.js - 从 React 错误(304)获取时无法获取数据
- python - 如何获得数独谜题主轮廓