-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentacion_Shiny.Rmd
287 lines (219 loc) · 7.8 KB
/
presentacion_Shiny.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
---
title: "SHINY"
author: " Grupo 5 <br/> INTEGRANTES <br/> Bayona Montañez <br/> Dulanto Castañeda <br/> Nina More"
date: "21/8/2021"
output:
xaringan::moon_reader:
css: ["default", "middlebury-fonts", "kunoichi"]
nature:
highlighStyle: github
highLine: true
countIncrementalSlides: true
---
class: inverse
# ¿QUÉ ES SHINY?
<img src="imagen/shiny_logo.png" width="100" height="100">
###Shiny es un paquete R que facilita la creación de aplicaciones web interactivas, directamente desde R. Shiny le permite tomar su trabajo en R y exponerlo a través de un navegador web para que cualquiera pueda usarlo.
En pocas palabras Shiny es un paquete de R que permite construir aplicaciones web interactivas a partir de los scripts de R.
###INSTALACIÓN:
```{R=V}
install.packages (" shiny " )
library (" shiny ")
```
---
###EJEMPLOS DE APLICACIONES:
```{R=V}
runExample ( " 01_ hello ") # un histograma
runExample ( " 02_ text ") # tablas y dataframes
runExample ( " 03_ reactivity ") # una expresi ón reactiva
runExample ( " 04_ mpg ") # variables globales
runExample ( " 05_ sliders ") # barras deslizables
runExample ( " 06_ tabsets ") # varias pesta ñas
runExample ( " 07_ widgets ") # textos de ayuda y botones
runExample ( " 08_ html ") # Shiny desde HTML
runExample ( " 09_ upload " ) # carga de archivos
runExample ( " 10_ download ") # descarga de archivos
runExample ( " 11_ timer ") # día y hora
```
---
class: inverse
##CREANDO UN SHINY
File Create project from: <br/> Project Type:<br/>
New project .<br/> New directory .<br/> Shiny Web Application <br/>
El resultado es un directorio nuevo con el nombre que hayamos elegido en el que
aparecen dos archivos (ui.R y server.R).
<UL type=square>
Una app de shiny consta (al menos) de dos archivos:<br/><br/><br/>
<LI>un script para la interfaz del usuario, (user-interface, ui.R), que recibe los inputs y
muestra los outputs, y<br/><br/><br/>
<LI>un script para los cálculos (server.R), que realiza los cálculos necesarios.<br/><br/><br/>
<img src="imagen/CREANDO_PROYECTO.png" width="600" height="200">
---
class: inverse
##INPUT
.pull.left[
<img src="imagen/input.png" width="200" height="400">]
.pull.right[
<img src="imagen/input 2.png" width="200" height="400">]
---
##OUTPUTS
<img src="imagen/outputs.png" width="900" height="400">
---
##UN PRIMER EJEMPLO
###LOS HISTOGRAMAS:
<img src="imagen/histograma.png" width="1200" height="400">
---
class: inverse
INPUTS (Elementos de ingreso)
Siguiendo con la línea de interacción de Shiny, tenemos:
Sliders
Esta herramienta permite obtener entradas del control deslizante, las que se pueden usar para seleccionar valores individuales, para seleccionar un rango continuo de valores e incluso para animar sobre un rango.
Se puede configurar de la siguiente forma:
SliderInput(inputId, label, min, max,
value, step, round, format, locale, ticks,
animate, width, sep, pre, post,
timeFormat, timezone, dragRange)
<img src="imagen/sliders.png" width="600" height="200">
---
##Sliders
A partir de los modelos predeterminados podemos crear nuestras propias visualizaciones web.
Una forma sencilla de acceder a Slider es con:
runExample(“05_sliders”) # slider bars
Pero para entender como funciona iremos desde cero.
Analicemos lo que sucede…
Define la interfaz de usuario.
Especifica el comportamiento de nuestra aplicación definiendo una
serverfunción. En la imagen se aprecia vacío.
Se ejecuta shinyApp(ui, server)para construir e iniciar una aplicación
Shiny desde la interfaz de usuario y el servidor.
<img src="imagen/sliders2.png" width="600" height="200">
---
Trabajando en el input:
```{R=V}
ui <- fluidPage(
titlePanel("Sliders_Inicio"),
sidebarLayout(
sidebarPanel(
sliderInput("integer", “Enteros:",
min = 0, max = 1000,
value = 500),
sliderInput("decimal", "Decimales:",
min = 0, max = 1,
value = 0.5, step = 0.1)
sliderInput("range", "Rangos:",
min = 1, max = 1000,
value = c(200,500)),
sliderInput("format", “Personalizado:",
min = 0, max = 10000,
value = 0, step = 2500,
pre = "$", sep = ",",
animate = TRUE),
sliderInput("animation", “Serie_Animacion:",
min = 1, max = 2000,
value = 1, step = 10,
animate =
animationOptions(interval = 300, loop = TRUE))
),
mainPanel(
tableOutput("values")
)
)
)
```
---
##Que se observa?
<UL type=square>
###<LI>Agregado del titulo “Sliders_Inicio” para el panel.
###<LI>SiderbarLayout para la vista.
###<LI>SiderbarPanel crea multiples widget
###<LI> Ingresa los barras interactivas. Escogiendo para los ejemplos
#Enteros, #Decimales, #Rangos, #Personalizado y #Aninacion.
###<LI>mainPanel: Crea una tabla con los valores de salida fijados por las barras de desplazamiento creada.
###<LI>tableOutput: Resume la tabla de valores de salida.
---
class: inverse
```{R=V}
Trabajando en el server:
server <- function(input, output) {
sliderValues <- reactive({
data.frame(
Name = c(“E
ntero",
"Decimales",
"Rangos",
“Personalizado",
“Serie_Animation"),
Value = as.character(c(input$integer,
input$decimal,
paste(input$range, collapse = " "),
input$format,
input$animation)),
stringsAsFactors = FALSE)
})
output$values <- renderTable({
sliderValues()
})
}
shinyApp(ui, server)
```
---
class: inverse
#Que se observa?
<UL type=square>
###<LI>Reactive: Crea una expresión para fijar un marco de datos de todos los valores de entrada.
###<LI>Se crea un data frame con las etiquetas para los valores de entrada.
###<LI>Se indica los valores de salida $, mostrados en una tabla.
###<LI> Crea Shiny app.
---
class: inverse
<img src="imagen/slider3.png" width="1200" height="600">
---
##Usar scripts y datos de R
```{R=V}
library(mapproj)
source("helpers.R")
counties <- readRDS("data/counties.rds")
percent_map(counties$white, "darkgreen", "% White")
ui <- fluidPage(
titlePanel("censo de EE.UU. en el 2010"),
sidebarLayout(
sidebarPanel(
helpText("Create demographic maps with
information from the 2010 US Census."),
selectInput("var",
label = "Choose a variable to display",
choices = c("Percent White", "Percent Black",
"Percent Hispanic", "Percent Asian"),
selected = "Percent White"),
sliderInput("range",
label = "Range of interest:",
min = 0, max = 100, value = c(0, 100))
),
mainPanel(plotOutput("map"))
)
)
# Server logic ----
server <- function(input, output) {
output$map <- renderPlot({
data <- switch(input$var,
"Percent White" = counties$white,
"Percent Black" = counties$black,
"Percent Hispanic" = counties$hispanic,
"Percent Asian" = counties$asian)
color <- switch(input$var,
"Percent White" = "darkgreen",
"Percent Black" = "black",
"Percent Hispanic" = "darkorange",
"Percent Asian" = "darkviolet")
legend <- switch(input$var,
"Percent White" = "% White",
"Percent Black" = "% Black",
"Percent Hispanic" = "% Hispanic",
"Percent Asian" = "% Asian")
percent_map(data, color, legend, input$range[1], input$range[2])
})
}
# Run app ----
shinyApp(ui, server)
```
---