-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.Rmd
99 lines (76 loc) · 2.54 KB
/
example.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: "TryRPython"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
if (!require("knitr")) {
install.packages("knitr")
library("knitr")
}
knitr::opts_chunk$set(echo = TRUE)
if (!require("reticulate")) {
install.packages("reticulate")
}
library(reticulate)
```
## R Markdown
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>.
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:
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
If the output target is HTML, R has some methods for turning R data into JavaScript or JSON data and printing the results within HTML tags:
```{r car_data}
head(mtcars)
```
```{r car_data_in_html, echo=FALSE, results='asis'}
library(jsonlite)
cat(paste('<div id="car_data_in_html"><script type="application/json">', toJSON(mtcars), '\n</script>\n</div>', sep=""))
```
In RStudio it is also possible to include Python code chunks in R Markdown, using the `reticulate` package that was loaded (in R) earlier:
```{python data_test}
data = {"foo": {"bar": "baz"}}
string = "Hydrogen"
for index, letter in enumerate(string):
print((letter, index))
```
```{python plot}
try:
import matplotlib.pyplot as plt
import numpy as np
# %matplotlib inline
# %config InlineBackend.figure_format = 'svg'
t = np.linspace(0, 20, 500)
plt.plot(t, np.sin(t))
plt.show()
except Exception as e:
print(e)
```
```{r map}
library(DBI)
library(RPostgreSQL)
library(sp)
options(warn=-1) # versions of GEOS runtime 3.7.2-CAPI-1.11.2 and GEOS at installation 3.7.1-CAPI-1.11.1 differ
library(rgdal)
library(rgeos)
library(sf)
library(maptools)
options(warn=0)
data(wrld_simpl)
xlim <- c(-130,-60)
ylim <- c(35,50)
plot(wrld_simpl,xlim=xlim,ylim=ylim,col='olivedrab3',bg='lightblue')
plot(wrld_simpl)
coords <- matrix(c(-122.92,-79.4, 49.277,43.66),ncol=2)
coords <- matrix(c(-122.92,-79.4, 49.277,43.66),ncol=2)
coords <- coordinates(coords)
spoints <- SpatialPoints(coords)
df <- data.frame(location=c("SFU","UofT"))
spointsdf <- SpatialPointsDataFrame(spoints,df)
plot(wrld_simpl) + plot(spointsdf,add=T,col=c('red','blue'),pch=16)
```