-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
196 lines (159 loc) · 6.91 KB
/
README.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---
title: "autoshiny -- R package for automatic transformation of an R function into a Shiny app"
author: "Aleksander Rutkowski"
date: "`r format(Sys.Date())`"
output: github_document
always_allow_html: yes
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
# Rendering this doc:
# rmarkdown::render(paste0(getwd(),'/README.Rmd'), envir=globalenv())
knitr::opts_chunk$set(message=FALSE)
knitr::opts_chunk$set(engine='R')
options(python_path = "C:/Python27/python.exe")
i <- 0
print.shiny.appobj2 <- function(x, ...) { # needed to prepare the screenshots, overriding shiny's
i <<- i+1
fname <- paste0('screenshot',i,'.png')
cat("", file=fname) # pre-save the file placeholder so that pandoc dones not complain
webshot::appshot(structure(x, class='shiny.appobj'),
file=paste0(getwd(),'/',fname),
vheight=300,
delay=3)
cat(paste0('\n![](https://cdn.rawgit.com/alekrutkowski/autoshiny/master/',fname,')\n\n', # needs results='asis' in chunk options
'See the [source code](https://github.com/alekrutkowski/autoshiny/tree/master/Example_',i,') generated with `makeFiles` ',
'(and formatted with [rfmt](https://github.com/google/rfmt)) ',
'of the app whose initial-state screenshot is displayed above.'))
}
```
## Installation
[From CRAN](https://CRAN.R-project.org/package=autoshiny):
```{r, eval=FALSE}
install.packages('autoshiny')
```
or the latest version from GitHub:
```{r, eval=FALSE}
# if package `devtools` not installed, first do this:
# install.packages('devtools')
devtools::install_github('alekrutkowski/autoshiny')
```
## Key info
There are two key twin functions: `makeApp` and `makeFiles`.
Both of them take a function as their first argument/parameter.
Function `makeApp` returns [a Shiny app object](https://rdrr.io/cran/shiny/man/shinyApp.html).
`makeFiles` produces `ui.R` and `server.R` files.
These files can be further edited to tweak the app if needed.
Using **autoshiny** does not imply any run-time dependency of the compiled app on **autoshiny**
i.e. **autoshiny** is needed only at the compile time. **autoshiny** uses standard Shiny
input and output widgets and render functions. Tiny helper functions are embedded in the
compiled app code to make it self-contained.
All the arguments/parameters of the function passed to `makeApp` and `makeFiles` *must have default values* which will
be used by **autoshiny** to define each argument's:
- type/class -- Shiny input widget type,
- allowed values -- Shiny input widget's allowed states,
- pre-selected/start-up value of the Shiny input widget -- for categorical values (e.g. integers, factors, character vectors
that will be the first element of the vector).
The default values of the function arguments/parameters will be also used to pre-evaluate that function in order to
test it and to determine the type of its output (return value / side effect) and, hence, the Shiny output widget.
## Examples
```{r, eval=FALSE}
library(autoshiny)
```
```{r}
library(shiny)
```
```{r, include=FALSE}
j <- 0
makeApp <- function(...) {
j <<- j + 1
autoshiny::makeFiles(...,directory=getwd())
ExN <- paste0('Example_',j)
if (!dir.exists(ExN))
dir.create(ExN)
fls <- c('server.R','ui.R')
fls2 <- paste0(ExN,'/',fls)
file.copy(fls, fls2)
file.remove(fls)
lapply(fls2,
rfmt::rfmt)
structure(autoshiny::makeApp(...),
class='shiny.appobj2')
}
File <- autoshiny::File
```
### Example 1: Trivial anonymous function
```{r, results='asis'}
makeApp(function(x=1:3, y=5:9) x+y)
```
### Example 2: Nicer function and argument names
```{r, results='asis', fig.keep='none'}
`Histogram for normal distribution` <-
function(`Number of observations` =
as.integer(c(100,10,1000))) # as.integer => the argument interpreted as categorical
plot(hist(rnorm(`Number of observations`))) # Generic R plots as "return values" are supported
makeApp(`Histogram for normal distribution`)
```
### Example 3: Data frame in (upload CSV), data frame out (displayed and downloadable as CSV)
```{r, results='asis'}
`Table of sin and cos values` <-
function(`Upload CSV file with column "x"` =
data.frame(x = seq(0, 2*pi, .25))) {
dta <- `Upload CSV file with column "x"`
data.frame(X = dta$x,
`Sin of X` = sin(dta$x),
`Cos of X` = cos(dta$x),
check.names = FALSE)
}
makeApp(`Table of sin and cos values`)
```
### Example 4: Arbitrary input and output files
```{r, results='asis'}
openxlsx::write.xlsx(data.frame(x=1:5,
y=11:15),
'my_test_file.xlsx')
`Excel file in and out` <-
function(`Input Excel file` =
File('my_test_file.xlsx')) { # File() obligatory here!
my.data <- openxlsx::read.xlsx(`Input Excel file`)
my.data2 <- within(my.data,
z <- x + y)
openxlsx::write.xlsx(my.data2,
'my_test_file_2.xlsx')
File('my_test_file_2.xlsx') # File() obligatory here too!
}
makeApp(`Excel file in and out`)
```
### Example 5: Using a button as a (re-)evaluation trigger
Use this option if:
- the evaluation of your functon takes time, so it should not be re-evaluated with every minor change of the value of inputs/arguments/parameter;
- the function is impure e.g. depends on some external data fetched internally and takes no arguments/parameters -- in such a case the function would be re-evaluated only through page refresh of the browser; the button is a faster and a more elegant solution.
```{r, results='asis'}
`Get "GDP and main components" from Eurostat` <-
function() {
# Getting data from
# http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&file=data%2Fnama_10_gdp.tsv.gz
x <- eurodata::importData('nama_10_gdp')
head(x, 10)
}
makeApp(`Get "GDP and main components" from Eurostat`,
withGoButton = TRUE)
```
### Example 6: Lists of inputs (arguments) and the output list (composite return value) are always decomposed
```{r, results='asis'}
`A function with lists everywhere` <-
function(`First argument group,` = list(`number one` = 1:3,
`number two` = letters[1:3]),
`2nd arg group,` = list(`1st argument` = 11:14,
`second arg.` = LETTERS[1:5]))
list(`Some text` =
as.character(c(`First argument group,`$`number two`,
`2nd arg group,`$`second arg.`)),
`Some numbers` =
`First argument group,`$`number one` +
`2nd arg group,`$`1st argument`,
`Even a ggplot2 chart` =
ggplot2::qplot(a,b,data=data.frame(a=1:20,b=log(1:20))))
makeApp(`A function with lists everywhere`)
```