-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions shiny.qmd
101 lines (93 loc) · 3.23 KB
/
interactions shiny.qmd
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
---
title: "Faire beau, utile et moderne"
subtitle: "Des graphiques interactifs dans les documents html ou les présentations"
date: 12/5/2022
author: Xavier Timbeau
institute: "*OFCE/Sciences Po*"
lang: fr
title-block-banner: true
server: shiny
format:
html:
theme: simple
code-fold: true
logo: ofce.png
editor: visual
---
::: {style="color: gray; font-size: 14pt"}
sources: eikon&Datastream
:::
## + interaction avancée avec shiny
```{r, echo=TRUE, message=FALSE, warning=FALSE}
library(shiny)
library(tidyverse)
library(plotly)
library(ofce)
library(ggh4x)
load("data/gcom.rdata")
selectInput(
"commodity",
"Commodity",
choices = CMOr |> filter(type!="unknown") |> pull(variable) |> unique(),
selected = c("Natural gas, Europe", "Crude oil, Brent", "Sunflower oil"),
multiple = TRUE)
plotlyOutput("comPlot")
```
```{r}
#| context: server
library(shiny)
library(tidyverse)
library(plotly)
library(ofce)
library(ggh4x)
cocol <- qs::qread("data/coicops.qs") |> pull(color, name = coicop)
gcom <- qs::qread("data/gcom.qs")
load("data/gcom.rdata")
CMOr <- CMOr |> mutate(
text = glue::glue(str_c(
"{variable}",
"obs. on: {format(time, format = '%B %Y')}",
"real price index (2018=100): {round(pcom_real,1)}",
"nominal price {prettyNum(epcom, digits=3, format='fg', big.mark = ',')} {eunit}", sep="<br>")))
output$comPlot <- renderPlotly({
g <- ggplot(CMOr |> filter(variable %in% input$commodity))+
geom_line(aes(x=time, y=pcom_real,
color=type, group=variable,
text=text),
show.legend = FALSE, size=0.25)+
scale_y_log10(breaks = scales::log_breaks(10),
minor_breaks = scales::log_breaks(16),
guide = "axis_minor")+
# geom_text(data=CMOd |> filter(type!="unknown"),
# aes(x=ymd("1960-04-01"), y=22, label=last_text, color= type),
# hjust=0, vjust=1, size=7/.pt, show.legend=FALSE)+
scale_color_manual(values = c(food = cocol[["CP01"]],
nrg = cocol[["CP07"]],
fer = cocol[["CP05"]])) +
xlab(NULL)+ylab(NULL)+
facet_wrap(vars(variable), ncol = 1)+
labs(
title = NULL,
subtitle = NULL,
caption =
str_c(
"Last data point for {ld_str}, real prices using France price and exhange rate indexes, 2018=100, log 10 scale" |> glue::glue(),
"Sources : World Bank Commodity Markets (www.worldbank.org/en/research/commodity-markets)",
"Eurostat HICP monthly since 1990, INSEE National accounts from 1960 to 1990 for price index (France)",
"OECD EO110 for exhange rate FRA/USD, EUR/USD",
sep="\n"))+
theme_ofce()+
scale_x_date(guide="axis_minor",
date_minor_breaks = "1 year",
date_labels = "%Y") +
theme(panel.spacing = unit(8, "pt"))
(g %+% theme(text = element_text(family = "arial", size = 9))) |>
plotly::ggplotly(tooltip = "text", dynamicTicks = TRUE,
width = 780, height = 300*length(input$commodity)) |>
layout(
yaxis = list(fixedrange=TRUE),
showlegend = FALSE,
xaxis = list(fixedrange=TRUE, rangeslider=list(visible=TRUE))) |>
config(displayModeBar=FALSE)
})
```