-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
131 lines (116 loc) · 4.2 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
# ###### Instructions ##############################
#
# The below app explores the build-in R dataset "mtcars". In this tutorial you
# will expand this app.
#
# Run the app by either using the runApp() command or by hitting "Run App" in the
# upper right corner of the script editor. Explore the interactive behaviour of
# the app and how it is implemented codewise.
#
#
# ### 1st task ###
#
# Improve the layout of the tab "Scatter Plots" by moving the input elements into
# a side bar and the plot into the main panel. You can do this using the
# sidebarLayout function.
#
#
# ### 2nd task ###
#
# Add a check box to the sidebar panel that allows the user to add a regression
# line to the plot.
#
# Hint: You can add geom_smooth(method = "lm") to the ggplot object to add a
# regression line to the plot.
#
#
# ### 3rd task ###
#
# The "Data" tab is blank. Implement the following:
# The tab should display the mtcars data in a table. The sidebar panel should allow
# user to filter the data for specific levels of the variables cyl and gear (e.g.
# drop down menus or radio buttons).
#
#
###Setup
if(!require("shiny")) install.packages("shiny")
if(!require("ggplot2")) install.packages("ggplot2")
if(!require("dplyr")) install.packages("dplyr")
if(!require("shinythemes")) install.packages("shinythemes")
###### Shiny App ################################
### UI
ui <- fluidPage(
theme = shinytheme(theme = "sandstone"),
titlePanel("mtcars Shiny Tutorial"),
#Github link
tags$a(href = "https://github.com/meiradania/shinyApp",
"Click here to see the R source code", target = "_blank"),
tabsetPanel(
tabPanel("Scatter Plots",
sidebarLayout(
sidebarPanel(
#Select axes variables
selectInput(inputId = "yvar", label = "y Axis",
choices = c("mpg", "disp", "hp", "drat", "wt", "qsec"),
selected = "mpg"),
selectInput(inputId = "xvar", label = "x Axis",
choices = c("mpg", "disp", "hp", "drat", "wt", "qsec"),
selected = "disp"),
#Check box that allows the user to add a regression line to the plot.
checkboxInput(inputId = "regressionline", label = "Regression Line")
),
mainPanel(
#Scatter plot output
plotOutput(outputId = "scatter", height = "400px")
)
)
),
tabPanel("Data",
sidebarLayout(
sidebarPanel(
# cyl filter
selectInput(inputId = "filtercyl", label = "Filter - Number of cylinders:",
choices = unique(mtcars$cyl), selected = 4),
# gear filter
selectInput(inputId = "filtergear", label = "Filter - Number of forward gears:",
choices = unique(mtcars$gear), selected = 4),
# Button
actionButton(inputId = "button", label = "Apply filters"),
actionButton(inputId = "buttonReset", label = "Reset filters")
),
mainPanel(
dataTableOutput(outputId = "data")
)
)
)
),
#Source link
tags$a(href = "https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html",
"Source: Henderson and Velleman (1981)", target = "_blank")
)
### Server function
server <- function(input, output) {
#Create scatterplot
output$scatter <- renderPlot({
if(input$regressionline)
plot <- ggplot(mtcars, aes_(y = as.name(input$yvar), x = as.name(input$xvar))) +
geom_point(size = 2.5) + geom_smooth(method = "lm")
else
plot <- ggplot(mtcars, aes_(y = as.name(input$yvar), x = as.name(input$xvar))) +
geom_point(size = 2.5)
plot
})
#Add data table code here
output$data <- renderDataTable(mtcars)
observeEvent(input$button,
{
valuecyl <- input$filtercyl
valuegear <- input$filtergear
output$data <- renderDataTable(mtcars[mtcars$cyl == valuecyl
& mtcars$gear == valuegear ,])
})
observeEvent(input$buttonReset,
output$data <- renderDataTable(mtcars))
}
### Create Shiny object
shinyApp(ui = ui, server = server)