Skip to content

Commit 9d2839b

Browse files
authored
Allow nested folder depth (#14)
* Improve logic for detecting and unrolling nested directories with hw. * Add a few test files not connected into testthat * Ensure we can recursively create nested directories, e.g. `mkdir -p` * Slight clean up to avoid matching `dir()` function.
1 parent fd46a58 commit 9d2839b

File tree

7 files changed

+113
-7
lines changed

7 files changed

+113
-7
lines changed

R/assignr.R

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ generate_hw_pkg = function(x,
5252
output_dir = paste0(name, "-", type),
5353
render_files = TRUE,
5454
zip_files = TRUE,
55-
file_dependencies = NULL) {
55+
hw_directory = '',
56+
file_dependencies = character(0)) {
5657

5758
if (length(remove_indexes) > 0) {
5859
# create assignment output lines
@@ -72,8 +73,16 @@ generate_hw_pkg = function(x,
7273

7374
# Fill directory
7475
if(length(file_dependencies) >= 1) {
75-
file.copy(file_dependencies,
76-
file.path(output_path, basename(file_dependencies)))
76+
77+
# Create any required directories for the copy
78+
for(dir_dependent in unique(dirname(file_dependencies))) {
79+
dir.create(file.path(output_path, dir_dependent),
80+
showWarnings = FALSE, recursive = TRUE)
81+
}
82+
83+
# Perform a vectorized copy to the appropriate directory
84+
file.copy(file.path(hw_directory, file_dependencies),
85+
file.path(output_path, file_dependencies))
7786
}
7887

7988
# Name of Rmd file to build
@@ -112,9 +121,22 @@ extract_hw_name = function(x) {
112121

113122
}
114123

115-
hw_dir_dependencies = function(x) {
116-
main_dir_files = list.files(dirname(x), full.names = TRUE, recursive = TRUE)
117-
grep(main_dir_files, pattern='-(main|assign|sol)', invert=TRUE, value=TRUE)
124+
hw_dir_dependencies = function(hw_directory) {
125+
# Move to where the file might be found
126+
old_wd = setwd(file.path(hw_directory))
127+
128+
# Determine all files and directories within the homework directory
129+
main_dir_files = list.files(path = ".", full.names = TRUE, recursive = TRUE)
130+
131+
# Avoid retrieving any file matching our exclusion list
132+
hw_dependencies = grep(main_dir_files, pattern = '-(main|assign|sol)',
133+
invert = TRUE, value = TRUE)
134+
135+
# Return to original working directory
136+
setwd(old_wd)
137+
138+
# Release files
139+
hw_dependencies
118140
}
119141

120142
#' Retrieve example file path
@@ -185,16 +207,23 @@ assignr = function(file,
185207
zip_files = TRUE,
186208
render_files = TRUE) {
187209

210+
# Minimal conditions for processing.
188211
if (length(file) != 1) {
189212
stop("Only one file may be processed at time.")
190213
} else if (!grep( "-main.Rmd$", file)) {
191214
stop("Supplied file must have -main.Rmd")
192215
}
193216

217+
# Retrieve value before -main.Rmd
194218
hw_name = extract_hw_name(file)
195219

196-
hw_dependency_files = hw_dir_dependencies(file)
220+
# Obtain location of the homework directory
221+
hw_directory = dirname(file)
222+
223+
# Extract a local file structure
224+
hw_dependency_files = hw_dir_dependencies(hw_directory)
197225

226+
# Begin processing chunks
198227
input_lines = readLines(file)
199228

200229
chunk_tick_lines = detect_positions(input_lines, "```")
@@ -235,6 +264,7 @@ assignr = function(file,
235264
output_dir = output_dir,
236265
render_files = render_files,
237266
zip_files = zip_files,
267+
hw_directory = hw_directory,
238268
file_dependencies = hw_dependency_files
239269
)
240270
}
@@ -248,6 +278,7 @@ assignr = function(file,
248278
output_dir = output_dir,
249279
render_files = render_files,
250280
zip_files = zip_files,
281+
hw_directory = hw_directory,
251282
file_dependencies = hw_dependency_files
252283
)
253284
}

tests/testthat.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library(testthat)
2+
library(assignr)
3+
4+
test_check("assignr")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: 'Homework X'
3+
author: "Prof. Name"
4+
date: 'Due: Friday, Month Day by 1:59 PM CDT'
5+
output:
6+
html_document:
7+
theme: readable
8+
toc: yes
9+
---
10+
11+
# Exercise 1 (Introductory `R`)
12+
13+
```{asis, directions = TRUE}
14+
For this exercise, we will create a couple different vectors.
15+
```
16+
17+
**(a)** Create two vectors `x0` and `x1`. Each should have a
18+
length of 25 and store the following:
19+
20+
- `x0`: Each element should be the value `10`.
21+
- `x1`: The first 25 cubed numbers, starting from `1.` (e.g. `1`, `8`, `27`, et cetera)
22+
23+
```{asis, solution = TRUE}
24+
**Solution:**
25+
```
26+
27+
```{r, solution = TRUE}
28+
x0 = rep(10, 25)
29+
x1 = (1:25) ^ 3
30+
```
31+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testing
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ba

tests/testthat/solormd/hw00-main.Rmd

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: 'Homework X'
3+
author: "Prof. Name"
4+
date: 'Due: Friday, Month Day by 1:59 PM CDT'
5+
output:
6+
html_document:
7+
theme: readable
8+
toc: yes
9+
---
10+
11+
# Exercise 1 (Introductory `R`)
12+
13+
```{asis, directions = TRUE}
14+
For this exercise, we will create a couple different vectors.
15+
```
16+
17+
**(a)** Create two vectors `x0` and `x1`. Each should have a
18+
length of 25 and store the following:
19+
20+
- `x0`: Each element should be the value `10`.
21+
- `x1`: The first 25 cubed numbers, starting from `1.` (e.g. `1`, `8`, `27`, et cetera)
22+
23+
```{asis, solution = TRUE}
24+
**Solution:**
25+
```
26+
27+
```{r, solution = TRUE}
28+
x0 = rep(10, 25)
29+
x1 = (1:25) ^ 3
30+
```
31+

tests/testthat/test-assignr.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test_that("test solo rmd", {
2+
expect_equal(2 * 2, 4)
3+
})
4+
5+
test_that("test file embed", {
6+
expect_equal(2 * 2, 4)
7+
})

0 commit comments

Comments
 (0)