1

radioGroupButtonsこのコードに基づいてさまざまな色を使用しています: https://github.com/dreamRs/shinyWidgets/issues/41# 選択肢を更新できるようにしたいのですが、 で更新すると色が消えupdateRadioGroupButtonsます。選択した値のみを更新している場合はすべて問題ありませんが、更新するchoiceValueschoiceNames色が消えます。

再現可能なコード:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
    radioGroupButtons(
        inputId = "sent", 
        label = "Sentiment", 
        choiceValues = -2:1, 
        checkIcon = list(yes = icon("check")),
        choiceNames = paste0(-2:1),
        justified = TRUE, width = "300px"
    ),
    tags$script("$(\"input:radio[name='sent'][value='-2']\").parent().css('background-color', '#DE6B63');"),
    tags$script("$(\"input:radio[name='sent'][value='-1']\").parent().css('background-color', '#EDB6B2');"),
    tags$script("$(\"input:radio[name='sent'][value='0']\").parent().css('background-color', '#E7E7E7');"),
    tags$script("$(\"input:radio[name='sent'][value='1']\").parent().css('background-color', '#B2EDB5');"),
    tags$script("$(\"input:radio[name='sent'][value='2']\").parent().css('background-color', '#7EF373');"),
    verbatimTextOutput(outputId = "res"),
    actionButton(inputId = "update", label = "Update")
)

server <- function(input, output, session) {
    
    output$res <- renderPrint(input$sent)
    
    observeEvent(input$update, {
        updateRadioGroupButtons(session = session, inputId = "sent", choiceValues = -2:2, choiceNames = paste0(-2:2))
    }, ignoreInit = TRUE)
}

shinyApp(ui, server)
4

1 に答える 1

1

もっと簡単な解決策があるかもしれませんが、ミューテーション オブザーバーを設定すると、変更 (ミューテーション) が検出されるたびに JavaScript を適用できます。

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  radioGroupButtons(
    inputId = "sent", 
    label = "Sentiment", 
    choiceValues = -2:1, 
    checkIcon = list(yes = icon("check")),
    choiceNames = paste0(-2:1),
    justified = TRUE, width = "300px"
  ),
  tags$script("
  // this is the setting in the first instance
  $(\"input:radio[name='sent'][value='-2']\").parent().css('background-color', '#DE6B63');
$(\"input:radio[name='sent'][value='-1']\").parent().css('background-color', '#EDB6B2');
$(\"input:radio[name='sent'][value='0']\").parent().css('background-color', '#E7E7E7');
$(\"input:radio[name='sent'][value='1']\").parent().css('background-color', '#B2EDB5');
$(\"input:radio[name='sent'][value='2']\").parent().css('background-color', '#7EF373');

// lets listen to changes with a mutation observer
  // select the target node
var target = document.getElementById('sent')  

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if( document.getElementById('sent')) {
// here we apply the formatting again
$(\"input:radio[name='sent'][value='-2']\").parent().css('background-color', '#DE6B63');
$(\"input:radio[name='sent'][value='-1']\").parent().css('background-color', '#EDB6B2');
$(\"input:radio[name='sent'][value='0']\").parent().css('background-color', '#E7E7E7');
$(\"input:radio[name='sent'][value='1']\").parent().css('background-color', '#B2EDB5');
$(\"input:radio[name='sent'][value='2']\").parent().css('background-color', '#7EF373');
    }
  });    
});

// configuration of the observer:
// it works with childList although I would expect it to work with characterData
var config = {
  childList: true,
  subtree: true,
  attruibutes: true,
  characterData: true
};

// pass in the target node, as well as the observer options
observer.observe(target, config); 
              "),
  
  verbatimTextOutput(outputId = "res"),
  actionButton(inputId = "update", label = "Update")
)

server <- function(input, output, session) {
  
  output$res <- renderPrint(input$sent)
  
  observeEvent(input$update, {
    updateRadioGroupButtons(session = session, inputId = "sent", choiceValues = -2:2, choiceNames = paste0(-2:2), checkIcon = list(yes = icon("check")))
  }, ignoreInit = TRUE)
}

shinyApp(ui, server)
于 2021-02-28T23:56:36.150 に答える