有没有办法捕获是否单击了任何单选按钮并从中做出反应

Is there a way to capture whether any radio button has been clicked and react from it

请参考下面的可重现代码。一切正常,直到一点。如果您 select 一个团队,并通过按下操作按钮选择权重,然后单击填充按钮,则会出现 table 和权重。

不过有一件事。如果你 select,说 team = a,然后输入 +10%,然后切换到 team = b。输入仍为+10%。我希望它恢复为 0,以便您始终重新开始。

无论如何都要这样做?

图书馆(闪亮)

library(dplyr)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)

df = data.frame(team = c("a", "b", "c", "d"),
                weights = c(0, 0, 0, 0))
df

ui = dashboardPage(
  header = dashboardHeader(title = "teams"),
  sidebar = dashboardSidebar(),
  body = dashboardBody(
    prettyRadioButtons(
      inputId = "radio1",
      label = "Select Teams",
      choices = df$team
    ),
    br(),

div(style = "margin-top: 10px;width: 100px; display: inline-block", textOutput("text1a")),
div(
  style = "width: 25px; display: inline-block; margin-left: 1px",
  actionBttn(
    inputId = "action1a",
    size = "xs",
    label = "-",
    style = "material-flat",
    color = "primary"
  )
),
div(style = "width: 25px; display: inline-block; margin-left: 10px; text-align: center", textOutput("text1b")),
div(
  style = "width: 25px; display: inline-block; margin-left: 25px",
  actionBttn(
    inputId = "action1b",
    size = "xs",
    label = "+",
    style = "material-flat",
    color = "primary"
  )
),
br(),
br(),

actionBttn(
  inputId = "populate",
  size = "xs",
  label = "Populate Weights",
  style = "material-flat",
  color = "danger"
),
br(),
br(),
div(style = "width: 800px; margin-left: 10px", tableOutput("table1"))
   ))  # End of Dashboard Body and Page

server = function(input, output, session) {
  rv = reactiveValues(action1 = 0, df = df)
  
  output$text1a = renderText(paste("Team", input$radio1, sep = " "))
  
  rv$action1 = eventReactive(c(input$action1a, input$action1b),
                             {
                               min(max(-5 * input$action1a + 5 * input$action1b,-50),50)
                             })
  
  output$text1b = renderText({
    if (rv$action1() >= 0) {
      paste("+", rv$action1(), sep = "")
    }
    else {
      rv$action1()
    }
  })
  
  rv$df = eventReactive(
    input$populate,
    df <<- rbind(
      df %>% filter(team == input$radio1) %>% mutate(weights = rv$action1()),
      df %>% filter(team != input$radio1)
    ) %>% arrange(team)
  )

  output$table1 = renderTable({
    rv$df()
  }) 
}

shinyApp(ui = ui,
         server = server,
         options = list(launch.browser = T))

以下是我对您想要的解释。我也把信息放在代码中,但我会在这里给出一个简短的想法。我没有使用 eventReactive,而是使用了 reactiveValues。这样我就有了一个数字,我可以用不同的输入来改变它。然后,我对三个输入 radio1action1aaction1b 中的每一个都使用了 observeEvent。如果按下 radio1 上的任何按钮,则 reactiveValues 设置为 0。如果按下 action1a,则从 reactiveValues 减去 5,如果 [=] 当然加 5 15=] 被按下。

library(dplyr)
library(shiny) #Added the library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)

df = data.frame(team = c("a", "b", "c", "d"),
                weights = c(0, 0, 0, 0))
df

ui = dashboardPage(
  header = dashboardHeader(title = "teams"),
  sidebar = dashboardSidebar(),
  body = dashboardBody(
    prettyRadioButtons(
      inputId = "radio1",
      label = "Select Teams",
      choices = as.character(df$team)
    ),
    br(),
    
    div(style = "margin-top: 10px;width: 100px; display: inline-block", textOutput("text1a")),
    div(
      style = "width: 25px; display: inline-block; margin-left: 1px",
      actionBttn(
        inputId = "action1a",
        size = "xs",
        label = "-",
        style = "material-flat",
        color = "primary"
      )
    ),
    div(style = "width: 25px; display: inline-block; margin-left: 10px; text-align: center", textOutput("text1b")),
    div(
      style = "width: 25px; display: inline-block; margin-left: 25px",
      actionBttn(
        inputId = "action1b",
        size = "xs",
        label = "+",
        style = "material-flat",
        color = "primary"
      )
    ),
    br(),
    br(),
    
    actionBttn(
      inputId = "populate",
      size = "xs",
      label = "Populate Weights",
      style = "material-flat",
      color = "danger"
    ),
    br(),
    br(),
    div(style = "width: 800px; margin-left: 10px", tableOutput("table1"))
  ))  # End of Dashboard Body and Page

server = function(input, output, session) {
  rv = reactiveValues(action1 = 0, df = df)
  
  output$text1a = renderText(paste("Team", input$radio1, sep = " "))
  
  DF<-reactiveValues("DF" = 0) #Using a reactiveValue instead of eventreactive
  
  observeEvent(input$action1a,{ #Observe's action 1a. When pressed, take the current reactiveValue DF$DF, and -5
    temp<-isolate(DF$DF)
    DF$DF<-temp-5
  })
  
  observeEvent(input$action1b,{ #Observe's action 1b. When pressed, take the current reactiveValue DF$DF, and +5
    temp<-isolate(DF$DF)
    DF$DF<-temp+5
  })
  
  observeEvent(input$radio1,{ #Observe's radio1. If this button is changed, reset reactiveValue to 0. 
    DF$DF<-0
  })
  
  
  output$text1b = renderText({ #Rendertext only displays the reactiveValue
    DF$DF
  })
  
  rv$df = eventReactive(
    input$populate,
    df <<- rbind(
      df %>% filter(team == input$radio1) %>% mutate(weights = DF$DF), #Changed weights to be the reactiveValues
      df %>% filter(team != input$radio1)
    ) %>% arrange(team)
  )
  
  
  output$table1 = renderTable({
    rv$df()
  }) 
}

shinyApp(ui = ui,
         server = server,
         options = list(launch.browser = T))

当我从 eventReactive 切换时,我发现这不是理想的解决方案,但这是我完成您正在做的事情的方式。另一种想法是使用 shinyjs,它可以重置某些输入,但我不确定它是否适用于您的原始代码,因为它可能不会重置 eventReactive。祝您好运,希望对您有所帮助!