意外点击按钮

时间:2017-04-18 13:30:18

标签: r shiny

我写小例子

library(shiny)

ui <- fluidPage(
  fluidRow(
    wellPanel(radioButtons(inputId = "radio1", label="Radio 1", choices = c("Yes", "No"), selected = "No"),
    actionButton("btnClick1", "Click 1"))
  ),
  fluidRow(
    wellPanel(radioButtons(inputId = "radio2", label="Radio 2", choices = c("Yes", "No"), selected = "No"),
    actionButton("btnClick2", "Click 2"))
  ),
  fluidRow(
    wellPanel(radioButtons(inputId = "radio3", label="Radio 3", choices = c("Yes", "No"), selected = "No"),
              actionButton("btnClick3", "Click 3"))
  )

)

options(shiny.reactlog=TRUE)

server <-function(input, output, session) {

  values <- reactiveValues(radio1 = FALSE, radio2 = FALSE, radio3 = FALSE)

  observe(
    if (input$btnClick1)
    {
      if (values$radio1 == TRUE)
      {
        isolate({cat("input$btnClick1 ", input$btnClick1, "\n")});
      }
    }
  );
  observeEvent(input$radio1, {
    print("observeEvent input$radio1");
    if (input$radio1 == "Yes") {
      print("Yes");
      values$radio1 = TRUE;
    } else {
      print("No");
      values$radio1 = FALSE;
    }
  });

  observe(
    if (input$btnClick2)
    {
      if (values$radio2 == TRUE)
      {
        isolate({cat("input$btnClick2 ", input$btnClick2, "\n")});
      }
    }
  );
  observeEvent(input$radio2, {
    print("observeEvent input$radio2");
    if (input$radio2 == "Yes") {
      print("Yes");
      values$radio2 = TRUE;
    } else {
      print("No");
      values$radio2 = FALSE;
    }
    freezeReactiveValue(values, "radio2")
  });

  observeEvent(input$btnClick3, {
    req(values$radio3 == TRUE);
    isolate({cat("input$btnClick3 ", input$btnClick3, "\n")});
  });
  observeEvent(input$radio3, {
    print("observeEvent input$radio3");
    if (input$radio3 == "Yes") {
      print("Yes");
      values$radio3 = TRUE;
    } else {
      print("No");
      values$radio3 = FALSE;
    }
  });
}

shinyApp(ui = ui, server = server)

https://gist.github.com/bezany/d9380a1e80b213dcd795f605355912bd 在RStudio中运行示例:runGist("d9380a1e80b213dcd795f605355912bd")

如果点击&#34;点击1&#34;并设置&#34; Radio 1&#34;到&#34;是&#34;看到那个句柄&#34;输入$ btnClick1&#34;。为什么会这样? 当前点击次数在&#34;输入$ btnClick1&#34;触发这个,当&#34; Radio 1&#34;设置为&#34;是&#34;?记得点击处理有多闪亮?

1 个答案:

答案 0 :(得分:0)

好吧,我想你忘了在你的第一个观察者中隔离$ radio1的值。 因此,当输入$ radio1变为TRUE时,观察者无效。