Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. library(shiny)
  2.  
  3. ui <- fluidPage(
  4. numericInput("temp_c", "Celsius", NA),
  5. numericInput("temp_f", "Fahrenheit", NA)
  6. )
  7.  
  8. server <- function(input, output, session) {
  9. c_to_f <- function(c, decimals = 1) {
  10. round((c * 9 / 5) + 32, decimals)
  11. }
  12.  
  13. f_to_c <- function(f, decimals = 1) {
  14. round((f - 32) * 5 / 9, decimals)
  15. }
  16.  
  17. observeEvent(input$temp_c, {
  18. # This early return is to prevent event handler cycles.
  19. # For example, setting F to 40 sets C to 4.4, which sets F to 39.9.
  20. # The conditional here is a crude way to prevent that 39.9.
  21. if (isTRUE(input$temp_c == f_to_c(input$temp_f))) {
  22. return()
  23. }
  24. updateNumericInput(session, "temp_f",
  25. value = c_to_f(input$temp_c))
  26. })
  27.  
  28. observeEvent(input$temp_f, {
  29. if (isTRUE(input$temp_f == c_to_f(input$temp_c))) {
  30. return()
  31. }
  32. updateNumericInput(session, "temp_c",
  33. value = f_to_c(input$temp_f))
  34. })
  35. }
  36.  
  37. shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement