我期待从当基于条件单击该按钮打开停止dropdownbutton
(shinywidgets
)。这至少避免错失renderUI
为input
模式面板上的内容dropdownButton
错误。
当用户点击一个dropdownButton
,它通常会打开一个面板。在我的情况下,此面板包含依赖于各种变量renderUI
elements。如果还不存在这些变量中,renderUIs
will导致错误吐了出来。
我想知道的是,是否有一种方式来看待点击observeEvent(input$MydropdownButton, { ....})
,然后完全打开,立即面板如果条件不符合,而不是将其切换至关闭停止(不工作版)
我打算做什么,就是给用户一个sweetalert
而不是通知该选项的他必须创建或加载所需数据的用户。我知道该怎么办消息,纯粹是看在“如果别人的办法来阻止开口部分
shinyjs::disable('MydropdownButton')
与if
statement阻止使用该按钮,但这不会让我触发sweetalert
on点击了renderUIs
的,而且我:
答:怕使代码的混乱,以及
B:急于找出是否有在一般办法阻止dropdownButtons
开幕我已经试过这样的事情:
observeEvent(input$MydropdownButton, {
if(!is.null(values$neededData)) { 'just open the dropdownbutton' }
else { toggleDropdownButton('TestDrop')
'run sweetalert code'}
})
但toggleDropdownButton
will仅关闭dropdownButton
panel一旦它已经触发打开,从而有光泽试图render
the ui
element,用所产生的误差,而不是从开放阻止它。
这里有一个完整的server
and ui
code文件,以证明其要求不存在的号码。
服务器文件
shinyServer = function(input, output, session) {
values <- reactiveValues()
output$Reset_Threshold <- renderUI({
if(values$randomNr == 2) { actionButton(inputId = "Reset_Threshold", label = icon("undo")) }
else if(values$randomNr == 1) { actionButton(inputId = "Reset_Threshold", label = icon("table")) }
})
observeEvent(input$TestDrop, {
if(!is.null(values$randomNr )) { print('no problems')}
else { toggleDropdownButton('TestDrop')
# Run other code here to alert user.
}
})
}
UI文件
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
dropdownButton(inputId= "TestDrop",
uiOutput('Reset_Threshold'),
icon = icon("table"), tooltip = tooltipOptions(title = "Click"))
)
```
是不是通过切换下拉菜单,但引用变量randomNr
不存在导致错误。我增加了变数,也是一个甜蜜的对话框时,数据是不是在现在的服务器逻辑它的工作做好准备。
但请注意,它不可能停止从开放的下拉菜单。我们还需要将其关闭。如果你想从完全打开阻止它,你可以在你的数据是没有准备好,仍然会触发一个事件有条件地呈现定期闪亮actionButton
。只要确保只有一个按钮不同的条件下呈现的,他们应该使用相同的输入ID。
function(input, output, session) {
values <- reactiveValues(Filter_df = NULL, randomNr = 0)
output$Reset_Threshold <- renderUI({
if (values$randomNr == 2) {
actionButton(inputId = "Reset_Threshold", label = icon("undo"))
}
else if (values$randomNr == 1) {
actionButton(inputId = "Reset_Threshold", label = icon("table"))
}
})
observeEvent(input$TestDrop, {
if (!is.null(values$Filter_df)) {
print("no problems")
} else {
toggleDropdownButton("TestDrop")
# Run other code here to alert user.
sendSweetAlert(session, "data not ready")
}
})
}
刚才呈现不同的按钮。我使用一个单独的文件app.R
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
uiOutput("button")
)
server <- function(input, output, session) {
values <- reactiveValues(Filter_df = NULL, randomNr = 0)
output$button <- renderUI({
if (values$randomNr == 1) {
dropdownButton(
inputId = "dropdown",
actionButton(inputId = "Reset_Threshold", label = icon("table")),
icon = icon("table"), tooltip = tooltipOptions(title = "Click")
)
} else {
actionButton(
inputId = "alert",
NULL,
icon = icon("table")
)
}
})
observeEvent(input$alert, {
sendSweetAlert(session, "data not ready")
})
}
shiny::shinyApp(ui, server)