Skip to content

Commit

Permalink
Merge branch 'BenjaminLouis-issue14-test-excel-names'
Browse files Browse the repository at this point in the history
  • Loading branch information
statnmap committed Jul 14, 2021
2 parents 69304df + 650a76c commit 13d65aa
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 93 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
^codecov\.yml$
^\.github$
^pkgdown$
^dev$
^thinkr\.Rproj$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ vignettes/*.pdf
.Rproj.user
pkgdown
inst/doc
# Working with fusen locally
dev
43 changes: 0 additions & 43 deletions R/excel_names.R

This file was deleted.

53 changes: 53 additions & 0 deletions R/ncol_to_excel.R
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())
}



2 changes: 2 additions & 0 deletions devstuff_history.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ usethis::use_test(name = "replace_pattern")
usethis::use_test("is_likert")
devtools::test()

# PR
usethis::pr_fetch(15)

# Development
devtools::check()
16 changes: 0 additions & 16 deletions man/excel_col.Rd

This file was deleted.

30 changes: 30 additions & 0 deletions man/excel_names.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 0 additions & 17 deletions man/excel_to_ncol.Rd

This file was deleted.

17 changes: 0 additions & 17 deletions man/ncol_to_excel.Rd

This file was deleted.

19 changes: 19 additions & 0 deletions tests/testthat/test-ncol_to_excel.R
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")
})

37 changes: 37 additions & 0 deletions vignettes/Using-excel_names-functions.Rmd
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'))
```


0 comments on commit 13d65aa

Please sign in to comment.