mikemahoney218 / mm218.dev

Code and assets for my personal website at https://mm218.dev
https://mm218.dev
Other
25 stars 14 forks source link

posts/2023-12-07-view/ #25

Open utterances-bot opened 7 months ago

utterances-bot commented 7 months ago

Mike Mahoney - Why is View() capitalized, anyway?

And down the rabbit hole we go.

https://www.mm218.dev/posts/2023-12-07-view/

njtierney commented 7 months ago

What a journey!

Something that broke my mind is that lowercase view is exported from tibble and dplyr. I had a student write some code and I remember saying, "oh wait, it needs to be capitalised, for reasons, I dunno", and they hit Enter and were like "I never capitalise it?". But then that's because they were using tidyverse stuff, so they never went through the pain!

yihui commented 6 months ago

The reason that

getOption("defaultPackages") |> 
  setdiff("datasets") |> 
  vapply(\(x) paste0("package:", x), character(1)) |> 
  ls() |> 
  strsplit("") |> 
  Filter(f = \(x) x[[1]] %in% LETTERS) |> 
  vapply(paste0, character(1), collapse = "")

didn't return all capitalized function names is that ls() is not vectorized. You passed a vector to it, but it will only use the first element, i.e., you were essentially listing functions in the utils package. To see this problem, you may run:

ls(c('package:utils', 'package:grDevices'))

and you will see the grDevices package is ignored.

This is what I'd do:

ns = lapply(getOption("defaultPackages"), getNamespace)
nm = unlist(lapply(ns, ls))
sort(grep('^[A-Z]', nm, value = TRUE))

In the current version of R, there are 500 such functions.

But it may make more sense to see exported functions only, because they are user-faced. In that case, you can do:

nm = lapply(getOption("defaultPackages"), getNamespaceExports)
sort(grep('^[A-Z]', unlist(nm), value = TRUE))

There are 85 such functions.

Anyway, I don't know why Viewer() is capitalized. Perhaps only the author knows, and I doubt anyone wants to ask him... Sometimes naming is just arbitrary. Even when a project is developed by a single person, it's not uncommon to see inconsistencies, not to mention multiple authors collaborating on a project. A style guide could help, but it could also mean compromises, which can be hard sometimes.