Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cell_limit to vector of cell references #30

Open
alephreish opened this issue Oct 24, 2017 · 4 comments
Open

cell_limit to vector of cell references #30

alephreish opened this issue Oct 24, 2017 · 4 comments
Milestone

Comments

@alephreish
Copy link

alephreish commented Oct 24, 2017

What is the recommended way of obtaining a vector of individual cell references from a cell_limit object? When dealing with columns < AA (which is my case since I'm actually parsing coordinates on A1:H12 PCR plates), one can do:

cl <- as.cell_limits("R1C1:R1C9")
eg <- expand.grid(LETTERS[cl$ul[2]:cl$lr[2]], cl$ul[1]:cl$lr[1])
paste0(eg[[1]], eg[[2]])
# [1] "A1" "B1" "C1" "D1" "E1" "F1" "G1" "H1" "I1"

Is there a lightweight generic solution?

@nacnudus
Copy link
Contributor

I did something similar recently, using cellranger::to_string() to handle columns > AA, and would have made a PR for unrange(), but for two things:

  • How to handle the 'whole column' and 'whole row' case, e.g. A:A and 4:4.
  • A dplyr-like set of join functions might meet the use-case better, i.e. a kind of spatial join of cells to cells, cells to ranges, ranges to cells, and ranges to ranges.

Anyway, this was the code.

unrange <- function(x) {
  limits <- cellranger::as.cell_limits(x)
  rows <- seq(limits$ul[1], limits$lr[1])
  cols <- seq(limits$ul[2], limits$lr[2])
  rowcol <- expand.grid(rows, cols)
  cell_addrs <- cellranger::cell_addr(rowcol[[1]], rowcol[[2]])
  cellranger::to_string(cell_addrs, fo = "A1", strict = FALSE)
}

unrange("A121:A122")
#> [1] "A121" "A122"

@alephreish
Copy link
Author

Looks nice despite the shortcomings. Do make a PR!

@jennybc
Copy link
Member

jennybc commented Oct 24, 2017

I note you can also do this:

reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-10-24

library(magrittr)
library(cellranger)

cell_addr(1, 1:9) %>% 
  to_string(fo = "A1", strict = FALSE)
#> [1] "A1" "B1" "C1" "D1" "E1" "F1" "G1" "H1" "I1"

I haven't looked at the package in a long time! The cell_addr class (and the ra_ref class) are from an unfinished effort to setup some classes useful for formula parsing down the road. It's all a bit foggy in my mind right now.

But I agree, it seems like there's some missing piece here that would be useful.

@nacnudus
Copy link
Contributor

That's neat. It works on single-row or single-column ranges, but for rectangles expand.grid() is still needed.

library(magrittr)
library(cellranger)

cell_addr(1:2, 1:9) %>%
  to_string(fo = "A1", strict = FALSE)
#> Error: length(row) == length(col) is not TRUE

@jennybc if you get a chance to revisit those ideas, I'd love to hear more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants