-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
95 lines (71 loc) · 1.79 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
library(shiny)
library(shinydashboard)
library(dplyr)
# dput(head(mtcars)[1:3, 1:3])
df <-
structure(
list(
mpg = c(21, 21, NA),
cyl = c(6, NA, 4),
disp = c(NA, 160, 108)
),
.Names = c("mpg", "cyl", "disp"),
row.names = c("Mazda RX4",
"Mazda RX4 Wag", "Datsun 710"),
class = "data.frame"
)
join_df <- function(df) {
# stopifnot(!is.data.frame(df))
df <- df %>%
na.omit() %>%
mutate(id = rownames(df))
to_join <- head(mtcars)[1:3, 4:6]
to_join <- to_join %>%
mutate(id = rownames(to_join))
df %>%
dplyr::left_join(to_join, by = "id")
}
header <- dashboardHeader(
title = "Example Shiny app"
)
body <- dashboardBody(
fluidRow(
column(width = 9,
box(width = NULL, solidHeader = TRUE,
textInput("text1", "Enter text:", value = "", placeholder = "letters only"),
actionButton("start", "Start"),
textOutput("output_text1")
),
box(width = NULL,
actionButton("process_df_btn", "Process 'mtcars' DF")
)
)
)
)
ui <- dashboardPage(
header,
dashboardSidebar(disable = TRUE),
body
)
server <- function(input, output, session) {
observeEvent(input$start, {
txt <- req(input$text1)
# letters only
req(nchar(gsub("[a-zA-Z]", "", txt)) == 0)
output$output_text1 <- renderText(sprintf("Entered text: %s", txt))
})
observeEvent(input$process_df_btn, {
# nicely handle error without crashing the app
tryCatch({
join_df(df)
}, error = function(e) {
showModal(modalDialog(
title = "Error occured!",
e
))
req(FALSE)
})
print("...do something else...")
})
}
shinyApp(ui = ui, server = server)