Javascript R Shinny-使用自定义消息处理程序禁用单选按钮组中的单个单选按钮
下面的应用程序包含一个带有两个选项下载和上载的selectInput输入$set1和一个使用renderUI呈现的单选按钮组input$set2。我正在使用一个自定义消息处理程序,根据selectInput的值禁用单选按钮组中的单选按钮,如果input$set1==“download”,则禁用上载单选按钮,否则启用它 以下是应用程序:Javascript R Shinny-使用自定义消息处理程序禁用单选按钮组中的单个单选按钮,javascript,jquery,html,r,shiny,Javascript,Jquery,Html,R,Shiny,下面的应用程序包含一个带有两个选项下载和上载的selectInput输入$set1和一个使用renderUI呈现的单选按钮组input$set2。我正在使用一个自定义消息处理程序,根据selectInput的值禁用单选按钮组中的单选按钮,如果input$set1==“download”,则禁用上载单选按钮,否则启用它 以下是应用程序: library(shiny) library(shinyWidgets) library(shinydashboard) toggleRadioOpt <
library(shiny)
library(shinyWidgets)
library(shinydashboard)
toggleRadioOpt <- function(session, inputId, choice, enable = TRUE, selected) {
session$sendCustomMessage(
type = 'toggleRadioOpt',
message = list(id = inputId, choice = choice, enable = enable, selected = selected)
)
}
modUI <- function(id) {
ns <- NS(id)
tagList(
tags$head(
tags$script("
Shiny.addCustomMessageHandler('toggleRadioOpt',
function(data) {
$('#' + data.id + ' input[value = \"' + data.choice + '\"]').attr('disabled', !data.enable).prop('checked', false);
if(data.selected !== null) {
$('#' + data.id + ' input[value = \"' + data.selected + '\"]').prop('checked', true);
}
}
);
")
),
prettyRadioButtons(ns('set1'), label = 'Select', choices = c('download', 'upload', 'abb')),
# selectInput(ns('set1'), label = 'Select', choices = c('download', 'upload', 'sql')),
uiOutput(ns('taskUI'))
)
}
modServer <- shinyServer(function(input, output, session) {
ns = session$ns
output$taskUI <- renderUI({
switch(input$set1,
'download' = selectInput(ns('data'), '', state.area),
'upload' = selectInput(ns('data'), '', state.center),
'abb' = tagList(
selectInput(ns('data'), '', state.abb),
actionButton(ns('upload'), 'Upload')
)
)
prettyRadioButtons(ns('set2'), label = '', choices = c('split', 'upload'))
})
observe({
if(!is.null(input$set2)) {
if(input$set1 %in% c('download', 'abb')) {
toggleRadioOpt(session = session, inputId = ns('set2'), choice = 'upload', enable = F, selected = 'split')
} else {
toggleRadioOpt(session = session, inputId = ns('set2'), choice = 'upload', enable = T, selected = 'split')
}
}
})
})
# UI ----------------------------------------------------------------------
ui = fluidPage(modUI('first'))
# SERVER ------------------------------------------------------------------
server <- shinyServer(function(input, output, session) {
callModule(modServer, 'first')
})
shinyApp(ui, server)
如果在UI中创建单选按钮组,则处理程序将按预期工作,但如果使用renderUI创建按钮,则处理程序将失败。具体地说,它在启动时失败,但如果随后更改了输入$set1,它就会工作。我不熟悉使用自定义消息处理程序,所以我不确定这里发生了什么或如何修复它。我认为这可能与在加载UI之前触发jQuery有关,但我不能确定
我认为这可能与jQuery被触发有关
在UI加载之前,但我不能确定
我想是的。在加载UI之前,输入$set2为空。你可以做:
observe({
if(!is.null(input$set2)){
if(input$set1 == 'download') {
toggleRadioOpt(session = session, inputId = ns('set2'), choice = 'upload',
enable = F, selected = 'split')
} else {
toggleRadioOpt(session = session, inputId = ns('set2'), choice = 'upload',
enable = T, selected = 'split')
}
}
})
如果我仅在renderUI中渲染set2,但如果多个元素以set1的值为条件进行渲染,则此方法不起作用。我已经编辑了我的文章来展示一个例子,请看一下好吗?向if语句添加其他元素无效。我求助于拨动收音机的开关。。。在shinyjs::delay中使用任意延迟。这是可行的,但我不确定这是否是最好的方法。