-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive-visualization.Rmd
193 lines (138 loc) · 5.84 KB
/
interactive-visualization.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
---
title: "Interactive Visualizations"
author: "Xingyi Xie"
date: "4/2021"
output: html_document
---
Step-by-step instructions for building a Shiny App
```{r}
library(shiny)
```
## 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/).
```{r}
library(rsconnect)
```
```{r}
rsconnect::setAccountInfo(name='xingyixie', token='520650622BA9509F48691BBD076152DB', secret='z3T8sPPTvnOQuUO+G38LWi00H1+lC9nmAnP22GN+')
```
Now see if you can replace the standard histogram in the above Shiny App with a ggplot histogram hosted on shinyapps.io.
## 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 somethimg new (just in time learning)
substantive - Question involved wrestling with a complex idea that does not have a definitive answer
```{r}
midterm.results <- read.csv("midterm-results.csv")
quiz.categories <- read.csv("quiz-categories.csv")
library('ggplot2')
library('shiny')
date <- midterm.results$Q1_time
num <- midterm.results$Q2_time
d <- data.frame(date,num)
#
ui <- fluidPage(
# Application title
titlePanel("select data"),
# Sidebar with a slider input for date range
sidebarLayout(
sidebarPanel(
sliderInput("date",
"date range:",
min = min(d$date),
max = max(d$date),
value = c(min(d$date),max(d$date))
)
# Show a plot of the generated plot
),
mainPanel(
plotOutput("Plot")
)
)
)
# Define server logic required to draw a ggplot chart
server <- function(input, output) {
df1 <- reactive(subset(d,date < input$date[2] & date > input$date[1],select = c(date,num)))
output$Plot <- renderPlot({
p1 <-ggplot(df1(),aes(date,num))
p1 + geom_line(data=df1(),aes(date,num))
})
}
# Run the application
shinyApp(ui = ui, server = server)
```