【发布时间】:2020-10-22 05:49:20
【问题描述】:
我可以在 UI 中为输入字段使用条件而不涉及服务器端。但是当我尝试为 input_rows_selected 或按钮状态之类的事情做这件事时;它们不起作用。
下面我有几个设置了条件的输入字段。第二个字段取决于第一个被选中的内容。然后第一个按钮取决于第二个字段中被选中的内容。
这就是问题所在。如果在数据表中选择了行,则应该只显示第三个按钮。如果选择了一行,我已经让它工作了,但不超过一行。然后第三个按钮下方的 html 和表格应该只在按下第三个按钮后显示。目前显示的是html,table不工作。
想法?我希望在 UI 方面尽可能地保留它。这样,我可以轻松地将按钮隐藏/显示功能调整到各种其他代码块。但我不确定这是否可能。
编辑:更新为当前代码 编辑 2:更新为最终工作代码
if (interactive()) {
library(shiny)
items <- data.frame(
category = c("Room", "IceBreaker", "Activity", "Break"),
group = c(1, 2, 3, 4),
className = c ("red_point", "blue_point", "green_point", "purple_point"),
content = c("Big Room", "Introductions", "Red Rover", "Lunch"),
length = c(480, 60, 120, 90)
)
ui <- fluidPage(shinyUI(
tagList(
useShinyalert(),
useShinyjs(),
title = "Conditional Inputs",
tabsetPanel(id = "mainTabset",
tabPanel(
title = "Wheeeeeee!",
class = "inputs",
column(
12,
selectInput(
inputId = "input1",
label = "A, B, or C?",
choices = c(
Choose = '',
A = 'a',
B = 'b',
C = 'c'
),
selectize = FALSE
),
conditionalPanel(
condition = "input.input1 !== ''",
selectInput(
inputId = "input2",
label = "D, or E?",
choices = c(Choose = '',
D = 'd',
E = 'e'),
selectize = FALSE
)
),
conditionalPanel(
condition = "input.input2 !== ''",
actionButton(
"button1",
"SUBMIT",
style = "background-color:#221B70;
color:#E0EB15;
border-color:#E61029;
border-style:double;
border-width:4px;
border-radius:50%;
font-size:19px;"
),
DT::dataTableOutput("tbl1"),
conditionalPanel(
condition = "typeof input.tbl1_rows_selected !== 'undefined' && input.tbl1_rows_selected.length > 0",
actionButton(
"button2",
"GENERATE TABLE",
style = "background-color:#221B70;
color:#E0EB15;
border-color:#E61029;
border-style:double;
border-width:4px;
border-radius:50%;
font-size:19px;"
)
),
conditionalPanel(condition = "input.button2 > 0",
div(
"Selected items:", DT::dataTableOutput("tbl2")
))
)
)
))
)
))
server <- function(input, output) {
observeEvent(input$button1, {
output$tbl1 <- DT::renderDataTable({
items
}, selection = 'multiple',
class = "display nowrap compact",
extensions = 'Scroller',
options = list(dom = 'Bfrtip'))
})
observeEvent(input$button2, {
table <- items[input$tbl1_rows_selected,c(2,3,4)]
output$tbl2 <- DT::renderDataTable({
table
})
})
}
shinyApp(ui, server)
}
【问题讨论】:
标签: r shiny shiny-server shiny-reactivity