0
votes

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 i.e. the default values or previously written values are overwritten.

enter image description here

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)
)
1

1 Answers

3
votes

You'll want to use an observeEvent and updateNumericInputs. 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

enter image description here

After

enter image description here