Skip to content

Commit 4e8749d

Browse files
authored
Merge pull request #532 from remlapmot/v0-6-6-before-and-and
TwoSampleMR 0.6.6
2 parents 2f0736e + e9aad60 commit 4e8749d

35 files changed

+225
-173
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ cache$
1717
^README\.Rmd$
1818
^CITATION\.cff$
1919
^README\.html$
20+
^\.lintr$

.github/workflows/check-full.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3+
#
4+
# NOTE: This workflow is overkill for most R packages and
5+
# check-standard.yaml is likely a better choice.
6+
# usethis::use_github_action("check-standard") will install it.
17
on:
28
push:
39
branches: [main, master]
@@ -9,6 +15,8 @@ on:
915

1016
name: R-CMD-check
1117

18+
permissions: read-all
19+
1220
jobs:
1321
R-CMD-check:
1422
runs-on: ${{ matrix.config.os }}
@@ -57,5 +65,5 @@ jobs:
5765
- uses: r-lib/actions/check-r-package@v2
5866
with:
5967
upload-snapshots: true
60-
args: 'c("--no-manual", "--as-cran")'
68+
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
6169
error-on: '"error"'

.github/workflows/pkgdown.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ on:
1111

1212
name: pkgdown
1313

14+
permissions: read-all
15+
1416
jobs:
1517
pkgdown:
1618
runs-on: ubuntu-latest

.github/workflows/test-coverage.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
13
on:
24
push:
35
branches: [main, master]
@@ -7,6 +9,8 @@ on:
79

810
name: test-coverage
911

12+
permissions: read-all
13+
1014
jobs:
1115
test-coverage:
1216
runs-on: macOS-latest

