可编辑的renderTable和保存值 - R Shiny

时间:2017-08-01 18:49:22

标签: r shiny

我有一个代码(类似于另一篇文章)Insert a numeric input for each row - R Shiny,它使用数据框中的现有值生成可编辑的renderTable。现在,我想在编辑值后将更新的值保存到同一数据框中。我该怎么做?

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)

}
))

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

我可以在那里找到你的一半。您需要使用静态值解析表的动态输入并创建一个新表 - 然后将新表(或数组)传递给Shiny。此外,可能有一种比点击表格以获得响应更好的方法。

无论如何,这里是:

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 })
  }

))