当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R readr with_edition 暂时更改活动阅读器版本


with_edition() 允许您更改给定代码块的 readr 的活动版本。 local_edition() 允许您更改 readr 的活动版本,直到当前函数或文件结束。

用法

with_edition(edition, code)

local_edition(edition, env = parent.frame())

参数

edition

应为单个整数,例如 12

code

使用更改后的版本运行的代码。

env

控制变更范围的环境。仅供专家使用。

例子

with_edition(1, edition_get())
#> [1] 1
with_edition(2, edition_get())
#> [1] 2

# readr 1e and 2e behave differently when input rows have different number
# number of fields
with_edition(1, read_csv("1,2\n3,4,5", col_names = c("X", "Y", "Z")))
#> Warning: 1 parsing failure.
#> row col  expected    actual         file
#>   1  -- 3 columns 2 columns literal data
#> # A tibble: 2 × 3
#>       X     Y     Z
#>   <dbl> <dbl> <dbl>
#> 1     1     2    NA
#> 2     3     4     5
with_edition(2, read_csv("1,2\n3,4,5", col_names = c("X", "Y", "Z")))
#> Warning: One or more parsing issues, call `problems()` on your data frame for
#> details, e.g.:
#>   dat <- vroom(...)
#>   problems(dat)
#> Rows: 2 Columns: 2
#> ── Column specification ──────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (1): X
#> num (1): Y
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> # A tibble: 2 × 2
#>       X     Y
#>   <dbl> <dbl>
#> 1     1     2
#> 2     3    45

# local_edition() applies in a specific scope, for example, inside a function
read_csv_1e <- function(...) {
  local_edition(1)
  read_csv(...)
}
read_csv("1,2\n3,4,5", col_names = c("X", "Y", "Z"))      # 2e behaviour
#> Warning: One or more parsing issues, call `problems()` on your data frame for
#> details, e.g.:
#>   dat <- vroom(...)
#>   problems(dat)
#> Rows: 2 Columns: 2
#> ── Column specification ──────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (1): X
#> num (1): Y
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> # A tibble: 2 × 2
#>       X     Y
#>   <dbl> <dbl>
#> 1     1     2
#> 2     3    45
read_csv_1e("1,2\n3,4,5", col_names = c("X", "Y", "Z"))   # 1e behaviour
#> Warning: 1 parsing failure.
#> row col  expected    actual         file
#>   1  -- 3 columns 2 columns literal data
#> # A tibble: 2 × 3
#>       X     Y     Z
#>   <dbl> <dbl> <dbl>
#> 1     1     2    NA
#> 2     3     4     5
read_csv("1,2\n3,4,5", col_names = c("X", "Y", "Z"))      # 2e behaviour
#> Warning: One or more parsing issues, call `problems()` on your data frame for
#> details, e.g.:
#>   dat <- vroom(...)
#>   problems(dat)
#> Rows: 2 Columns: 2
#> ── Column specification ──────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (1): X
#> num (1): Y
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> # A tibble: 2 × 2
#>       X     Y
#>   <dbl> <dbl>
#> 1     1     2
#> 2     3    45
源代码:R/edition.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Temporarily change the active readr edition。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。