0
votes

I want to allow the users to see the changes in inputs to be reflected on the table directly. So as soon as the user changes numeric value for inputlower it should reflect the change in column lower_rate in the table and also multiply that value with low_val. Is this possible with observeEvent on numeric input change.

input_data <- data.frame(lower_rate = c (.5, .5, .5),
                         low_val = c(10,11,12),
                         upper_rate = c(1.5, 1.5, 1.5),
                         upp_val = c(20,21,22),
                         stringsAsFactors = FALSE) 

ui <- shinyUI(
  fluidPage(
    titlePanel("Basic DataTable"),



    # Create a new row for the table.
    fluidRow(
      column(12,

             numericInput("low", label = h3("lower"), value = 0.5),
             numericInput("up", label = h3("Upper"), value = 1.5),
             dataTableOutput(outputId="table")
      )
    )    
  )  
)

server <- shinyServer(function(input, output) {
  d <- reactive({
    input_data
  })

  dat <- reactiveValues(dat=NULL)
  observe({
    dat$dat <- d()
  })

  output$table <- renderDataTable({
    dat$dat
  })
})

shinyApp(ui=ui,server=server)```

1

1 Answers

0
votes

I believe it would be best to edit the column value inside the reactive environment renderDataTable. The observe events are not needed. As long as you don't use the <<- notation to write to environment, this wont change the original data.

library(shiny)
library(data.table)

input_data <- data.frame(lower_rate = c(.5, .5, .5),
                         low_val = c(10,11,12),
                         upper_rate = c(1.5, 1.5, 1.5),
                         upp_val = c(20,21,22),
                         stringsAsFactors = FALSE) 

ui <- shinyUI(
  fluidPage(
    titlePanel("Basic DataTable"),



    # Create a new row for the table.
    fluidRow(
      column(12,

             numericInput("low", label = h3("lower"), value = 0.5),
             numericInput("up", label = h3("Upper"), value = 1.5),
             dataTableOutput(outputId="table")
      )
    )    
  )  
)

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

  output$table <- renderDataTable({
    input_data$lower_rate <- input$low
    #it is not clear where you want the multiplied value to end up
    input_data$new_val <-  input$low*input_data$low_val
    data.table(input_data)
  })
})

shinyApp(ui=ui,server=server)