【发布时间】:2022-01-05 13:08:54
【问题描述】:
我正在尝试根据我们在每一行从用户收到的输入更新数据表中的逐行过滤器,以便只能选择后续输入中的相关值。
我尝试使用以下代码复制我的场景,如果用户选择“setosa”作为“spieces_selector”,则“New_Data_selector”中应该只出现“1-50”值。同样,如果用户在第二行选择“versicolor”,那么对于第二行,“New_Data_selector”的值应该是“51-100”。
非常感谢您对此的帮助。
library(shiny)
library(DT)
iris$New_Data <- c(1:150)
ui <- fluidPage(
title = 'Selectinput column in a table',
h3("Source:", tags$a("Yihui Xie", href = "https://yihui.shinyapps.io/DT-radio/")),
numericInput('num', "enter a number", value = 5, min = 1, max = 10, step = 1),
DT::dataTableOutput('foo'),
verbatimTextOutput('sel'),
actionButton(
"saveBtn",
"Submit Request",
style = "color: #fff; background-color: #282364;
border-color: #2e6da4",
class = "btn btn-primary"
)
)
server <- function(input, output, session) {
data <- reactive({
df <- head(iris, input$num)
for (i in 1:nrow(df)) {
df$species_selector[i] <- as.character(selectInput(paste0("sel1", i),
"",
choices = unique(iris$Species),
width = "100px"))
df$New_Data_selector[i] <- as.character(selectInput(paste0("sel2", i),
"",
choices = unique(iris$New_Data),
width = "100px"))
}
df
})
output$foo = DT::renderDataTable(
data(), escape = FALSE, selection = 'none', server = 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-container');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
)
output$sel = renderPrint({
str(sapply(1:nrow(data()), function(i) input[[paste0("sel", i)]]))
})
observeEvent(input$saveBtn, {
Test_Data <- sapply(1:nrow(data()), function(i) input[[paste0("sel", i)]])
Test_Data <- as.data.frame(Test_Data)
print(Test_Data)})
}
shinyApp(ui, server)
【问题讨论】:
-
我认为应该有类似的方式:stackoverflow.com/questions/42745432/…。但是不知道如何在闪亮中实现它
-
@ismirsehregal:这对使用 sapply 更新值非常有帮助,我发现了您提供的多种解决方案,但它们都与更新第二个下拉列表无关,如果我们有超过 5 个数据表中的下拉列表,什么是理想的解决方案,如果您可以通过一些示例分享,将非常感谢您的帮助。你是希望!!!
-
@ismirsehregal 别担心,我们将等待您的指导......