index.html 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></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/codemirror">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. # Code from http://www.mayin.org/ajayshah/KB/R/
  31. # FIRST LEARN ABOUT LISTS --
  32. X = list(height=5.4, weight=54)
  33. print("Use default printing --")
  34. print(X)
  35. print("Accessing individual elements --")
  36. cat("Your height is ", X$height, " and your weight is ", X$weight, "\n")
  37. # FUNCTIONS --
  38. square <- function(x) {
  39. return(x*x)
  40. }
  41. cat("The square of 3 is ", square(3), "\n")
  42. # default value of the arg is set to 5.
  43. cube <- function(x=5) {
  44. return(x*x*x);
  45. }
  46. cat("Calling cube with 2 : ", cube(2), "\n") # will give 2^3
  47. cat("Calling cube : ", cube(), "\n") # will default to 5^3.
  48. # LEARN ABOUT FUNCTIONS THAT RETURN MULTIPLE OBJECTS --
  49. powers <- function(x) {
  50. parcel = list(x2=x*x, x3=x*x*x, x4=x*x*x*x);
  51. return(parcel);
  52. }
  53. X = powers(3);
  54. print("Showing powers of 3 --"); print(X);
  55. # WRITING THIS COMPACTLY (4 lines instead of 7)
  56. powerful <- function(x) {
  57. return(list(x2=x*x, x3=x*x*x, x4=x*x*x*x));
  58. }
  59. print("Showing powers of 3 --"); print(powerful(3));
  60. # In R, the last expression in a function is, by default, what is
  61. # returned. So you could equally just say:
  62. powerful <- function(x) {list(x2=x*x, x3=x*x*x, x4=x*x*x*x)}
  63. </textarea></form>
  64. <script>
  65. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
  66. </script>
  67. <p><strong>MIME types defined:</strong> <code>text/x-rsrc</code>.</p>
  68. <p>Development of the CodeMirror R mode was kindly sponsored
  69. by <a href="https://twitter.com/ubalo">Ubalo</a>.</p>
  70. </article>