简体   繁体   中英

update values in numericInput R shiny

I have an app wherein users can input numeric values for certain fields (using numericInput() ). Alternatively, they can choose to select values from a reference table (via a checkboxInput() field).

I'm able to code this behaviour in the script properly. But I also want that if the checkboxInput field is selected, the values displayed in the numericInput() get updated ie the default values or previously written values are overwritten.

在此处输入图片说明

In the screenshot, the numericInput fields are highlighted in yellow. The top field has a default value of 14 whereas the others are empty. I want that the if the "Copy reference values?" checkboxInput is selected, the copied values get displayed in the corresponding fields (k1 = 72.49 for "Flow Coef. for dP" etc.)

My code is as below:

fluidRow(
    column(4,
           numericInput(inputId = "Area", 
                        label = tags$div(HTML(paste("rea (m", tags$sup(2), ")", sep = ""))),
                        min = 1, max = 100, step = 0.1, value = 14),
           numericInput(inputId = "k1", label = "Flow coef. for dP", min = 1.0, max = 600.0, value = ""),
           numericInput(inputId = "k2", label = "Flow exponent for dP" , min = 1.0, max = 20.0, value = "")
           checkboxInput("copyVals", "Copy Reference Values?", value = FALSE)
)

You'll want to use an observeEvent and updateNumericInput s. Since you didn't provide a reproducible example here is a mockup:

library("shiny")
library("DT")

data <- data.frame(area = 18.61, k1 = 74.29, k2 = 1.44)

server <- function(input, output, session) {
  # assuming your data is reactive, not static
  data_reac <- reactive({
    data
  })

  output$parm_tab <- renderDataTable({
    datatable(data_reac())
  })

  # set the values if checked
  observeEvent(input$copyVals == TRUE, {
    c_data <- data_reac()

    updateNumericInput(session, "area", value = c_data$area)
    updateNumericInput(session, "k1", value = c_data$k1)
    updateNumericInput(session, "k2", value = c_data$k2)
  }, ignoreInit = TRUE)

}

ui <- fluidPage(
  sidebarLayout(
  sidebarPanel(
      numericInput(inputId = "area", label = "Area", min = 1, max = 100, step = 0.1, value = 14),
      numericInput(inputId = "k1", label = "Flow coef. for dP", min = 1.0, max = 600.0, value = ""),
      numericInput(inputId = "k2", label = "Flow exponent for dP" , min = 1.0, max = 20.0, value = ""),
      checkboxInput("copyVals", "Copy Reference Values?", value = FALSE)
  )
  , mainPanel(
      dataTableOutput("parm_tab")
    )
  )
)

shinyApp(ui = ui, server = server)

Before

在此处输入图片说明

After

在此处输入图片说明

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