【问题标题】:How to save edits made using rhandsontable r package如何保存使用 rhandsontable r 包所做的编辑
【发布时间】:2016-06-20 10:44:10
【问题描述】:

我的 R 程序按预期工作。它显示了一个包含我的数据框的表格,并让我编辑这些值。

如何捕获这些值并将它们保存到我的数据框或我的数据框的副本中?

require(shiny)
library(rhandsontable)

    DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10],
                    small = letters[1:10],
                    dt = seq(from = Sys.Date(), by = "days", length.out = 10),
                    stringsAsFactors = F)

    rhandsontable(DF, rowHeaders = NULL)

编辑: 上面的代码生成一个包含行和列的表。我可以编辑任何行和列。但是当我查看我的数据框时,这些编辑不会出现。我想弄清楚的是我需要更改什么,以便捕获已编辑的新值。

【问题讨论】:

  • 在闪亮的上下文中使用它。
  • 非常抱歉,我的剪切/粘贴省略了设置 Shiny 上下文的代码的第一行。我已经更正了。
  • 那么我该如何编写一个响应式小部件呢?您可以提供任何见解,或者您可以指出我的资源吗?
  • 我并不是要给人一种冒犯或严厉的印象,但这对谷歌来说是真的直截了当的事情,甚至是“SO”(不确定搜索的简写是什么) stackoverflow 是):stackoverflow.com/questions/27827962/…
  • 我不认为你是冒犯或严厉的,但我花了过去 6 个小时试图找到答案。我知道如何使用搜索引擎(实际上我以前写过一个),但我仍然找不到任何可以帮助我回答这个问题的东西。您提供的参考是针对滑块的,但经过研究后,我仍然无法弄清楚如何执行我在原始帖子中列出的内容。如果有帮助,我可以发布我探索过但未能找到答案的 50 多个链接。你是对的,这对谷歌来说是一件非常简单的事情,但我仍然无法找到答案!

标签: r dataframe shiny handsontable


【解决方案1】:

我不知道你想要恢复什么,但这似乎有效:

DF <- rhandsontable(DF, rowHeaders = NULL)
library(jsonlite)
fromJSON(DF$x$data)

【讨论】:

  • 你的建议不起作用,可能是因为我不清楚。我将编辑我的问题。
【解决方案2】:

我知道这个线程已经死了好几年了,但它是 StackOverflow 关于这个问题的第一个结果。

在这篇文章的帮助下 - https://cxbonilla.github.io/2017-03-04-rhot-csv-edit/,我想出了这个:

library(shiny)
library(rhandsontable)

values <- list() 

setHot <- function(x) 
  values[["hot"]] <<- x 

DF <- data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10],
                small = letters[1:10],
                dt = seq(from = Sys.Date(), by = "days", length.out = 10),
                stringsAsFactors = FALSE)


ui <- fluidPage(
  rHandsontableOutput("hot"),
  br(),
  actionButton("saveBtn", "Save changes")
)

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

  observe({
    input$saveBtn # update dataframe file each time the button is pressed
    if (!is.null(values[["hot"]])) { # if there's a table input
      DF <<- values$hot
    }
  })

  observe({
    if (!is.null(input$hot)){
      DF <- (hot_to_r(input$hot))
      setHot(DF)
    } 
  })


  output$hot <- renderRHandsontable({ 
    rhandsontable(DF) %>% # actual rhandsontable object
      hot_table(highlightCol = TRUE, highlightRow = TRUE, readOnly = TRUE) %>%
      hot_col("big", readOnly = FALSE) %>%
      hot_col("small", readOnly = FALSE)
  })

}

shinyApp(ui = ui, server = server)

但是,我不喜欢 DF &lt;&lt;- values$hot 的解决方案,因为我之前在保存对全局环境的更改时遇到了问题。不过,我想不出任何其他方式。

【讨论】:

    【解决方案3】:

    我能够通过一个更简单的解决方案来实现这一点

    根据在应用打开时单击的保存按钮创建观察事件。我已经在更复杂的应用程序中扩展了这种方法,您有一个 selectizeinput 用于将不同的数据帧换入和换出到 rhandsontable,每个数据帧都在应用程序打开时进行编辑、保存和调用。

    在服务器中:

       observeEvent(input$save, { #button is the name of the save button, change as needed
        df <<- hot_to_r(input$rhandsontable) #replace rhandsontable with the name of your own
        }) #df is the data frame that have it access when the app starts
    

    在用户界面中:

    actionButton("save","Save Edits")
    

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 2012-08-28
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 2022-11-03
      相关资源
      最近更新 更多