如何保存使用rhandsontable r包进行的编辑

问题描述 投票:2回答:2

我的R程序按预期工作。它显示了一个包含我的dataFrame的表,并允许我编辑这些值。

如何捕获这些值并将其保存到我的数据帧或我的数据帧的副本?

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)

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

r dataframe shiny handsontable
2个回答
0
投票

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

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

0
投票

我知道这个帖子已经死了很多年了,但它是这个问题的第一个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 <<- values$hot的解决方案,因为我以前遇到了保存全局环境变化的问题。不过,我无法用任何其他方式解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.