gpt4 book ai didi

使用 R googlesheets 包替换 googlesheet 的所有内容?

转载 作者:行者123 更新时间:2023-12-01 00:35:21 25 4
gpt4 key购买 nike

刚刚发现googlesheets打包并发现它到目前为止非常有用。我现在希望能够替换现有工作表中的全部或部分内容。

例子:

> library(googlesheets) 
> set.seed(10)
> test1 <- data.frame(matrix(rnorm(10),nrow = 5))
> test1
X1 X2
1 0.01874617 0.3897943
2 -0.18425254 -1.2080762
3 -1.37133055 -0.3636760
4 -0.59916772 -1.6266727
5 0.29454513 -0.2564784

> gs_new("foo_sheet", input = test1, trim = TRUE)

这会按预期创建一个新工作表。假设我们然后需要更新工作表(此数据用于 Shinyapps.io 托管的 Shiny 应用程序,我希望不必重新部署该应用程序以更改工作表引用)。
> test1$X2 <- NULL
> test1
X1
1 0.01874617
2 -0.18425254
3 -1.37133055
4 -0.59916772
5 0.29454513

我试图简单地用 gs_new() 覆盖但遇到以下警告消息:
> gs_new("foo_sheet", input = test1, trim = TRUE)  

Warning message:
At least one sheet matching "foo_sheet" already exists, so you may
need to identify by key, not title, in future.

这会产生一个新的工作表 foo_sheet正在创建一个新的 key ,但不会替换现有工作表,因此会生成 key如果我们尝试注册更新的工作表,则会出错
gs_title("foo_sheet")

Error in gs_lookup(., "sheet_title", verbose) :
"foo_sheet" matches sheet_title for multiple sheets returned by gs_ls() (which should reflect user's Google Sheets home screen). Suggest you identify this sheet by unique key instead.

这意味着,如果我们稍后尝试访问新工作表 foo_sheetgs_read("foo_sheet") ,API 将返回原始工作表,而不是新工作表。
> df <- gs_read("foo_sheet")
> df
X1 X2
1 0.01874617 0.3897943
2 -0.18425254 -1.2080762
3 -1.37133055 -0.3636760
4 -0.59916772 -1.6266727
5 0.29454513 -0.2564784

据我了解,一种可能的解决方案可能是首先使用 gs_delete("test1") 删除工作表。然后创建一个新的。或者,可以使用 gs_edit_cells() 清空单元格,但希望有某种形式的覆盖功能。

提前致谢!

最佳答案

我发现编辑单元格功能是一个很好的解决方法:

gs_edit_cells(ss = "foo_sheet", ws = "worksheet name", input = test1, anchor = "A1" trim = TRUE, col_names = TRUE)

通过将数据 anchor 定到左上角,您可以有效地覆盖所有其他数据。修剪功能将消除所有不更新的单元格。

关于使用 R googlesheets 包替换 googlesheet 的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41695226/

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