简体   繁体   中英

R Shiny: Reactive Value in Modal not working

The header of my modalDialog is a reactiveValue. Once a certain input is changed within the modal, I would like the header to change. I have tried a few different ways and haven't been able to get it working seamlessly. The header either doesn't update, only updates if I close/reopen the modal, or causes the modal to re-render completely when the header is updated immediately.

Currently, I'm stuck on the last one (modal re-rendering completely). However, when I created the minimal, reproducible example (below), the header only updates if I close & reopen the modal.

ui<-fluidPage(
  actionButton(inputId="model", label="Edit Model")
)

server<-function(input, output, session) {
  rv<-reactiveValues(header="Standard Model")

  observeEvent(input$model, {
    showModal(
      modalDialog(
        fluidPage(
          fluidRow(h3(rv$header)),
          sliderInput(inputId="factor_1", "Factor #1", value=70, min=0, max=100),
          sliderInput(inputId="factor_2", "Factor #2", value=30, min=0, max=100)
        ),
        footer=modalButton("Save Weights"), size="s", easyClose=TRUE
      )
    )
  })

  observe({if (!is.null(input$factor_1)) {
    if (input$factor_1!=70) {
      rv$header<-"Custom Model"
      }
    }
  })
}

shinyApp(ui=ui, server=server)

Essentially, I want the title to change from 'Standard Model' to 'Custom Model' (without closing/reopening or re-rendering the modal) once the input for Factor #1 is changed for the first time.

Using renderUI/uiOutput :

library(shiny)

ui <- fluidPage(
  actionButton("model", label = "Edit Model") 
)

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

  rv <- reactiveValues(header = "Standard Model")

  observeEvent(input[["model"]], {
    showModal(
      modalDialog(
        fluidPage(
          uiOutput("modalTitle"),
          sliderInput("factor_1", "Factor #1", value=70, min=0, max=100),
          sliderInput("factor_2", "Factor #2", value=30, min=0, max=100)
        ),
        footer = modalButton("Save Weights"), 
        size="s", easyClose=TRUE
      )
    )
  })

  output[["modalTitle"]] <- renderUI({
    fluidRow(h3(rv$header))
  })

  observe({
    if (!is.null(input$factor_1)) {
      if (input$factor_1 != 70) {
        rv$header <- "Custom Model"
      }
    }
  })

}

shinyApp(ui=ui, server=server)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM