-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
163 lines (139 loc) · 6.43 KB
/
README.Rmd
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
output:
github_document:
html_preview: true
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.align = "center",
fig.show = "hold",
fig.retina = 2,
out.width = "70%",
#dev = "svg",
message = FALSE,
warning = FALSE
)
devtools::load_all()
```
```{r include=FALSE}
# a workaround to include htmlwidgets in github_document output format.
# This just generates the github-flavored markdown (.md) and then github
# generates on-the-fly the html from it. Thus htmlwidgets wont's work
# out-of-the-box. This solution is based on
# https://stackoverflow.com/questions/61253015/render-ggplotly-in-r-markdown-github-document
#
# yet, it will not work in github. Locally it works fine, but up there in
# github, the github flavored markdown spec disallows some html tags, including
# <iframe> and therefore it will just show the code and not actually render the
# content of the iframe target
# https://github.github.com/gfm/#disallowed-raw-html-extension-
# Then, just webshot the htmlwidgets and make them a static image
iframe_htmlwidget <- function(htmlwidget) {
html_path <- knitr::fig_path("html") # get the path like knitr would do
# TODO: should I ensure this is always a relative path?
# it seems it is, but can't find assurances
htmlwidgets::saveWidget(
widget = htmlwidget,
file = normalizePath(html_path), # full path, otherwise it'll complain
selfcontained = TRUE
# selfcontained = TRUE, but still the dependencies are copied to te figures
# dir so you need to exclude them via .gitignore to avoid commiting jquery,
# plotly, and all other libraries in version control
)
# If local preview, you want the iframe to point to an absolute path because
# "when you render from within RStudio using the Knit button the preview HTML
# is written to a temporary directory and then automatically displayed by
# RStudio.".
# https://rmarkdown.rstudio.com/github_document_format.html#HTML_Preview
#
# They handle the images well, but they are not aware of the
# iframe, and therefore, the saved htmlwidget will not be found.
# To overcome this, and absolute path in your system will do. However, that
# won't work when it's online, so before pushing this to github, you should
# set html_preview = FALSE and this little function will leave the iframe
# with a relative path
iframe_src_path <- ifelse(
test = isTRUE(rmarkdown::metadata$output$github_document$html_preview),
yes = normalizePath(html_path),
no = html_path
)
htmltools::tags$iframe(
src = iframe_src_path,
width = "70%",
height = "600",
scrolling = "no",
seamless = "seamless",
frameBorder = "0",
style = "display: block; margin: auto;"
)
# TODO: consider also not including assets in the html file but rather cdn links
# see for example this SO question for ideas
# https://stackoverflow.com/questions/51213463/how-to-skip-writing-dependencies-in-htmlwidgetssavewidget
# plot_html_widget$dependencies[[2]]$src$file <- NULL
# plot_html_widget$dependencies[[2]]$src$href <- "https://code.jquery.com/"s
}
# I guess this is another viable approach
# https://github.com/ropensci/plotly/issues/1620
# Basically use output: html_document: keep_md: true
# Need this little function to use webshot2
# Although plain webshot would be more convenient as rmarkdown would handle
# it automatically, webshot does not get along good with plotly
webshotme <- function(htmlwidget, ...) {
png_path <- knitr::fig_path("png") # get the path like knitr would do
html_path <- tempfile(fileext = ".html")
htmlwidgets::saveWidget(htmlwidget, file = html_path, selfcontained = TRUE)
webshot2::webshot(html_path, file = png_path, ...)
}
```
# wdiquickplots
<!-- badges: start -->
[![Codecov test coverage](https://codecov.io/gh/edalfon/wdiquickplots/branch/master/graph/badge.svg)](https://app.codecov.io/gh/edalfon/wdiquickplots?branch=master)
[![R-CMD-check](https://github.com/edalfon/wdiquickplots/workflows/R-CMD-check/badge.svg)](https://github.com/edalfon/wdiquickplots/actions)
<!-- badges: end -->
The goal of `wdiquickplots` is to provide, well, quick plots for World
Development Indicators (WDI, ["the primary World Bank collection of
development indicators, compiled from officially recognized international
sources."](https://databank.worldbank.org/home.aspx)). Just jump to the "Get
started" page if you want to take a look at all the quick plots examples. To
get WDI data, this package is powered by
[`WDI`](http://vincentarelbundock.github.io/WDI/) package, developed by Vincent
Arel-Bundock.s
## Installation
You can install it from this Github repo with:
```r
remotes::install_github("edalfon/wdiquickplots")
```
## How to use it
Use case: hey I have to present this study I have been working on in my home
country to an audience where I currently live (studying abroad or whatever).
Thus, some background data on my home country is in order. A table would
certainly do, but it is boring. So let's put some plots in there.
```{r dist, fig.asp=870/550, fig.width=4.5, out.width="47%", dev="svg"}
library(wdiquickplots)
plot_dist_wdi("NY.GDP.PCAP.PP.CD", p = 0)
```
There you go. That's the spirit of this package. One line of code and bang!,
a relatively decent plot that you can put in your slides to convey a quick
message.
Using this package goes as follows:
- Find the code of the indicator of interest. You can use `WDI::WDIsearch` for
this, but I actually find it a bit more user-friendly to simply go to the
[indicators page
(https://data.worldbank.org/indicator)](https://data.worldbank.org/indicator)
and get the code from there (it's in the URL).
- You pass the indicator code as the first argument of the different plotting
functions in this package.
- As second argument, you pass the countries you want to highlight.
And that's it.
You can read other details and description of features [in the `pkgdown` site
for this little package](https://edalfon.github.io/wdiquickplots/) (I know, a
`pkgdown` site may be overkill, but anyway). There you can also see examples
for all the quick plots in this package, but in general, they quickly show:
- Where the highlighted countries stand in terms of the indicator of interest.
- How do they compare among highlighted countries, and against the rest of the
world, regions or income groups.
- What have been the changes in time.