0

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

我试图server.R通过添加一个计数器来修改reactiveValues

# 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$cpt更改时值会input$s1更改,但selectGrEv()不会刷新绘图。

当用户也发生变化时,如何刷新渲染图input$s1

4

1 回答 1

1

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

尝试

##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)}
  })
})
于 2016-01-11T12:22:26.157 回答