Skip to content

Commit

Permalink
first stab at POC
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesMcBain committed Sep 10, 2021
1 parent c74df73 commit 3e24c0f
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
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
8 changes: 8 additions & 0 deletions Julia/dummy_file.jl
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
4 changes: 4 additions & 0 deletions Julia/fastest_2_Julia_fn.jl
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
25 changes: 25 additions & 0 deletions R/fastest_2_R_fn.R
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]
)
}
)


}
18 changes: 18 additions & 0 deletions R/make_travel_time_mat.R
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

}
79 changes: 79 additions & 0 deletions R/tarjet.R
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()
31 changes: 31 additions & 0 deletions _targets.R
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)
)
)
1 change: 1 addition & 0 deletions julia_libraries.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
using Distributions
5 changes: 5 additions & 0 deletions packages.R
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)

0 comments on commit 3e24c0f

Please sign in to comment.