R object browser for ESS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
8.7KB

  1. ---
  2. title: R object browser functions
  3. ---
  4. # Helpers
  5. ## Get object
  6. Get an object from its full qualifying name (*fullname*).
  7. ```{r get object}
  8. ##' Get an object
  9. ##'
  10. ##' Get an object from its fullname
  11. ##' @title Get an object
  12. ##' @param fullname A string with the fullname of an object
  13. ##' @return The object
  14. get_object <- function(fullname)
  15. eval(parse(text = fullname))
  16. ```
  17. ## Get name
  18. Get the printing name of an object given its identifier.
  19. Identifiers come in **quoted** if the object is named, or as an **index** number.
  20. The printing name is unquoted or surrounded with double brackets if an index.
  21. ```{r get name}
  22. ##' Get an object name
  23. ##'
  24. ##' Return the printing name of an object as given to print_object
  25. ##' @title
  26. ##' @param identifier An object identifier string
  27. ##' @return The name of the object or its index in brackets
  28. get_name <- function(identifier)
  29. {
  30. if (substr(identifier, 1, 1) == "\"")
  31. substr(identifier, 2, nchar(identifier) - 1)
  32. else
  33. paste0("[[", identifier, "]]")
  34. }
  35. ```
  36. ## Get fullname
  37. Get the fullname of an object given its identifier and fullname of the parent
  38. ```{r get fullname}
  39. ##' Get an object fullname
  40. ##'
  41. ##' Get the fullname of an object
  42. ##' by prepending its parent and indexing by name/position
  43. ##' @title Get fullname
  44. ##' @param identifier An object identifier string
  45. ##' @param parent A string with the name of the parent
  46. ##' @return The full object name
  47. get_fullname <- function(identifier, parent)
  48. {
  49. if (substr(parent, nchar(parent) - 1, nchar(parent)) == "::") # If part of a package
  50. fullname <- paste0(parent, gsub("\"", "`", identifier)) # replace quotes with backticks and prepend the package
  51. else
  52. fullname <- paste0(parent, "[[", identifier, "]]") # else index the parent by name/position with double brackets
  53. tryCatch(get_object(fullname), # If the object is not exported, use three colons
  54. error = function(e) fullname <<- gsub("::", ":::", fullname))
  55. fullname
  56. }
  57. ```
  58. ## Get type
  59. Get the type / class / mode of an object given its fullname.
  60. ```{r get type}
  61. ##' Get type of an object
  62. ##'
  63. ##' Get the type of an object
  64. ##' @title Get object type
  65. ##' @param fullname A string with the fullname of an object
  66. ##' @return The type of the object
  67. get_type <- function(fullname)
  68. class(get_object(fullname))[[1]] # Get the first class of the
  69. # object. This works for matrices, but
  70. # not for tibbles…
  71. ```
  72. ## Get size
  73. Get the size of an object given its fullname.
  74. - environments, pkg, list : length
  75. - dataframe, matrix : ncol × nrow
  76. - function : args
  77. - default : length
  78. ```{r get size}
  79. ##' Get an object size
  80. ##'
  81. ##' Get the size of an object:
  82. ##' - list : length
  83. ##' - dataframe : nrow × length
  84. ##' -
  85. ##' @title
  86. ##' @param fullname A string with the fullname of an object
  87. ##' @param type The type of the object
  88. ##' @return The size of the object
  89. get_size <- function(fullname, type)
  90. {
  91. object <- get_object(fullname)
  92. switch(type,
  93. "environment" = length(objects(object)),
  94. "pkg" = length(objects(object)),
  95. "data.frame" = paste0(ncol(object), " × ", nrow(object)),
  96. "tbl_df" = paste0(ncol(object), " × ", nrow(object)),
  97. "matrix" = paste0(ncol(object), " × ", nrow(object)),
  98. "function" = length(formals(object)),
  99. length(object))
  100. }
  101. ```
  102. # Printers
  103. These functions return the line to print in the emacs buffer for different objects.
  104. ## Package
  105. Print a package:
  106. **package name**|pkg| **package name**
  107. ```{r print package}
  108. ##' Print a package
  109. ##'
  110. ##' Display the fullname of the package,
  111. ##' the type, and the name with the size.
  112. ##' @title Print a package
  113. ##' @param pkg The package to print
  114. ##' @return The line to print to the buffer
  115. print_package <- function(pkg)
  116. paste0(pkg, "|pkg| ", pkg)
  117. ```
  118. ## searchlist
  119. Print a searchlist:
  120. **environment name**|env| **environment name** (*number of objects in the environment*)
  121. **Could be replaced by print_object**
  122. ```{r print searchlist}
  123. ##' Print an environment
  124. ##'
  125. ##' Display the fullname of the environment,
  126. ##' the type, and the name with the size.
  127. ##' @title Print an environment
  128. ##' @param env The environment to print
  129. ##' @return The line to print to the buffer
  130. print_searchlist <- function(env)
  131. paste0(env, "|environment| ", env, " (", length(objects(env)), ")")
  132. ```
  133. ## Objects
  134. Print other objects:
  135. **object full qualifying nume**|*type*| **object name** (*number of objects in the container, dimensions for df and matrices, number of args in a function*)
  136. ```{r print object}
  137. ##' Print an object
  138. ##'
  139. ##' Display the fullname of an object,
  140. ##' the type, and the name with the size
  141. ##'
  142. ##' If the parent is "" then it is a base name,
  143. ##' else we construct the fullname by subsetting by name/position
  144. ##' Thus the caller needs to build the object name accordingly.
  145. ##' @title Print an object
  146. ##' @param identifier The identifier of the object to print
  147. ##' @param parent The parent object
  148. ##' @return The line to print to the buffer
  149. print_object <- function(identifier, parent = "")
  150. {
  151. full_name <- get_fullname(identifier, parent)
  152. type <- get_type(full_name)
  153. paste0(full_name,
  154. "|",
  155. type,
  156. "| ",
  157. get_name(identifier),
  158. " (",
  159. get_size(full_name, type),
  160. ")")
  161. }
  162. ```
  163. # List objects
  164. List different objects and their contents.
  165. ## Packages
  166. ### List packages
  167. List all installed packages.
  168. ```{r list packages}
  169. ##' List packages
  170. ##'
  171. ##' List all the installed packages
  172. ##' @title List packages
  173. ##' @return A character vector of packages
  174. list_packages <- function()
  175. sapply(row.names(installed.packages()),
  176. print_package,
  177. USE.NAMES = F,
  178. simplify = T)
  179. ```
  180. ### List package contents
  181. Print all the objects in a package.
  182. Parent is *package::*
  183. The names are quoted.
  184. The generic print_object function is used.
  185. Pourrait être remplacé par list_container_contents du même trait que pour list_searchlist_contents
  186. ```{r list package contents}
  187. ##' List package contents
  188. ##'
  189. ##' .. content for \details{} ..
  190. ##' @title List package contents
  191. ##' @param package Name of the package as a string ("dplyr")
  192. ##' @return char vector of contents of package
  193. list_package_contents <- function(package)
  194. {
  195. parent = paste0(package, "::")
  196. objects <- paste0("\"", objects(asNamespace(package)), "\"")
  197. mapply(print_object,
  198. objects,
  199. parent,
  200. USE.NAMES = F,
  201. SIMPLIFY = T)
  202. }
  203. ```
  204. ## searchlist
  205. ### searchlist
  206. List all objects on the search list.
  207. print_searchlist could be replaced by print_object
  208. ```{r list searchlist}
  209. ##' List searchlist
  210. ##'
  211. ##' List environments as returned by `search()`,
  212. ##' but ommiting `ESSR` from ESS, and the `Autoloads`,
  213. ##' as they contain only hidden objects and have length=0
  214. ##' @title List searchlist
  215. ##' @return A character vector of environments
  216. list_searchlist <- function()
  217. {
  218. sapply(setdiff(search(), c("ESSR", "Autoloads")),
  219. print_searchlist,
  220. USE.NAMES = F,
  221. simplify = T)
  222. }
  223. ```
  224. ### List searchlist contents
  225. List the contents of the searchlist.
  226. Pourrait être remplacé par list_container_contents
  227. Si le parsing de "package:" est fait dans get_object et fix_names ?
  228. ```{r list searchlist contents}
  229. ##' List environment contents
  230. ##'
  231. ##' List environement contents.
  232. ##' Works with ".GlobalEnv", and "package:stats"
  233. ##' @title List environment contents
  234. ##' @name Name of the environment as character
  235. ##' @return A character vector of objects in the environment
  236. list_searchlist_contents <- function(env)
  237. {
  238. if (substring(env, 1, 8) == "package:")
  239. parent <- paste0(substring(env, 9), "::")
  240. else
  241. parent <- env
  242. objects <- paste0("\"", objects(env), "\"")
  243. mapply(print_object,
  244. objects,
  245. parent,
  246. USE.NAMES = F,
  247. SIMPLIFY = T)
  248. }
  249. ```
  250. ## Containers
  251. ### Fix names
  252. Fix the names given by `ls` and create identifiers.
  253. If a name exists, surround it with quotes.
  254. If a name doesn't exist, replace it by its index.
  255. ```{r fix names}
  256. fix_names <- function(object)
  257. {
  258. indexes <- 1:length(object)
  259. names_out <- names(object)
  260. names_out[(names(object) == "") | is.na(names(object))] <- indexes[(names(object) == "") | is.na(names(object))]
  261. names_out[!(names(object) == "") | is.na(names(object))] <- paste0("\"", names_out[!(names(object) == "") | is.na(names(object))], "\"")
  262. names_out
  263. }
  264. ```
  265. ### container
  266. ```{r list container contents}
  267. list_container_contents <- function(fullname)
  268. {
  269. mapply(print_object,
  270. fix_names(get_object(fullname)),
  271. fullname,
  272. USE.NAMES = F,
  273. SIMPLIFY = T)
  274. }
  275. ```