Change output in Shiny only when I change value in numericInput()

Asked

Viewed 83 times

3

I have this table in my Shiny app.

| a | 2 |
| b | 3 |
| c | 5 |

Has a box to choose a line (from 1 to 3), and from this it prints the value for that line.

There is also another box so I can change this value. Only the function numericInput() asks for an initial value, so when I select a line it already changes the output to the initial value.

Only that I want that value to change only if I change the value in the numericInput(). I can set the initial value to a negative value and put a if() but I don’t want it that way.


Here the example:

library('shiny')

ui <- fluidPage(

  numericInput('line', 'Line Choice:', value = 1, min = 1, max = 3),
  numericInput('number', 'Value Choice:', value = 0),
  textOutput('text')

)

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

    df <- data.frame(c('a', 'b', 'c'), c(2, 3, 5))

    output$text <- renderText({

      i <- input$line
      df[i,2] <- input$number

      paste0(df[i,1], ': ', df[i, 2])

    })
  })

}
shinyApp(ui, server)

Thank you

1 answer

2


You can generate this numericInput on the server side, so you can determine which should be the initial value. Example:

library('shiny')

ui <- fluidPage(

  numericInput('line', 'Line Choice:', value = 1, min = 1, max = 3),
  uiOutput("number_ui"),
  textOutput('text')

)

server <- function(input, output) {

    df <- data.frame(c('a', 'b', 'c'), c(2, 3, 5))


    output$number_ui <- renderUI({
      numericInput('number', 'Value Choice:', df[input$line, 2])
    })

    output$text <- renderText({
      i <- input$line
      if(!is.null(input$number))
        df[i,2] <- input$number
      paste0(df[i,1], ': ', df[i, 2])
    })

}
shinyApp(ui, server)

More details in this article

  • Thanks Falbel! .

  • Good!! These things are annoying in Shiny... In the end you end up generating almost all inputs directly on the server

Browser other questions tagged

You are not signed in. Login or sign up in order to post.