-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
131 lines (95 loc) · 3.28 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
library(shiny)
library(shinythemes)
library(DBI)
library(RSQLite)
library(glue)
# Import custom functions we created
source("funs.R")
# UI ----------------------------------------------------------------------
ui <- shiny::navbarPage(
title = "Boston useR Meetup",
theme = shinythemes::shinytheme(theme = "darkly"),
inverse = TRUE,
collapsible = TRUE,
# Create the "Home" tab on the app
shiny::tabPanel(
title = "Home",
shiny::fluidRow(
# Add text input widget to capture the beer name entered by the user
shiny::column(
width = 4,
shiny::textInput(
inputId = "beer_name",
label = "Please Enter the Name of Your Favorite Beer",
placeholder = "Type Beer Name Here"
)
),
# Add numeric slider input widget to capture the ABV of the user's
# favorite beer
shiny::column(
width = 4,
shiny::sliderInput(
inputId = "abv_value",
label = "Please Provide the ABV% of Your Favorite Beer",
value = 6.5, # default value slider starts at
min = 0.1, # minimum allowable value
max = 15, # maximum allowable value
step = 0.1 # slider increment
)
),
# Add radio button widget to capture the user's choice of cat or dog
shiny::column(
width = 4,
shiny::radioButtons(
inputId = "cat_or_dog",
label = "Are You More of a Cat or Dog Person?",
choices = c(emo::ji("cat"), emo::ji("dog"))
)
)
),
shiny::hr(),
# Add a button for the user to save their choices
shiny::fluidRow(
shiny::column(
width = 4,
shiny::actionButton(
inputId = "save_btn",
label = "Save New Record"
)
)
)
)
)
# Server ------------------------------------------------------------------
server <- function(input, output, session) {
# When the user clicks on the "Save New Record" button...
shiny::observeEvent(input$save_btn, {
# Require that they entered at least 1 character of text for the beer name
shiny::req(nchar(input$beer_name) > 0)
# Create a folder in the current working directory called "database" (if it
# doesn't already exist)
fs::dir_create("database")
# Create a SQLite database named "db-main.sqlite", and create a connection
# to that database;
# Note: this will also connect to the SQLite database if it already exists
# (won't overwrite an existing database named "db-main.sqlite")
con <- DBI::dbConnect(
drv = RSQLite::SQLite(),
"database/db-main.sqlite"
)
# Add the "AppHistoryTable" to the SQLite database if it doesn't already
# exist, using our custom `make_db()` function
make_db(sql_con = con)
# Save the user's inputs to the corresponding columns in the SQLite database,
# as defined by our custom `save_to_db()` function
save_to_db(
sql_con = con,
beer = input$beer_name,
abv = input$abv_value,
cat_dog_choice = input$cat_or_dog
)
# Close the connection to teh SQLite database when finished
DBI::dbDisconnect(conn = con)
})
}
shiny::shinyApp(ui = ui, server = server)