R 闪亮条件 button/divs/table 如果按下按钮或从 DT 选择行
Rshiny conditional button/divs/table if button is pressed or rows selected from DT
我可以在 UI 中对输入字段使用条件,而无需涉及服务器端。但是当我尝试为 input_rows_selected 或按钮状态之类的事情做这件事时;他们不工作。
下面我有几个设置了条件的输入字段。第二个字段取决于第一个被选中的内容。然后第一个按钮取决于第二个字段中的内容被选中。
这是错误的地方。第三个按钮应该仅在数据 table 中选择了行时才会显示。如果选择了一行,但不超过一行,我已经让它工作了。然后第三个按钮下方的 html 和 table 只应在按下第三个按钮后显示。目前显示html,table不显示
想法?我希望尽可能将它保留在 UI 一侧。这样我就可以轻松地将按钮 hide/display 功能调整为各种其他代码块。但我不确定这是否可能。
编辑:更新为当前代码
编辑 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)
}
input$tbl1_rows_selected
如果只选中一行是一个整数,如果选中几行就是一个向量,如果没有选中一行就是NULL
。所以合适的条件是
"input.tbl1_rows_selected !== null"
input$button2
始终是一个整数。它被初始化为 0
,然后每次按下按钮时它都会递增。检查按钮是否被按下的条件是
"input.button2 > 0"
我不明白“第三个按钮下方的 table”是什么意思。我在这个按钮下面没有看到任何 table。
编辑
那行不通:
"input.tbl1_rows_selected !== null"
有效:
"input.tbl1_rows_selected.length > 0"
当input$tbl1_rows_selected
在R中为NULL
时,input.tbl1_rows_selected
似乎是一个空JavaScript数组。否则它是一个非空数组
我可以在 UI 中对输入字段使用条件,而无需涉及服务器端。但是当我尝试为 input_rows_selected 或按钮状态之类的事情做这件事时;他们不工作。
下面我有几个设置了条件的输入字段。第二个字段取决于第一个被选中的内容。然后第一个按钮取决于第二个字段中的内容被选中。
这是错误的地方。第三个按钮应该仅在数据 table 中选择了行时才会显示。如果选择了一行,但不超过一行,我已经让它工作了。然后第三个按钮下方的 html 和 table 只应在按下第三个按钮后显示。目前显示html,table不显示
想法?我希望尽可能将它保留在 UI 一侧。这样我就可以轻松地将按钮 hide/display 功能调整为各种其他代码块。但我不确定这是否可能。
编辑:更新为当前代码 编辑 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)
}
input$tbl1_rows_selected
如果只选中一行是一个整数,如果选中几行就是一个向量,如果没有选中一行就是NULL
。所以合适的条件是
"input.tbl1_rows_selected !== null"
input$button2
始终是一个整数。它被初始化为 0
,然后每次按下按钮时它都会递增。检查按钮是否被按下的条件是
"input.button2 > 0"
我不明白“第三个按钮下方的 table”是什么意思。我在这个按钮下面没有看到任何 table。
编辑
那行不通:
"input.tbl1_rows_selected !== null"
有效:
"input.tbl1_rows_selected.length > 0"
当input$tbl1_rows_selected
在R中为NULL
时,input.tbl1_rows_selected
似乎是一个空JavaScript数组。否则它是一个非空数组