Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(shiny)
- ui <- fluidPage(
- numericInput("temp_c", "Celsius", NA),
- numericInput("temp_f", "Fahrenheit", NA)
- )
- server <- function(input, output, session) {
- c_to_f <- function(c, decimals = 1) {
- round((c * 9 / 5) + 32, decimals)
- }
- f_to_c <- function(f, decimals = 1) {
- round((f - 32) * 5 / 9, decimals)
- }
- observeEvent(input$temp_c, {
- # This early return is to prevent event handler cycles.
- # For example, setting F to 40 sets C to 4.4, which sets F to 39.9.
- # The conditional here is a crude way to prevent that 39.9.
- if (isTRUE(input$temp_c == f_to_c(input$temp_f))) {
- return()
- }
- updateNumericInput(session, "temp_f",
- value = c_to_f(input$temp_c))
- })
- observeEvent(input$temp_f, {
- if (isTRUE(input$temp_f == c_to_f(input$temp_c))) {
- return()
- }
- updateNumericInput(session, "temp_c",
- value = f_to_c(input$temp_f))
- })
- }
- shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement