SHARE
TWEET

Untitled

a guest Oct 10th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Global env:
  2.  
  3. square <- function(x) {
  4.   x ^ 2
  5. }
  6.  
  7. const <- 2
  8.  
  9. nonassigned <- function(p) {
  10.   t <- p * 2 # t assigned but not used
  11.   p * 2
  12. }
  13.  
  14. wronguse <- function(x) {
  15.   return(square(...))
  16. }
  17.  
  18. ## Check global environment:
  19.  
  20. ls()
  21. ## [1] "const"       "nonassigned" "square"      "wronguse"
  22.  
  23. ## findLocals
  24. ## ----------
  25.  
  26. codetools::findLocals(quote(q<-1))
  27. ## [1] "q"
  28.  
  29. codetools::findLocals({
  30.   quote({
  31.     square <- function(x) {
  32.       x ^ 2
  33.     }
  34.     const <- 2
  35.     })
  36.   })
  37. ## [1] "const"  "square"
  38.  
  39. ## checkUsage
  40. ## ----------
  41.  
  42. codetools::checkUsage(
  43.   quote({
  44.     square <- function(x) {
  45.       x ^ 2
  46.     }
  47.   })
  48. )
  49. ## <anonymous>: Error while checking: only works for closures
  50.  
  51. codetools::checkUsage(square)
  52. ## <anonymous>: Error while checking: object 'square' not found
  53.  
  54. ## checkUsageEnv
  55. ## -------------
  56.  
  57. codetools::checkUsageEnv(globalenv())
  58. ## nonassigned: local variable 't' assigned but may not be used (<text>:8)
  59. ## wronguse: ... may be used in an incorrect context: 'square(...)' (<text>:13)
  60.  
  61. ## collectUsage
  62. ## ------------
  63.  
  64. codetools::collectUsage(square)
  65. ## NULL
  66.  
  67. codetools::collectUsage(wronguse)
  68. ## Warning: <anonymous>: ... may be used in an incorrect context: 'square(...)' (<text>:13)
  69. ## NULL
  70.  
  71. ## findGlobals
  72. ## -----------
  73.  
  74. codetools::findGlobals(square)
  75. ## [1] "{" "^"
  76.  
  77. codetools::findGlobals(nonassigned)
  78. ## [1] "{"  "*"  "<-"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top