-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetUp.Rmd
More file actions
executable file
·176 lines (136 loc) · 3.96 KB
/
SetUp.Rmd
File metadata and controls
executable file
·176 lines (136 loc) · 3.96 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
title: "Set Up"
output:
html_document:
toc: true
theme: united
---
```{r, echo=FALSE}
stopifnot(require(svgR, quietly=TRUE))
```
```{r, echo=FALSE}
library(stringr)
showCode %<c-% function(txt){
indentCount<-function(s){
if(grepl(pattern="^ +", s)){
nchar(str_match(pattern="^ +", s))
} else {
0
}
}
line2span<-function(s, default='black'){
pattern='\\\\\\w+'
#browser()
if(grepl(pattern,s)){
spns<-str_split(pattern=pattern,s)[[1]]
cols<-as.character(str_match_all(pattern=pattern,s)[[1]])
cols<-gsub("\\\\","",cols)
if(length(cols)<length(spns)){
cols<-c('black',cols)
}
} else {
spns<-s
cols<-'black'
}
lapply(1:length(spns), function(i){
font.size=ifelse( cols[i]=="black",12,16 )
stroke.width=ifelse( cols[i]=="black",.2,1 )
tspan(spns[i], stroke=cols[i], fill=cols[i], font.size=font.size, stroke.width=stroke.width)
})
}
lines<-str_split(pattern="\\n",txt1)[[1]]
lapply(1:length(lines), function(i){
line<-lines[i]
ic<-indentCount(line)
xy<-c(ic*10+20, i*18+10)
text(xy=xy, line2span(line))
})
}
WH<-c(910,100)
codeBlock %<c-% function(wh){
rect(xy=c(1,1), wh=wh-c(2,2), fill="#F4F4F4", stroke='#444', rxy=c(5,5), stroke.width=.3)
}
```
## Installing
To install svgR we recommend using the **devtools** package. That is,
A. Install R if R is not already installed.
B. Install the **devtools** package if the **devtools** package is not already installed.
This can be done by opening up an R session and then executing the following command:
```
install.packages("devtools")
```
C. Use **devtools** to install **svgR** from **github**
This can be done by opening up an R session and then executing the following commands:
```
library(devtools)
install_github("mslegrand/svgR")
```
## To render in an RPres or R Markdown Document
- Include prior to any calls to svgR include a block with
```{r, echo=FALSE, results="asis"}
txt1<-'\\`\\`\\`{r, echo=FALSE}
\\purple library(svgR)
\\`\\`\\`
'
WH=c(800,80)
svgR( wh=WH, font.family='sans-serif', font.size=12, stroke.width=0.2, letter.spacing=2,
codeBlock(WH),
showCode(txt1)
)
```
### To render
either
- a simple call to svgR inside a block with results='asis'
```{r, echo=FALSE, results="asis"}
txt1<-' \\`\\`\\`{r, echo=TRUE, \\red results="asis"}
\\purple svgR( \\black wh=c(600, 100), circle(cxy=c(200,50),r=50, fill="red") \\purple )
\\`\\`\\`
'
WH=c(800,80)
svgR( wh=WH, font.family='sans-serif', font.size=12, stroke.width=0.2, letter.spacing=2,
codeBlock(WH),
showCode(txt1)
)
```
```{r, echo=FALSE, results="asis"}
svgR( wh=c(600, 100), circle(cxy=c(200,50),r=50, fill='red') )
```
or
- a call to svgR to create an svg markup object and echo back within a block with results='asis'
```{r, echo=FALSE, results="asis"}
txt1<-'\\`\\`\\`{r, echo=TRUE, \\red results="asis"}
\\purple svgM<-svgR( \\black wh=c(600, 100), circle(cxy=c(200,50),r=50, fill="red") \\purple )
\\purple svgM
\\black \\`\\`\\`
'
WH=c(800,80)
svgR( wh=WH, font.family='sans-serif', font.size=12, stroke.width=0.2, letter.spacing=2,
codeBlock(WH),
showCode(txt1)
)
```
```{r, echo=F, results="asis"}
svgM<-svgR( wh=c(600, 100), circle(cxy=c(200,50),r=50, fill='red') )
svgM
```
**Note** Note the mustache is three backticks
## To Rendered in an RStudio **Shiny-server**
- Require svgR
- In the ui, set the svg output to be an htmlOutput
- In the server,
+ convert the svg markup to text using *as.character*
+ set the corresponding output object to renderUI of an
*HTML* frame of the svg markp text.
A short example is:
```
require(shiny)
svgMarkup<-svgR( circle(cxy=c(200,100),r=50, fill='red') )
app <- list(
ui = bootstrapPage( htmlOutput("svg") ) ,
server = function(input, output) {
output$svg<-renderUI( { HTML(
as.character(svgMarkup)) } ) }
)
runApp(app)
```
**Again Note** we convert the svgMarkup to a character vector before rendering.