-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive-visualization.Rmd
More file actions
268 lines (207 loc) · 8.16 KB
/
Copy pathinteractive-visualization.Rmd
File metadata and controls
268 lines (207 loc) · 8.16 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
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
---
title: "Interactive Visualizations"
author: "Nicole Schlosberg"
date: "4/2/2021"
output: html_document
---
Step-by-step instructions for building a Shiny App
```{r}
library(shiny)
#library(shinydashboard)
#install.packages("devtools")
#require(devtools)
#install_github('ramnathv/rCharts')
#library(rCharts)
```
## Basic Button
Review the annotated code below
```{r}
ui <- fluidPage(actionButton("goButton", "Wake up!"),
textOutput("reply"))
server <- function(input, output) {
observeEvent(input$goButton, #1. Create reactivity wih input$
output$reply <- #2. Save output to output$
renderText({"5 more minutes..."})) #3. Build output with render*()
}
shinyApp(ui = ui, server = server)
```
## Random Histogram Generator
Now we will build another Shiny App one piece at a time (Only the code starting at line 97 will run). This app will generate a histogram based on random values drawn from a normal distribution, the user will be able to select the number of draws that generate the histogram by using a slider.
1. Begin with the template:
```{r}
ui <- fluidPage()
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
```
2. Then, start by defining the user interface (ui) object by describing the components of fluidPage(). We want to use a slider, so the input expression is sliderInput(). We label the slider object "num" and the label that the user will see "Choose a number". Three other arguments are required sepcifically for sliders:
value: the value the slider will start at when the app loads
min: the minimum value the slider will have
max: the maximum value the slider will have
```{r}
ui <- fluidPage(
sliderInput(inputId = "num", label = "Choose a number",
value = 1, min = 1, max = 100),
)
```
3. Now we need to define the output expression. Here will we name it "hist"
```{r}
ui <- fluidPage(
sliderInput(inputId = "num", label = "Choose a number",
value = 1, min = 1, max = 100),
plotOutput("hist")
)
```
4. Now that we have defined the ui object we can move on to defining the server object. We are going to call the output label we just defined, "hist" by refering to it as ``"output$hist"``. We will then use the renderPlot() expression to call a plot.
```{r}
server <- function(input, output) {
output$hist <- renderPlot({})
}
```
5. The command for generating a random number drawn from a normal distribution is "rnorm()". Try rnorm in teh command line using any number you like between the parentheses. It will draw numbers from a normal distribution with mean of zero and standard deviation of 1. Now, put the hist() command around your rnorm command:
hist(rnorm())
Now you have generated a random histogram! We want our Shiny App to do this, but with a range of values that the user can choose from.
6. The way to achieve our histogram is to call it from within curly brackets ({}), curly brackets bind a set of commands together so R will read them at the same time. Our complete server function will look like:
```{r}
server <- function(input, output) {
output$hist <- renderPlot({
hist(rnorm(input$num))
})
}
```
7. We can then put everything together to generate our Shiny App. Run the code below.
```{r}
ui <- fluidPage(
sliderInput(inputId = "num", label = "Choose a number",
value = 1, min = 1, max = 100), plotOutput("hist"))
server <- function(input, output) {
output$hist <- renderPlot({
hist(rnorm(input$num))
})
}
shinyApp(ui = ui, server = server)
```
## Deploy
Go to the [Shiny Apps hosting service](http://www.shinyapps.io/) and set up an account. Transfer your visualization to the RStudio servers according to the instructions [here](http://docs.rstudio.com/shinyapps.io/).
Now see if you can replace the standard histogram in the above Shiny App with a ggplot histogram hosted on shinyapps.io.
```{r}
#rsconnect::setAccountInfo(name='06w8d7-nicole0schlosberg', token='53C9C3CD1AA643128FF3837CBCEE58E4', secret='nxVBCSN9dkv+I0S48EC2fgbUREmds1sN5nkjZdEI')
#library(rsconnect)
#rsconnect::deployApp('/Users/nicoleschlosberg/Documents/workspace/Git/interactive-visualization')
```
```{r}
library(shiny)
library(ggplot2)
ui <- fluidPage(
sliderInput(inputId = "num", label = "Choose a number",
value = 1, min = 1, max = 100), plotOutput("ggplot"))
server <- function(input,output){
output$ggplot <- renderPlot({
#if(input$num=1)
df <- data.frame(rnorm(input$num))
ggplot(df, aes(x=rnorm(input$num))) + geom_histogram() + ggtitle("Ggplot Histogram of Input Data") + xlab("Input Number") + ylab("Count")
})
}
shinyApp(ui = ui, server = server)
```
## Final Project
Finally, build an interactive visualization using the data sets quiz-categories.csv and midterm-results.csv. These data represent midterm results from an open book test. Deploy your dashboard to the Shiny Apps hosting service.
The categories represent the skills required to answer each question:
wrangling - Question required data manipulations skills
coding - Question required coding skills
d.trees - Question invoilved decision trees
sna - Question involved social network analysis
nlp - Question involved natural language processing
viz - Question involved visualization of data
n.nets - Question involved neural nets
googleable - Question could be answered by searching the internet
non-googleable - Question could not be answered through simple searching of the internet
jitl - Question involved learning something new (just in time learning)
substantive - Question involved wrestling with a complex idea that does not have a definitive answer
```{r}
library(dplyr)
library(tidyr)
#Import data
Q1 <- read.csv("quiz-categories.csv", header = TRUE)
M1 <- read.csv("midterm-results.csv", header = TRUE)
#Organize data
M1 <- select(M1,1:32)
M1 <- select(M1,-2)
M2 <- gather(M1,2:31,key="Question",value="counter", na.rm = TRUE, convert = FALSE) %>% select(id,Question,counter)
Q2 <- as.matrix(Q1)
Q2 <- t(Q1)
Q2 <- data.frame(Q2)
names <- rownames(Q2)
rownames(Q2) <- NULL
Q2 <- cbind(names,Q2)
Q2[1,1] <- "Subject"
names <- Q2[1,]
colnames(Q2) <- names
Q2 <- Q2[-1,]
Q3 <- gather(Q2,2:31,key="Question",value="counts", na.rm = TRUE, convert = FALSE) %>% select(Subject,Question,counts)
D1<- left_join(Q3,M2,by=c("Question"="Question"))
D1 <-unique(D1)
D1$key <- ifelse(D1$counts==0,0, ifelse(D1$counts==1&D1$counter==0,0, ifelse(D1$counts==1&D1$counter==1,1, ifelse(D1$counts==0&D1$counter==0,0,NA))))
D2 <- D1 %>% group_by(id,Subject) %>% summarize(score=sum(key))
D2 <-unique(D2)
I1 <- spread(D2,Subject,score)
I1<-data.frame(I1)
names <- I1$id
rownames(I1) <- names
I1 <- I1[,-1]
#Save dataframe to a csv file so it can be imported into the shiny app
write.csv(I1,"/Users/nicoleschlosberg/Documents/workspace/Git/interactive-visualization/interactive-visualization/Calculated-Scores.csv", row.names = TRUE)
```
#Final interactive interface
```{r}
library(shiny)
library(ggplot2)
#Set up interactive interface
ui <- fluidPage(
titlePanel("Student Midterm Scores by Subject"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("show_subjects", "Pick Subject to See Scores:", names(I1),selected = NA)
),
mainPanel(
DT::dataTableOutput("scores")
)
)
)
server <- function(input, output) {
output$scores <- DT::renderDataTable({
DT::datatable(I1[, input$show_subjects, drop = FALSE])
},include.rownames=TRUE)
}
shinyApp(ui = ui, server = server)
```
#Testing out code
```{r}
library(shiny)
library(ggplot2)
#Set up interactive interface
ui <- fluidPage(
titlePanel("Student Midterm Scores by Subject"),
sidebarLayout(
sidebarPanel(
#conditionalPanel(
#'input.dataset === "I1"',
checkboxGroupInput("show_subjects", "Pick Subject to See Scores:", names(I1),selected = NA)
#),
#selectInput(inputId = "show_subjects", label = "Pick Subject", choices = names(I1), selected = "Select")
),
mainPanel(
#id = 'dataset',
#"show_subjects",
#tableOutput(
DT::dataTableOutput("scores")#,
)
)
)
server <- function(input, output) {
#I2 = I1[sample(nrow(I1)),]
output$scores <- DT::renderDataTable({
DT::datatable(I1[, input$show_subjects, drop = FALSE])
},include.rownames=TRUE)
}
shinyApp(ui = ui, server = server)
```