How to update numericInput value based on the row users select from a DT table

Below is my simple example. So, if users select the 1st row, the value should be 50. If users select the 2nd row, the value should be 100. Is there a way to do it without using the 'refresh' button?

library(shiny)
library(DT)


ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            numericInput("price",
                        "Average price:",
                        min = 0,
                        max = 50,
                        value = 0),

            actionButton('btn', "Refresh")
        ),

        mainPanel(
            DT::dataTableOutput('out.tbl')
        )
    )
)

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

    price_selected <- reactive({
        if (input$out.tbl_rows_selected == 1) { 
            price = 50
        } else {
            price = 100
        } 
    })

    observeEvent (input$btn, {
        shiny::updateNumericInput(session, "price",  value = price_selected())
    })

    output$out.tbl <- renderDataTable({
        Level1 <- c("Branded", "Non-branded")
        Level2 <- c("A", "B")
        df <- data.frame(Level1, Level2)
    })
}

shinyApp(ui = ui, server = server)

Does this sort of work the way you want?

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textOutput("price")
    ),
    
    mainPanel(
      DT::dataTableOutput('out.tbl')
    )
  )
)

server <- function(input, output, session) {
  
  price_selected <- reactive({
    req(input$out.tbl_rows_selected)
    if (input$out.tbl_rows_selected == 1) { 
      price = 50
    } else {
      price = 100
    } 
  })
  
  output$price <- renderText({
    price_selected()
  })
  
  output$out.tbl <- renderDataTable(
    selection = list(mode = 'single', selected = c(1), target = 'row'),
    {
    Level1 <- c("Branded", "Non-branded")
    Level2 <- c("A", "B")
    df <- data.frame(Level1, Level2)
  })
}

shinyApp(ui = ui, server = server)

Hi Matt,

No. We need to use numericInput since users can override 'price' if they choose too.
This is the actual flow:

  1. Users select a row from DT
  2. I create a binary variable for each row in my dataset based on the selected row. For example, if users select 'Branded' then 'A', then every row with 'Branded' and 'A' will get a value 1; otherwise 0.
  3. I'll calculate average price of the items with binary variable 1.
  4. Use the value from #3 as they default value of numericInput.
  5. Users can override the value if they choose too.
  6. I'll use the value from #5 in other calculations.

Hope this clarify. Any suggestions on how to refresh the default value without using a button are welcome. Thanks so much!

I see. So you want users to be able to select cells, not rows, correct?

User selects two cells, one for Level1 and one for Level2, you use this information to calculate average price using some other data, return this average price, and the user can override this value and it gets carried forward.

Is the correct?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.