-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c74df73
commit 3e24c0f
Showing
9 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
_R_CHECK_LENGTH_1_LOGIC2_=verbose | ||
_R_CHECK_LENGTH_1_CONDITION_=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function add_2(x,y) | ||
x + y | ||
end | ||
|
||
|
||
function add_3(x,y,z) | ||
x + y + z | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
function fastest_2_Julia_fn(travel_times) | ||
sorted = mapslices(sortperm, travel_times, dims=1) | ||
sorted[1:2,:] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#' .. content for \description{} (no empty lines) .. | ||
#' | ||
#' .. content for \details{} .. | ||
#' | ||
#' @title | ||
#' @param travel_time_mat | ||
#' @return | ||
#' @author Miles McBain | ||
#' @export | ||
fastest_2_R_fn <- function(travel_times) { | ||
|
||
apply( | ||
travel_times, | ||
2, | ||
function(times) { | ||
result <- sort.int(times, index.return = TRUE) | ||
list( | ||
time = result$x[1:2], | ||
index = result$ix[1:2] | ||
) | ||
} | ||
) | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#' .. content for \description{} (no empty lines) .. | ||
#' | ||
#' .. content for \details{} .. | ||
#' | ||
#' @title | ||
#' @param n_incidents | ||
#' @param n_locations | ||
#' @param n_stations | ||
#' @return | ||
#' @author Miles McBain | ||
#' @export | ||
make_travel_time_mat <- function(n_incidents, n_locations, n_stations) { | ||
|
||
times <- sample.int(20 * 60, n_incidents * (n_locations + n_stations), replace = TRUE) | ||
dim(times) <- c(n_stations + n_locations, n_incidents) | ||
times | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
tar_julia <- function(name, command, ...) { | ||
|
||
command_expr <- substitute(command) | ||
if (!inherits(command_expr, "call")) stop("I only accept Julia function calls for now.") | ||
function_name <- deparse(command_expr[[1]]) | ||
function_args <- as.list(command_expr)[-1] | ||
methods_result <- | ||
JuliaCall::julia_eval(glue::glue("methods({function_name}) |> (x -> map(string, x))")) | ||
if (length(methods_result) == 0) { | ||
stop( | ||
"Could not find Julia function matching your expression:", | ||
deparse(command_expr) | ||
) | ||
} | ||
function_paths <- | ||
parse_julia_paths(methods_result) |> | ||
unique() | ||
|
||
local_function_path <- | ||
Filter(in_wd, function_paths) | ||
|
||
if (length(local_function_path) != 1) { | ||
stop( | ||
"Julia function matched in more than one local source file:", | ||
paste(local_function_path) | ||
) | ||
} | ||
file_target_name <- glue::glue("{function_name}_source") | ||
file_target <- targets::tar_target_raw( | ||
name = file_target_name, | ||
command = local_function_path | ||
) | ||
command <- | ||
as.call(c( | ||
quote(julia_eval_source), | ||
function_name, | ||
as.name(file_target_name), | ||
function_args | ||
)) | ||
command_target <- targets::tar_target_raw( | ||
name = glue::glue("{function_name}_exec"), | ||
command = command, | ||
... | ||
) | ||
|
||
list( | ||
file_target, | ||
command_target | ||
) | ||
} | ||
|
||
load_julia_sources <- function() { | ||
julia_files <- | ||
list.files( | ||
julia_source_path(), | ||
full.names = TRUE | ||
) | ||
JuliaCall::julia_call("include.", julia_files) | ||
} | ||
|
||
`%|s|%` <- function(a, b) { | ||
if (!(rlang::is_scalar_character(a) && nzchar(a))) b else a | ||
} | ||
|
||
julia_source_path <- function() Sys.getenv("JULIA_TARGET_SOURCES") %|s|% "Julia" | ||
|
||
parse_julia_paths <- function(methods_result) { | ||
file_path <- gregexec("at\\s(?<path>.*)(?=:[0-9]+)", methods_result, perl = TRUE) | ||
regmatches(methods_result, file_path) |> | ||
lapply(function(x) x["path", ]) |> | ||
unlist() |> | ||
unname() | ||
} | ||
|
||
julia_eval_source <- function(fn, path, ...) { | ||
JuliaCall::julia_call(fn, ...) | ||
} | ||
|
||
in_wd <- function(x) fs::path_common(c(x, fs::path_wd())) == fs::path_wd() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## Load your packages, e.g. library(targets). | ||
source("./packages.R") | ||
|
||
## Load your Julia libraries | ||
JuliaCall::julia_call("include", "julia_libraries.jl") | ||
|
||
## Load your R files | ||
lapply(list.files("./R", full.names = TRUE), source) | ||
|
||
## Load your Julia files | ||
load_julia_sources() | ||
|
||
## tar_plan supports drake-style targets and also tar_target() | ||
tar_plan( | ||
# target = function_to_make(arg), ## drake style | ||
n_incidents = 10000, | ||
n_locations = 1000, | ||
n_stations = 10, | ||
travel_times = make_travel_time_mat( | ||
n_incidents, | ||
n_locations, | ||
n_stations | ||
), | ||
fastest_2_R = fastest_2_R_fn( | ||
travel_times | ||
), | ||
tar_julia( | ||
fastest_2_Julia, | ||
fastest_2_Julia_fn(travel_times) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
using Distributions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## library() calls go here | ||
library(conflicted) | ||
library(dotenv) | ||
library(targets) | ||
library(tarchetypes) |