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.

88 lines
2.5 KiB

2 months ago
  1. <!doctype html>
  2. <title>CodeMirror: R mode</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../../doc/docs.css">
  5. <link rel="stylesheet" href="../../lib/codemirror.css">
  6. <script src="../../lib/codemirror.js"></script>
  7. <script src="r.js"></script>
  8. <style>
  9. .CodeMirror { border-top: 1px solid silver; border-bottom: 1px solid silver; }
  10. .cm-s-default span.cm-semi { color: blue; font-weight: bold; }
  11. .cm-s-default span.cm-dollar { color: orange; font-weight: bold; }
  12. .cm-s-default span.cm-arrow { color: brown; }
  13. .cm-s-default span.cm-arg-is { color: brown; }
  14. </style>
  15. <div id=nav>
  16. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png" alt=""></a>
  17. <ul>
  18. <li><a href="../../index.html">Home</a>
  19. <li><a href="../../doc/manual.html">Manual</a>
  20. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  21. </ul>
  22. <ul>
  23. <li><a href="../index.html">Language modes</a>
  24. <li><a class=active href="#">R</a>
  25. </ul>
  26. </div>
  27. <article>
  28. <h2>R mode</h2>
  29. <form><textarea id="code" name="code">
  30. X <- list(height = 5.4, weight = 54)
  31. cat("Printing objects: "); print(X)
  32. print("Accessing individual elements:")
  33. cat(sprintf("Your height is %s and your weight is %s\n", X$height, X$weight))
  34. # Functions:
  35. square <- function(x) {
  36. return(x * x)
  37. }
  38. cat(sprintf("The square of 3 is %s\n", square(3)))
  39. # In R, the last expression in a function is, by default, what is
  40. # returned. The idiomatic way to write the function is:
  41. square <- function(x) {
  42. x * x
  43. }
  44. # or, for functions with short content:
  45. square <- function(x) x * x
  46. # Function arguments with default values:
  47. cube <- function(x = 5) x * x * x
  48. cat(sprintf("Calling cube with 2 : %s\n", cube(2)) # will give 2^3
  49. cat(sprintf("Calling cube : %s\n", cube()) # will default to 5^3.
  50. powers <- function(x) list(x2 = x*x, x3 = x*x*x, x4 = x*x*x*x)
  51. cat("Powers of 3: "); print(powers(3))
  52. # Data frames
  53. df <- data.frame(letters = letters[1:5], '#letter' = 1:5)
  54. print(df$letters)
  55. print(df$`#letter`)
  56. # Operators:
  57. m1 <- matrix(1:6, 2, 3)
  58. m2 <- m1 %*% t(m1)
  59. cat("Matrix product: "); print(m2)
  60. # Assignments:
  61. a <- 1
  62. b <<- 2
  63. c = 3
  64. 4 -> d
  65. 5 ->> e
  66. </textarea></form>
  67. <script>
  68. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
  69. </script>
  70. <p><strong>MIME types defined:</strong> <code>text/x-rsrc</code>.</p>
  71. <p>Development of the CodeMirror R mode was kindly sponsored
  72. by <a href="https://twitter.com/ubalo">Ubalo</a>.</p>
  73. </article>