gpt4 book ai didi

r - 如何保存使用 rhandsontable r 包所做的编辑

转载 作者:行者123 更新时间:2023-12-03 14:40:09 25 4
gpt4 key购买 nike

我的 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)

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

最佳答案

我知道这个线程已经死了很多年了,但这是关于这个问题的第一个 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因为我以前在保存对全局环境的更改时遇到了问题。不过,我想不出任何其他方式。

关于r - 如何保存使用 rhandsontable r 包所做的编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35834322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com