-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshiny_app_crop_check.R
83 lines (61 loc) · 2.47 KB
/
shiny_app_crop_check.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
## Shiny apps
## Author: Abel Gelman
library(tidyverse)
# downlaod data
drs_df_raw_link <- "https://raw.githubusercontent.com/CartONG/R_training/main/df19.csv?token=ALMJAS5FZNUMPT2YXW2HM23A4B6Z2"
df19 <- read_csv(drs_df_raw_link,
col_types = cols(.default = "c"))
df19s <- df19 %>%
select(BE, Country, Crop1, Crop1KG, Crop1HA) %>%
filter(!is.na(Crop1)) %>%
filter(BE == "Baseline")
df19s$Crop1KG <- as.numeric(df19s$Crop1KG)
df19s$Crop1HA <- as.numeric(df19s$Crop1HA)
df19s <- df19s %>%
mutate(Yield = Crop1KG/Crop1HA) %>%
filter(!is.na(Yield)) %>%
mutate(Check = Yield < 150)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("UNHCR Liveilhoods 2019 - Yield Data Quality Check"),
sidebarLayout(
sidebarPanel(
selectInput("country", "Select country",
choices= unique(df19s$Country)),
selectInput("crops", "Select crops",
choices = NULL),
sliderInput("bins",
"Histogram binwidth:",
min = 50,
max = 500,
value = 150),
hr(),
helpText("Use the binwidth slider to adjust the histogram")
),
mainPanel(
plotOutput("plot")
)
)
)
# Define server logic required to draw a histogram§
server <- function(session, input, output) {
observe({
print(input$country)
x <- df19s %>%
filter(Country == input$country) %>%
select(Crop1)
updateSelectInput(session, "crops", "Crops",
choices = unique(x))
})
output$plot <- renderPlot(ggplot(df19s, aes(x = Yield, fill = Check))+
geom_histogram(binwidth = input$bins,
data = df19s[df19s$Crop1 == input$crops,])+
scale_fill_manual(values = c("#0072BC","red4"))+
labs(title = paste(input$country, " - ", input$crops, " Yield Check"),
x = paste(input$crops, " yield (Kg/Ha)"),
y = "Number of observations")
)
}
# Run the application
shinyApp(ui = ui, server = server)