-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture12.R
141 lines (118 loc) · 2.93 KB
/
lecture12.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
library(usethis)
library(dplyr)
library(tidyr)
library(dbplyr)
library(magrittr)
library(ggplot2)
library(knitr)
library(devtools)
library(tidyverse)
library(pacman)
library(ggthemes)
library(gdata)
library(griffen)
library(hrbrthemes)
library(gt)
library(gtExtras)
library(webshot2)
library(broom)
p_load(gapminder,ggthemes,Hmisc,wesanderson,ggridges)
p_load(shiny,shinythemes,bslib,showtext,thematic)
library(palmerpenguins)
##numeric#######
ui <- fluidPage(
# front end interface
numericInput("N", label = "N", value = 100, step = 50),
textOutput("double")
)
server <- function(input, output, session){
output$double <- renderPrint(2*input$N)
}
shinyApp(ui, server)
##slider#####
ui <- fluidPage(
# front end interface
sliderInput("N", label = "N", value = 50, min = 0, max = 100),
textOutput("double")
)
server <- function(input, output, session){
output$double <- renderPrint(2*input$N)
}
shinyApp(ui, server)
##reactivity#####
ui <- fluidPage(
# front end interface
sliderInput("N", label = "N", value = 50, min = 0, max = 1000),
plotOutput("density")
)
server <- function(input, output, session){
output$density <- renderPlot({
x = rnorm(input$N)
df = tibble(x)
ggplot(df, aes(x)) + geom_density()
})
}
shinyApp(ui, server)
#######
ui <- fluidPage(
textInput("name", "What's your name?"),
numericInput("age", "How old are you?", value = 0, min = 0, max = 100),
textOutput("greeting")
)
server <- function(input, output, session){
output$greeting <- renderText({
paste0("Hello ", input$name, "!", " Congratulations on being ", input$age)
})
}
shinyApp(ui, server)
#######
ui <- fluidPage(
titlePanel("Palmer Penguins"),
sidebarLayout(
sidebarPanel(
selectInput("island", label = "Island", choices = c("Torgersen", "Biscoe", "Dream"))
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output, session){
#gets input on island
data <- reactive({
dplyr::filter(penguins, island==input$island)
})
output$plot <- renderPlot({
ggplot(data(), aes(bill_length_mm, body_mass_g)) +
geom_point() +
labs(x = "Bill length (mm)", y = "Body mass (g)")
})
}
penguins_app <- shinyApp(ui, server)
#######
ui <- fluidPage(
titlePanel("Gapminder Data"),
sidebarLayout(
sidebarPanel(
selectInput("continent", label = "Continent", choices = c("Asia", "Americas", "Europe", "Oceania", "Africa"))
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output, session){
data <- reactive({
dplyr::filter(gapminder, continent==input$continent)
})
output$plot <- renderPlot({
ggplot(data <- data(),
mapping = aes(x =log(gdpPercap), y = lifeExp, color = country)
) +
geom_point(aes(size = pop), show.legend = FALSE) +
labs(x = "log GDP per cap", y = "Life Expectancy") +
scale_color_manual(values = country_colors)
})
}
gapminder_app <- shinyApp(ui, server)
runApp((gapminder_app))