forked from ThinkR-open/thinkr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'BenjaminLouis-issue14-test-excel-names'
- Loading branch information
Showing
11 changed files
with
145 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ | |
^codecov\.yml$ | ||
^\.github$ | ||
^pkgdown$ | ||
^dev$ | ||
^thinkr\.Rproj$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,5 @@ vignettes/*.pdf | |
.Rproj.user | ||
pkgdown | ||
inst/doc | ||
# Working with fusen locally | ||
dev |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#' Get position or excel name of column | ||
#' | ||
#' \code{ncol_to_excel} returns excel column name from a position number. \code{excel_to_ncol} returns excel column position number from a column name. \code{excel_col} returns all excel column name. | ||
#' | ||
#' @param n the column position | ||
#' @param col_name the culumn name | ||
#' | ||
#' @importFrom assertthat assert_that | ||
#' @importFrom tidyr unite | ||
#' | ||
#' @name excel_names | ||
NULL | ||
|
||
|
||
#' return excel column name from a position number | ||
#' | ||
#' @rdname excel_names | ||
#' | ||
#' @export | ||
#' @examples | ||
#' ncol_to_excel(35) | ||
#' excel_to_ncol("BF") | ||
#' excel_col() | ||
#' ncol_to_excel(1:6) | ||
#' excel_to_ncol(c('AF', 'AG', 'AH')) | ||
ncol_to_excel<-function(n){ | ||
assert_that(all(is.numeric(n))) | ||
assert_that(all(round(n) == n)) | ||
excel_col()[n] | ||
} | ||
|
||
|
||
|
||
#' @rdname excel_names | ||
#' @export | ||
excel_to_ncol<-function(col_name){ | ||
assert_that(all(is.character(col_name))) | ||
assert_that(all(nchar(col_name) > 1 & nchar(col_name) <= 2)) | ||
assert_that(all(unlist(strsplit(col_name, split = "")) %in% LETTERS)) | ||
which(excel_col() %in% col_name) | ||
} | ||
|
||
|
||
#' @rdname excel_names | ||
#' @export | ||
excel_col <- function(){ | ||
# require(dplyr) | ||
c(LETTERS, | ||
expand.grid(LETTERS,LETTERS)[2:1] %>% tidyr::unite(Var2,Var1,col="col",sep = "") %>% unlist() %>% unname()) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
test_that("ncol_to_excel works properly", { | ||
expect_equal(ncol_to_excel(35), "AI") | ||
expect_equal(ncol_to_excel(1:3), LETTERS[1:3]) | ||
expect_error(ncol_to_excel(5.2)) | ||
expect_error(ncol_to_excel("not_a_number")) | ||
expect_error(ncol_to_excel(TRUE)) | ||
}) | ||
|
||
test_that("excel_to_ncol works properly", { | ||
expect_equal(excel_to_ncol("BF"), 58) | ||
expect_equal(excel_to_ncol(c("BF", "BG", "BH")), 58:60) | ||
expect_error(excel_to_ncol(5)) | ||
expect_error(excel_to_ncol(c("a", "B", "C"))) | ||
expect_error(excel_to_ncol(TRUE)) | ||
}) | ||
test_that("excel_col works properly", { | ||
expect_equal(excel_col()[1], "A") | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: "Functions related to Excel" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{Functions related to Excel} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
```{r setup} | ||
library(thinkr) | ||
``` | ||
|
||
|
||
# `excel_names` : dealing with column names and position number | ||
|
||
- `ncol_to_excel()` returns Excel column name from a position number. | ||
- `excel_to_ncol()` returns Excel column position number from a column name. | ||
- `excel_col()` returns all Excel column name. | ||
|
||
|
||
```{r examples-1} | ||
ncol_to_excel(35) | ||
excel_to_ncol("BF") | ||
excel_col() | ||
ncol_to_excel(1:6) | ||
excel_to_ncol(c('AF', 'AG', 'AH')) | ||
``` | ||
|
||
|