Shiny - actionButton:渲染而不点击

Shiny - actionButton: rendering without clicking

在下面的简单示例中,图形是在使用按钮刷新后根据 checkboxGroupInput 的输入呈现的。 在应用程序第一次启动时,选中的复选框由 selectInput s1.

提供
# ui.R
library(shiny)
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("s1", "Select 1", 1:5)
    ),
      mainPanel(
        plotOutput("myplot"),
        uiOutput("chk.output"),
        actionButton("refresh", "Refresh")
      )
    )
  )
)
# server.R
library(shiny)
shinyServer(function(input, output, session) { 
  output$chk.output <- renderUI(checkboxGroupInput("Chk", label = "Choices", choices = list("1" = 1, "2" = 2, "3"= 3, "4"=4, "5"=5), selected = input$s1, inline = TRUE, width = "100%"))
  selectGrEv <- eventReactive(input$refresh, {input$Chk}, ignoreNULL = FALSE)
  output$myplot <- renderPlot({ 
    plot(1:5, 1:5)
    if (1 %in% selectGrEv()) {text(1,1, labels= 1, col = "red", pos = 3)}
    if (2 %in% selectGrEv()) {text(2,2, labels= 2, col = "blue", pos = 3)}
    if (3 %in% selectGrEv()) {text(3,3, labels= 3, col = "black", pos = 3)}
    if (4 %in% selectGrEv()) {text(4,4, labels= 4, col = "orange", pos = 3)}
    if (5 %in% selectGrEv()) {text(5,5, labels= 5, col = "green", pos = 1)}
  })
})

当用户更改input$s1时,checkboxGroupInput会刷新但不会刷新什么是正常的。 我想通过单击“刷新”按钮或更改 input$s1.

来刷新绘图

我试图通过添加计数器 reactiveValues:

来修改 server.R
# server.R
library(shiny)
shinyServer(function(input, output, session) { 
  output$chk.output <- renderUI(checkboxGroupInput("Chk", label = "Choices", choices = list("1" = 1, "2" = 2, "3"= 3, "4"=4, "5"=5), selected = input$s1, inline = TRUE, width = "100%"))

  values <- reactiveValues(cpt = 0) # Here code was added
  observeEvent(input$s1, values$cpt <- values$cpt + 1) # Here code was added

  selectGrEv <- eventReactive(input$refresh | values$cpt, {input$Chk}, ignoreNULL = FALSE) # Here code was modified
  output$myplot <- renderPlot({ 
    plot(1:5, 1:5)
    if (1 %in% selectGrEv()) {text(1,1, labels= 1, col = "red", pos = 3)}
    if (2 %in% selectGrEv()) {text(2,2, labels= 2, col = "blue", pos = 3)}
    if (3 %in% selectGrEv()) {text(3,3, labels= 3, col = "black", pos = 3)}
    if (4 %in% selectGrEv()) {text(4,4, labels= 4, col = "orange", pos = 3)}
    if (5 %in% selectGrEv()) {text(5,5, labels= 5, col = "green", pos = 1)}
  })
})

values$cptinput$s1 更改时更改,但 selectGrEv() 不更改,因此不会刷新绘图。

如何在用户更改时也刷新渲染图 input$s1

我只知道一个(可能不是很好的变体)

尝试

##Server.r
shinyServer(function(input, output, session) { 
  output$chk.output <- renderUI(checkboxGroupInput("Chk", label = "Choices", choices = list("1" = 1, "2" = 2, "3"= 3, "4"=4, "5"=5), selected = input$s1, inline = TRUE, width = "100%"))
  selectGrEv <- reactiveValues(aa=0)

  observeEvent(input$s1,{
    selectGrEv$aa=input$s1
    })

  observeEvent(input$refresh,{
    selectGrEv$aa=input$Chk
  })

  output$myplot <- renderPlot({ 

    plot(1:5, 1:5)
    if (1 %in%  selectGrEv$aa) {text(1,1, labels= 1, col = "red", pos = 3)}
    if (2 %in% selectGrEv$aa) {text(2,2, labels= 2, col = "blue", pos = 3)}
    if (3 %in% selectGrEv$aa) {text(3,3, labels= 3, col = "black", pos = 3)}
    if (4 %in% selectGrEv$aa) {text(4,4, labels= 4, col = "orange", pos = 3)}
    if (5 %in% selectGrEv$aa) {text(5,5, labels= 5, col = "green", pos = 1)}
  })
})