2
0

index.html 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeMirror: R mode</title>
  6. <link rel="stylesheet" href="../../lib/codemirror.css">
  7. <script src="../../lib/codemirror.js"></script>
  8. <script src="r.js"></script>
  9. <style>
  10. .CodeMirror { border-top: 1px solid silver; border-bottom: 1px solid silver; }
  11. .cm-s-default span.cm-semi { color: blue; font-weight: bold; }
  12. .cm-s-default span.cm-dollar { color: orange; font-weight: bold; }
  13. .cm-s-default span.cm-arrow { color: brown; }
  14. .cm-s-default span.cm-arg-is { color: brown; }
  15. </style>
  16. <link rel="stylesheet" href="../../doc/docs.css">
  17. </head>
  18. <body>
  19. <h1>CodeMirror: R mode</h1>
  20. <form><textarea id="code" name="code">
  21. # Code from http://www.mayin.org/ajayshah/KB/R/
  22. # FIRST LEARN ABOUT LISTS --
  23. X = list(height=5.4, weight=54)
  24. print("Use default printing --")
  25. print(X)
  26. print("Accessing individual elements --")
  27. cat("Your height is ", X$height, " and your weight is ", X$weight, "\n")
  28. # FUNCTIONS --
  29. square <- function(x) {
  30. return(x*x)
  31. }
  32. cat("The square of 3 is ", square(3), "\n")
  33. # default value of the arg is set to 5.
  34. cube <- function(x=5) {
  35. return(x*x*x);
  36. }
  37. cat("Calling cube with 2 : ", cube(2), "\n") # will give 2^3
  38. cat("Calling cube : ", cube(), "\n") # will default to 5^3.
  39. # LEARN ABOUT FUNCTIONS THAT RETURN MULTIPLE OBJECTS --
  40. powers <- function(x) {
  41. parcel = list(x2=x*x, x3=x*x*x, x4=x*x*x*x);
  42. return(parcel);
  43. }
  44. X = powers(3);
  45. print("Showing powers of 3 --"); print(X);
  46. # WRITING THIS COMPACTLY (4 lines instead of 7)
  47. powerful <- function(x) {
  48. return(list(x2=x*x, x3=x*x*x, x4=x*x*x*x));
  49. }
  50. print("Showing powers of 3 --"); print(powerful(3));
  51. # In R, the last expression in a function is, by default, what is
  52. # returned. So you could equally just say:
  53. powerful <- function(x) {list(x2=x*x, x3=x*x*x, x4=x*x*x*x)}
  54. </textarea></form>
  55. <script>
  56. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
  57. </script>
  58. <p><strong>MIME types defined:</strong> <code>text/x-rsrc</code>.</p>
  59. <p>Development of the CodeMirror R mode was kindly sponsored
  60. by <a href="http://ubalo.com/">Ubalo</a>, who hold
  61. the <a href="LICENSE">license</a>.</p>
  62. </body>
  63. </html>