Skip to content

Commit 9dbffcf

Browse files
committed
Adress codex review
1 parent ae93bbe commit 9dbffcf

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

R/r2f-matrix.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,12 +632,21 @@ register_r2f_handler(
632632
"diag",
633633
function(args, scope, ..., hoist = NULL, dest = NULL) {
634634
arg_names <- names(args)
635-
has_first <- length(args) >= 1L
636-
first_named <- has_first &&
637-
length(arg_names) >= 1L &&
638-
nzchar(arg_names[[1L]])
639-
first_is_dim <- first_named && arg_names[[1L]] %in% c("nrow", "ncol")
640-
x_arg <- if (!has_first || first_is_dim) NULL else args$x %||% args[[1L]]
635+
if (is.null(arg_names)) {
636+
arg_names <- rep("", length(args))
637+
}
638+
x_arg <- NULL
639+
if (!is.null(args$x) && !is_missing(args$x)) {
640+
x_arg <- args$x
641+
} else if (length(args) >= 1L) {
642+
unnamed_idx <- which(!nzchar(arg_names) | is.na(arg_names))
643+
if (length(unnamed_idx) >= 1L) {
644+
candidate <- args[[unnamed_idx[[1L]]]]
645+
if (!is_missing(candidate)) {
646+
x_arg <- candidate
647+
}
648+
}
649+
}
641650
nrow_arg <- args$nrow
642651
ncol_arg <- args$ncol
643652
if (!is.null(args$names) && !is_missing(args$names)) {

tests/testthat/test-matrix-lapack.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ test_that("diag matches R for vectors, matrices, and sizes", {
117117
diag(x, nrow = 2, ncol = 3)
118118
}
119119

120+
diag_named_order <- function(x) {
121+
declare(type(x = double(n)))
122+
diag(nrow = 3L, x = x)
123+
}
124+
120125
set.seed(3)
121126
x <- rnorm(4)
122127
A <- matrix(rnorm(2 * 3), nrow = 2)
@@ -125,6 +130,7 @@ test_that("diag matches R for vectors, matrices, and sizes", {
125130
expect_quick_equal(diag_mat, list(A = A))
126131
expect_quick_equal(diag_size, list())
127132
expect_quick_equal(diag_value, list(x = 2.5))
133+
expect_quick_equal(diag_named_order, list(x = c(1, 2)))
128134
})
129135

130136
test_that("diag handles missing x with nrow/ncol and 1x1 matrices", {

0 commit comments

Comments
 (0)