.lintr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
linters: linters_with_defaults(
2+
line_length_linter = NULL,
3+
commented_code_linter = NULL,
4+
indentation_linter = NULL,
5+
trailing_whitespace_linter = NULL,
6+
infix_spaces_linter = NULL,
7+
quotes_linter = NULL,
8+
trailing_blank_lines_linter = NULL,
9+
brace_linter = NULL,
10+
commas_linter = NULL,
11+
whitespace_linter = NULL,
12+
object_name_linter = NULL,
13+
assignment_linter = NULL,
14+
cyclocomp_linter = NULL
15+
)
16+
encoding: "UTF-8"

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: TwoSampleMR
22
Title: Two Sample MR Functions and Interface to MR Base Database
3-
Version: 0.6.5
3+
Version: 0.6.6
44
Authors@R: c(
55
person("Gibran", "Hemani", , "g.hemani@bristol.ac.uk", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-0920-1055")),
@@ -36,7 +36,7 @@ Imports:
3636
glmnet,
3737
gridExtra,
3838
gtable,
39-
ieugwasr (>= 1.0.0),
39+
ieugwasr (>= 1.0.1),
4040
jsonlite,
4141
knitr,
4242
lattice,
@@ -59,7 +59,8 @@ Suggests:
5959
MendelianRandomization,
6060
MRInstruments,
6161
randomForest,
62-
testthat
62+
testthat,
63+
tidyr
6364
VignetteBuilder:
6465
knitr
6566
Remotes:

NEWS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# TwoSampleMR v0.6.6
2+
3+
(Release date 2024-07-06)
4+
5+
* Improve a test
6+
* Improve permissions in GitHub Actions workflows
7+
* Bump minimum required version of **ieugwasr** to 1.0.1
8+
* Made some amends to the code to bring it more in line with **lintr** recommendations
9+
* Added omitted **tidyr** soft dependency
10+
111
# TwoSampleMR v0.6.5
212

313
(Release date: 2024-06-30)

R/add_metadata.r

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ add_metadata <- function(dat, cols = c("sample_size", "ncase", "ncontrol", "unit
1515
get_info <- function(id, what="exposure", cols)
1616
{
1717
info <- ieugwasr::gwasinfo(id)
18-
if(nrow(info) == 0)
18+
if (nrow(info) == 0)
1919
{
2020
message(what, ": none of the IDs found in database")
2121
return(NULL)
2222
}
23-
for(col in cols)
23+
for (col in cols)
2424
{
25-
if(!col %in% names(info))
25+
if (!col %in% names(info))
2626
{
2727
info[[col]] <- NA
2828
}
2929
}
3030
info <- subset(info, select=c("id", cols))
3131
names(info) <- paste0(names(info), ".", what)
3232
names(info)[names(info) == paste0("sample_size.", what)] <- paste0("samplesize.", what)
33-
if("sample_size" %in% cols)
33+
if ("sample_size" %in% cols)
3434
{
3535
index <- grepl("ukb-d", info$id) & is.na(info[[paste0("samplesize.", what)]])
3636
info[[paste0("samplesize.", what)]][index] <- 300000
@@ -39,42 +39,42 @@ add_metadata <- function(dat, cols = c("sample_size", "ncase", "ncontrol", "unit
3939
}
4040

4141
order_col <- random_string()
42-
dat[[order_col]] <- 1:nrow(dat)
43-
if("id.exposure" %in% names(dat))
42+
dat[[order_col]] <- seq_len(nrow(dat))
43+
if ("id.exposure" %in% names(dat))
4444
{
4545
exposure_id <- unique(dat[["id.exposure"]])
4646
info <- get_info(id=exposure_id, what="exposure", cols=cols)
47-
if(!is.null(info))
47+
if (!is.null(info))
4848
{
49-
for(x in names(info))
49+
for (x in names(info))
5050
{
51-
if(! x %in% names(dat))
51+
if (! x %in% names(dat))
5252
{
5353
dat[[x]] <- NA
5454
}
5555

56-
for(id in unique(info[["id.exposure"]]))
56+
for (id in unique(info[["id.exposure"]]))
5757
{
5858
dat[[x]][is.na(dat[[x]]) & dat[["id.exposure"]] == id] <- info[[x]][info[["id.exposure"]] == id]
5959
}
6060
}
6161
}
6262
}
6363

64-
if("id.outcome" %in% names(dat))
64+
if ("id.outcome" %in% names(dat))
6565
{
6666
outcome_id <- unique(dat[["id.outcome"]])
6767
info <- get_info(id=outcome_id, what="outcome", cols=cols)
68-
if(!is.null(info))
68+
if (!is.null(info))
6969
{
70-
for(x in names(info))
70+
for (x in names(info))
7171
{
72-
if(! x %in% names(dat))
72+
if (! x %in% names(dat))
7373
{
7474
dat[[x]] <- NA
7575
}
7676

77-
for(id in unique(info[["id.outcome"]]))
77+
for (id in unique(info[["id.outcome"]]))
7878
{
7979
dat[[x]][is.na(dat[[x]]) & dat[["id.outcome"]] == id] <- info[[x]][info[["id.outcome"]] == id]
8080
}

R/add_rsq.r

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ add_rsq_one <- function(dat, what="exposure")
6868
} else {
6969
message("Try adding metadata with add_metadata()")
7070
}
71-
} else if(all(grepl("SD", dat[[paste0("units.", what)]])) & all(!is.na(dat[[paste0("eaf.", what)]]))) {
71+
} else if(all(grepl("SD", dat[[paste0("units.", what)]])) && all(!is.na(dat[[paste0("eaf.", what)]]))) {
7272
dat[[paste0("rsq.", what)]] <- NA
7373
dat[[paste0("rsq.", what)]] <- 2 * dat[[paste0("beta.", what)]]^2 * dat[[paste0("eaf.", what)]] * (1-dat[[paste0("eaf.", what)]])
7474
dat[[paste0("effective_n.", what)]] <- dat[[paste0("samplesize.", what)]]
@@ -109,12 +109,19 @@ get_r_from_pn_less_accurate <- function(p, n)
109109

110110
test_r_from_pn <- function()
111111
{
112+
if (!requireNamespace("tidyr", quietly = TRUE)) {
113+
stop(
114+
"Package \"tidyr\" must be installed to use this function.",
115+
call. = FALSE
116+
)
117+
}
118+
112119
param <- expand.grid(
113120
n = c(10, 100, 1000, 10000, 100000),
114121
rsq = 10^seq(-4,-0.5, length.out=30)
115122
)
116123

117-
for(i in 1:nrow(param))
124+
for(i in seq_len(nrow(param)))
118125
{
119126
message(i)
120127
x <- scale(stats::rnorm(param$n[i]))
@@ -125,7 +132,7 @@ test_r_from_pn <- function()
125132
param$rsq2[i] <- get_r_from_pn(param$pval[i], param$n[i])^2
126133
}
127134

128-
param <- gather(param, key=out, value=value, rsq1, rsq2)
135+
param <- tidyr::gather(param, key=out, value=value, rsq1, rsq2)
129136

130137
p <- ggplot2::ggplot(param, ggplot2::aes(x=rsq_emp, value)) +
131138
ggplot2::geom_abline(slope=1, linetype="dotted") +
@@ -170,7 +177,7 @@ get_r_from_pn <- function(p, n)
170177
abs(-log10(suppressWarnings(get_p_from_r2n(x, sample_size))) - -log10(pvalue))
171178
}
172179

173-
if(length(p) > 1 & length(n) == 1)
180+
if(length(p) > 1 && length(n) == 1)
174181
{
175182
message("Assuming n the same for all p values")
176183
n <- rep(n, length(p))
@@ -182,7 +189,7 @@ get_r_from_pn <- function(p, n)
182189
if(any(index))
183190
{
184191
index <- which(index)
185-
for(i in 1:length(index))
192+
for(i in seq_along(index))
186193
{
187194
if(p[index[i]] == 0)
188195
{
@@ -240,15 +247,15 @@ get_r_from_lor <- function(lor, af, ncase, ncontrol, prevalence, model="logit",
240247
stopifnot(length(ncase) == 1 | length(ncase) == length(lor))
241248
stopifnot(length(ncontrol) == 1 | length(ncontrol) == length(lor))
242249
stopifnot(length(prevalence) == 1 | length(prevalence) == length(lor))
243-
if(length(prevalence) == 1 & length(lor) != 1)
250+
if(length(prevalence) == 1 && length(lor) != 1)
244251
{
245252
prevalence <- rep(prevalence, length(lor))
246253
}
247-
if(length(ncase) == 1 & length(lor) != 1)
254+
if(length(ncase) == 1 && length(lor) != 1)
248255
{
249256
ncase <- rep(ncase, length(lor))
250257
}
251-
if(length(ncontrol) == 1 & length(lor) != 1)
258+
if(length(ncontrol) == 1 && length(lor) != 1)
252259
{
253260
ncontrol <- rep(ncontrol, length(lor))
254261
}
@@ -340,7 +347,7 @@ get_population_allele_frequency <- function(af, prop, odds_ratio, prevalence)
340347
{
341348
stopifnot(length(af) == length(odds_ratio))
342349
stopifnot(length(prop) == length(odds_ratio))
343-
for(i in 1:length(odds_ratio))
350+
for(i in seq_along(odds_ratio))
344351
{
345352
co <- contingency(af[i], prop[i], odds_ratio[i])
346353
af_controls <- co[1,2] / (co[1,2] + co[2,2])

R/forest_plot.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ mr_forest_plot_grouped <-
162162
data_Fm$eff_col <- log(as.numeric(data_Fm[,eff_col]))
163163
}
164164
# ggplot code to generate the forest plot using geom_segments and geom_points and to make a relatively minimal theme
165-
raw_forest <- ggplot(data = data_Fm, aes( y = space_col, yend = space_col, x = as.numeric(lb_col), xend = as.numeric(ub_col) )) + geom_segment() + geom_point(aes( y = space_col, x = as.numeric(eff_col), size = 4 )) + theme_bw() + theme( axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.title = element_blank(), panel.grid = element_blank(), rect = element_blank(), title = element_text(size = 23), legend.position = 'none' ) + expand_limits(y = c(data_Fm[,space_col] - 1, data_Fm[,space_col] + 2)) + labs(title = title_text) # returns ggplot2 object with the (un-annotated) forest plot
165+
raw_forest <- ggplot2::ggplot(data = data_Fm, ggplot2::aes( y = space_col, yend = space_col, x = as.numeric(lb_col), xend = as.numeric(ub_col) )) + ggplot2::geom_segment() + ggplot2::geom_point(ggplot2::aes( y = space_col, x = as.numeric(eff_col), size = 4 )) + ggplot2::theme_bw() + ggplot2::theme( axis.text.y = ggplot2::element_blank(), axis.ticks.y = ggplot2::element_blank(), axis.title = ggplot2::element_blank(), panel.grid = ggplot2::element_blank(), rect = ggplot2::element_blank(), title = ggplot2::element_text(size = 23), legend.position = 'none') + ggplot2::expand_limits(y = c(data_Fm[,space_col] - 1, data_Fm[,space_col] + 2)) + ggplot2::labs(title = title_text) # returns ggplot2 object with the (un-annotated) forest plot
166166
return(raw_forest)
167167
}
168168

@@ -183,7 +183,7 @@ mr_forest_plot_grouped <-
183183
text_widths <- c(-1, max(10,0.5 * max(sapply( as.character(data_Fm[,text_col]),nchar ))))
184184

185185
# GGplot rendering of the annotation column
186-
lefttext <- ggplot(data = data_Fm, aes( y = space_col, x = 0, label = text_col, fontface = attr_list )) + geom_text(hjust = 0) + theme_bw() + theme( axis.text.y = element_blank(), axis.ticks.y = element_blank(),axis.text.x = element_text(colour = "white"),axis.ticks.x = element_line(colour = "white"), axis.title = element_blank(), rect = element_blank(), panel.grid = element_blank(), title = element_text(size = 23) ) + expand_limits(x = text_widths, y = c(data_Fm[,space_col] - 1, data_Fm[,space_col] + 2)) + labs(title = title_text, size = 40) # returns two-item list with left_text, the GGplot annotations, and text_widths, the x-axis limits of the plot
186+
lefttext <- ggplot2::ggplot(data = data_Fm, ggplot2::aes( y = space_col, x = 0, label = text_col, fontface = attr_list )) + ggplot2::geom_text(hjust = 0) + ggplot2::theme_bw() + ggplot2::theme( axis.text.y = ggplot2::element_blank(), axis.ticks.y = ggplot2::element_blank(),axis.text.x = ggplot2::element_text(colour = "white"),axis.ticks.x = ggplot2::element_line(colour = "white"), axis.title = ggplot2::element_blank(), rect = ggplot2::element_blank(), panel.grid = ggplot2::element_blank(), title = ggplot2::element_text(size = 23) ) + ggplot2::expand_limits(x = text_widths, y = c(data_Fm[,space_col] - 1, data_Fm[,space_col] + 2)) + ggplot2::labs(title = title_text, size = 40) # returns two-item list with left_text, the GGplot annotations, and text_widths, the x-axis limits of the plot
187187
return(list(left_text = lefttext, text_widths = text_widths))
188188
}
189189

@@ -201,7 +201,7 @@ mr_forest_plot_grouped <-
201201
title_list <- col_names
202202
}
203203

204-
for (i in 1:length(col_names)) {
204+
for (i in seq_along(col_names)) {
205205
# loop to get the widths of each annotation column and to group the annotation objects together
206206
col <- anot_col( data_Fm = data_Fm, text_col = col_names[i], space_col = space_col, title_text = title_list[[i]] )
207207
relative_widths[i] <- col$text_widths[2] - col$text_widths[1]
@@ -230,7 +230,7 @@ mr_forest_plot_grouped <-
230230
left_Grobs <- left_Hs
231231
left_Grobs$relative_widths <- NULL
232232

233-
for (i in 1:length(left_Grobs)) {
233+
for (i in seq_along(left_Grobs)) {
234234
grob_Bag[paste('l',names(left_Grobs)[i],sep = '')] <- left_Grobs[i]
235235
}
236236

@@ -239,7 +239,7 @@ mr_forest_plot_grouped <-
239239
right_RW <- right_Hs$relative_widths
240240
right_Grobs <- right_Hs
241241
right_Grobs$relative_widths <- NULL
242-
for (i in 1:length(right_Grobs)) {
242+
for (i in seq_along(right_Grobs)) {
243243
grob_Bag[paste('r',names(right_Grobs)[i], sep = '')] <- right_Grobs[i]
244244
}
245245

0 commit comments

Comments
 (0)