重新渲染相同的UI次数

时间:2016-10-20 12:29:32

标签: shiny

我有这个简单闪亮的应用程序,有两个动作按钮。 “渲染”按钮呈现滑块输入控件,而“删除”按钮则删除相同的滑块输入。这第一次工作正常,但第二次尝试重新渲染相同的滑块输入不起作用。任何想法或解释代码可能有什么问题?

这是代码:

    ui <- fluidPage(
            actionButton("render", "Render"),
            actionButton("remove", "Remove"),
            uiOutput("moreControls")
    )

    server <- function(input, output) {
            observeEvent(input$render, {
                    if (input$render > 0) {
                            output$moreControls <- renderUI({
                                    tagList(
                                            sliderInput("n", "N", 1, 1000, 500)
                                    )
                            })    
                    }
            })
            observeEvent(input$remove, {
                    if (input$remove > 0) {
                            removeUI(
                                    selector = "div[id='moreControls']"
                            )
                    }
            })

    }
    shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

You can set the control to NULL to remove it. See code.

library(shiny)  

ui <- fluidPage(
  actionButton("render", "Render"),
  actionButton("remove", "Remove"),
  uiOutput("moreControls")
)

server <- function(input, output) { 

  rv <- reactiveValues() 

  observeEvent(input$render, { rv$action <- 'render' })
  observeEvent(input$remove, { rv$action <- 'remove' }) 

  output$moreControls <- renderUI({ 

    if (is.null(rv$action)) {return(NULL)} 

    if(rv$action == 'render'){ 
      sliderInput(inputId = "n",label =  "N", min = 1, max = 1000, value = 500)    
    } else {  
      return(NULL)
    } 
  }) 
}
shinyApp(ui, server)