使用 googlesheets 包提取 Google Sheet 的名称
Extract name of Google Sheet with googlesheets package
我正在使用 googlesheets
包从 Google Sheet 中获取数据,并希望将 sheet 名称作为列附加到 data.frame.
例如,使用 public Gapminder sheet:
library(googlesheets)
library(dplyr)
my_dat <- gs_gap() %>% # register public Gapminder practice sheet
gs_read(ws = 1) %>%
mutate(source_dat = "Africa")
我的最后一行是手动执行的,但我想自动执行,即用 sheet 的名称填充变量 source_dat
。 (是的,我知道已经有一个包含该信息的专栏 - 这只是一个示例)。有没有办法引用活动的 sheet 名称?
目前只有单元格提要阅读功能将工作表名称附加为属性。第二个解决方案展示了如何使用它。如果您在 github 上提出问题,我可以将其添加到所有读取方法中。
也可以像方案一一样,预先存好ws的名字
library(googlesheets)
library(dplyr)
ws <- "Africa"
my_dat <- gs_gap() %>% # register public Gapminder practice sheet
gs_read(ws = ws) %>%
mutate(source_dat = ws)
#> Accessing worksheet titled 'Africa'.
#> No encoding supplied: defaulting to UTF-8.
my_dat
#> Source: local data frame [624 x 7]
#>
#> country continent year lifeExp pop gdpPercap source_dat
#> (chr) (chr) (int) (dbl) (int) (dbl) (chr)
#> 1 Algeria Africa 1952 43.077 9279525 2449.008 Africa
#> 2 Algeria Africa 1957 45.685 10270856 3013.976 Africa
#> 3 Algeria Africa 1962 48.303 11000948 2550.817 Africa
#> 4 Algeria Africa 1967 51.407 12760499 3246.992 Africa
#> 5 Algeria Africa 1972 54.518 14760787 4182.664 Africa
#> 6 Algeria Africa 1977 58.014 17152804 4910.417 Africa
#> 7 Algeria Africa 1982 61.368 20033753 5745.160 Africa
#> 8 Algeria Africa 1987 65.799 23254956 5681.359 Africa
#> 9 Algeria Africa 1992 67.744 26298373 5023.217 Africa
#> 10 Algeria Africa 1997 69.152 29072015 4797.295 Africa
#> .. ... ... ... ... ... ... ...
## currently only gs_read_cellfeed stores ws_title as an attribute
my_dat <- gs_gap() %>% # register public Gapminder practice sheet
gs_read_cellfeed() %>%
{
ws <- attr(., "ws_title")
gs_reshape_cellfeed(.) %>%
mutate(source_dat = ws)
}
#> Accessing worksheet titled 'Africa'.
my_dat
#> Source: local data frame [624 x 7]
#>
#> country continent year lifeExp pop gdpPercap source_dat
#> (chr) (chr) (int) (dbl) (int) (dbl) (chr)
#> 1 Algeria Africa 1952 43.077 9279525 2449.008 Africa
#> 2 Algeria Africa 1957 45.685 10270856 3013.976 Africa
#> 3 Algeria Africa 1962 48.303 11000948 2550.817 Africa
#> 4 Algeria Africa 1967 51.407 12760499 3246.992 Africa
#> 5 Algeria Africa 1972 54.518 14760787 4182.664 Africa
#> 6 Algeria Africa 1977 58.014 17152804 4910.417 Africa
#> 7 Algeria Africa 1982 61.368 20033753 5745.160 Africa
#> 8 Algeria Africa 1987 65.799 23254956 5681.359 Africa
#> 9 Algeria Africa 1992 67.744 26298373 5023.217 Africa
#> 10 Algeria Africa 1997 69.152 29072015 4797.295 Africa
#> .. ... ... ... ... ... ... ...
我正在使用 googlesheets
包从 Google Sheet 中获取数据,并希望将 sheet 名称作为列附加到 data.frame.
例如,使用 public Gapminder sheet:
library(googlesheets)
library(dplyr)
my_dat <- gs_gap() %>% # register public Gapminder practice sheet
gs_read(ws = 1) %>%
mutate(source_dat = "Africa")
我的最后一行是手动执行的,但我想自动执行,即用 sheet 的名称填充变量 source_dat
。 (是的,我知道已经有一个包含该信息的专栏 - 这只是一个示例)。有没有办法引用活动的 sheet 名称?
目前只有单元格提要阅读功能将工作表名称附加为属性。第二个解决方案展示了如何使用它。如果您在 github 上提出问题,我可以将其添加到所有读取方法中。
也可以像方案一一样,预先存好ws的名字
library(googlesheets)
library(dplyr)
ws <- "Africa"
my_dat <- gs_gap() %>% # register public Gapminder practice sheet
gs_read(ws = ws) %>%
mutate(source_dat = ws)
#> Accessing worksheet titled 'Africa'.
#> No encoding supplied: defaulting to UTF-8.
my_dat
#> Source: local data frame [624 x 7]
#>
#> country continent year lifeExp pop gdpPercap source_dat
#> (chr) (chr) (int) (dbl) (int) (dbl) (chr)
#> 1 Algeria Africa 1952 43.077 9279525 2449.008 Africa
#> 2 Algeria Africa 1957 45.685 10270856 3013.976 Africa
#> 3 Algeria Africa 1962 48.303 11000948 2550.817 Africa
#> 4 Algeria Africa 1967 51.407 12760499 3246.992 Africa
#> 5 Algeria Africa 1972 54.518 14760787 4182.664 Africa
#> 6 Algeria Africa 1977 58.014 17152804 4910.417 Africa
#> 7 Algeria Africa 1982 61.368 20033753 5745.160 Africa
#> 8 Algeria Africa 1987 65.799 23254956 5681.359 Africa
#> 9 Algeria Africa 1992 67.744 26298373 5023.217 Africa
#> 10 Algeria Africa 1997 69.152 29072015 4797.295 Africa
#> .. ... ... ... ... ... ... ...
## currently only gs_read_cellfeed stores ws_title as an attribute
my_dat <- gs_gap() %>% # register public Gapminder practice sheet
gs_read_cellfeed() %>%
{
ws <- attr(., "ws_title")
gs_reshape_cellfeed(.) %>%
mutate(source_dat = ws)
}
#> Accessing worksheet titled 'Africa'.
my_dat
#> Source: local data frame [624 x 7]
#>
#> country continent year lifeExp pop gdpPercap source_dat
#> (chr) (chr) (int) (dbl) (int) (dbl) (chr)
#> 1 Algeria Africa 1952 43.077 9279525 2449.008 Africa
#> 2 Algeria Africa 1957 45.685 10270856 3013.976 Africa
#> 3 Algeria Africa 1962 48.303 11000948 2550.817 Africa
#> 4 Algeria Africa 1967 51.407 12760499 3246.992 Africa
#> 5 Algeria Africa 1972 54.518 14760787 4182.664 Africa
#> 6 Algeria Africa 1977 58.014 17152804 4910.417 Africa
#> 7 Algeria Africa 1982 61.368 20033753 5745.160 Africa
#> 8 Algeria Africa 1987 65.799 23254956 5681.359 Africa
#> 9 Algeria Africa 1992 67.744 26298373 5023.217 Africa
#> 10 Algeria Africa 1997 69.152 29072015 4797.295 Africa
#> .. ... ... ... ... ... ... ...