1

I have a code (similar to another post) Insert a numeric input for each row - R Shiny that generates editable renderTable with existing values from a data frame. Now I want to save the updated values into the same data frame after editing the values. How do I do that?

shiny::runApp(list(
ui = basicPage(
tableOutput("My_table")
),
server = function(input, output, session) {

My_table = matrix( 
  c(1:100), 
  nrow=20, 
  ncol=5)

output$My_table <- renderTable({
  input1 <- paste0("<input id='a", 1:nrow(My_table), "'"," value='",My_table[,1],"'" ," class='shiny-bound-input' type='character' style='width: 50px;'>")
  input2 <- paste0("<input id='b", 1:nrow(My_table), "'"," value='",My_table[,5],"'" ," class='shiny-bound-input' type='number' style='width: 50px;'>")
  cbind(input1, My_table[,c(2,3,4)], input2)
}, sanitize.text.function = function(x) x)

}
))

Any help would be appreciated. Thanks.

Jay
  • 1,181
  • 1
  • 9
  • 10

1 Answers1

0

I can get you about half way there. You'll need to parse the table's dynamic inputs with the static values and create a new table - then pass the new table (or array) out to Shiny. Also, there's probably a better method than clicking on the table to get the response.

Anyway, here goes:

shiny::runApp(list(
  ui = basicPage(
    # a div named mydiv

    tableOutput("myTable"),
    # javascript code to send data to shiny server
    tags$script('
    document.getElementById("myTable").onclick = function() {
                 var value = $("table tr").value();
                 Shiny.onInputChange("mydata", value);
                 };
                 '),
    tableOutput("results")
  ),
  server = function(input, output, session) {

    My_table = matrix( 
      c(1:100), 
      nrow=20, 
      ncol=5)

    output$myTable <- renderTable({
      input1 <- paste0("<input id='a", 1:nrow(My_table), "'"," value='",My_table[,1],"'" ," class='shiny-bound-input' type='character' style='width: 50px;'>")
      input2 <- paste0("<input id='b", 1:nrow(My_table), "'"," value='",My_table[,5],"'" ," class='shiny-bound-input' type='number' style='width: 50px;'>")
      cbind(input1, My_table[,c(2,3,4)], input2)
    }, sanitize.text.function = function(x) x)

    output$results = renderTable({
      input$mydata })
  }

))
Ryan Morton
  • 2,605
  • 1
  • 16
  • 19