-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.R
158 lines (129 loc) · 6.18 KB
/
app.R
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
# load the necessary libraries
library(shiny)
library(ggplot2)
library(plotly)
# load the data
data(faithful)
# Define UI
ui <- fluidPage(
# App title
headerPanel("Old Faithful Data"),
# side-by-side layout
sidebarLayout(
# panel for inputs
sidebarPanel(
# dropdown to choose which marginal plots to show
# https://shiny.rstudio.com/reference/shiny/latest/selectInput.html
selectInput(
"marginsToShow", "Marginal Axes to Plot:",
c("x","y","both", "none"),
selected = "both"
),
# dropdown to choose the type of marginal distribution
# https://shiny.rstudio.com/reference/shiny/1.6.0/conditionalPanel.html
conditionalPanel(
condition = "input.marginsToShow != 'none'" ,
selectInput(
"marginalFormat", "Marginal Format:",
c("histogram", "density"),
selected = "histogram"
)
),
# sliders to choose the bin size for the x and y axes (only relevant for histogram margin type)
# https://shiny.rstudio.com/reference/shiny/latest/sliderInput.html
conditionalPanel(
condition = "input.marginalFormat == 'histogram' && input.marginsToShow != 'y' && input.marginsToShow != 'none'" ,
sliderInput(
"xbinwidth", "Eruption Duration binwidth (min.):",
min = 1e-3, max = 1, value = 0.1
),
),
conditionalPanel(
condition = "input.marginalFormat == 'histogram' && input.marginsToShow != 'x' && input.marginsToShow != 'none'" ,
sliderInput(
"ybinwidth", "Time Between Erruptions binwidth (min.):",
min = 1e-3, max = 20, value = 1
),
),
# checkboxes to turn on/off plot elements
# https://shiny.rstudio.com/reference/shiny/latest/checkboxInput.html
strong("Additional Plot Options:"), # a simple html element to provide a title for this section of the UI
checkboxInput(
"showContours", "Include Contours",
value = FALSE
),
checkboxInput(
"showFitLine", "Include Linear Fit Line",
value = TRUE
)
),
# Main panel for displaying outputs
mainPanel(
# https://shiny.rstudio.com/reference/shiny/latest/plotOutput.html
plotlyOutput("finalPlot", height = "500px"),
conditionalPanel(
condition = "input.showFitLine",
# https://shiny.rstudio.com/reference/shiny/1.0.3/verbatimTextOutput.html
verbatimTextOutput("modelSummary")
)
)
)
)
# Define server logic
server <- function(input, output) {
# when we have input values that we want to use for generating output,
# we need to wrap that portion of the code in observe({}), or another reactive container
observe({
# create the scatter plot
main_plot <- ggplot(faithful, aes(eruptions, waiting)) +
geom_point() +
scale_x_continuous(limits = c(1, 5.9), expand = c(0, 0)) +
scale_y_continuous(limits = c(40, 99), expand = c(0, 0)) +
labs(x ="Eruption Duration (min.)", y = "Time Between Eruptions (min.)") +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
# add the contours if requested by the user
if (input$showContours) main_plot <- main_plot + geom_density2d()
# add the fit line if requested by the user
if (input$showFitLine) main_plot <- main_plot + stat_smooth(method = "lm", formula = y ~ x, geom = "smooth", se = FALSE, color = "red") # lm for linear
f <- main_plot
# add the marginal plots if desired
if (input$marginsToShow != "none"){
# set up empty plots that will hold the marginal distributions
top_plot <- ggplot(faithful, aes(eruptions)) +
scale_x_continuous(limits = c(1, 5.9), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, NA), expand = c(0, 0)) +
theme_classic() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
right_plot <- ggplot(faithful, aes(waiting)) + coord_flip() +
scale_x_continuous(limits = c(40, 99), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, NA), expand = c(0, 0)) +
theme_classic() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
# add the histograms or density plots
if (input$marginalFormat == 'histogram'){
top_plot <- top_plot + geom_histogram(binwidth = input$xbinwidth)
right_plot <- right_plot + geom_histogram(binwidth = input$ybinwidth)
}
if (input$marginalFormat == 'density'){
top_plot <- top_plot + geom_density()
right_plot <- right_plot + geom_density()
}
if (input$marginsToShow == "y") top_plot <- plotly_empty()
if (input$marginsToShow == "x") right_plot <- plotly_empty()
f <- subplot(
top_plot, plotly_empty(), main_plot, right_plot,
nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2), margin = 0,
shareX = TRUE, shareY = TRUE
)
}
# store the figure in the "finalPlot" key of the output variable which can be seen by the UI
output$finalPlot <- renderPlotly(f)
# store the output from the linear fit in the "modelSummary" key of the output variable which can be seen by the UI
output$modelSummary <- renderPrint(
summary(lm(waiting ~ eruptions, data = faithful))
)
})
}
# Return the shiny.appobj object
shinyApp(ui, server)