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.

83 lines
1.7 KiB

  1. ---
  2. title: "Test"
  3. author: "Pjotr"
  4. date: "24/02/2020"
  5. output: html_document
  6. ---
  7. ```{r setup, include=FALSE}
  8. knitr::opts_chunk$set(echo = TRUE)
  9. ```
  10. ## R Markdown
  11. This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
  12. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
  13. ```{r cars}
  14. summary(cars)
  15. ```
  16. ## Including Plots
  17. You can also embed plots, for example:
  18. ```{r pressure, echo=FALSE}
  19. plot(pressure)
  20. ```
  21. Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
  22. ## Environments
  23. R environments are used to bind names against values. In
  24. this case the subjectid is the name.
  25. ```{r}
  26. id <- new.env()
  27. id$16K0021 = 'first'
  28. ```
  29. oops, can't start with a number
  30. ```{r}
  31. id$m16K0021 = 'first'
  32. ls(id, all.names = TRUE)
  33. ```
  34. Note that data frames use environment too with the $ notation.
  35. To add material to an environment we create a new environment (a hash of hash). So
  36. ```{r}
  37. mydata <- new.env()
  38. mydata$days = 21
  39. mydata$name = "Pjotr Prins"
  40. id$m16K0021 = mydata
  41. ls(id$m16K0021, all.names = TRUE)
  42. id$m16K0021$name
  43. ```
  44. That works well. Step is to be able to parametrize
  45. ```{r}
  46. id[["m16K0021"]]$days
  47. ```
  48. So you can see the dollar notation is just syntactic sugar.
  49. ```{r}
  50. id[["m16Ktest"]] = new.env()
  51. test = id[["m16Ktest"]]
  52. test$name = "Jan Wolkers"
  53. test$days = 22
  54. ls(id, all.names = TRUE)
  55. ls(test, all.names=TRUE)
  56. test$name
  57. ```
  58. See also http://adv-r.had.co.nz/Environments.html