Invalid Path Argument Error for print()

I'm trying to use a forloop to produce multiple reports by feeding an Rmarkdown file through an r script.
Upon running the forloop, I receive the following error, originating from the Rmakrdown file:

Quitting from lines 69-70 (WeeklyReportTemplate.Rmd)
Error in dir.create(dirname(name), recursive = TRUE) :
invalid 'path' argument

Here is the r script:

library("rmarkdown")
library(tidyverse)
library(ggplot2)
install.packages("reprex")
library(reprex)
PennState1 <- read_csv("Baseball/PennState1.csv") 
PitcherName = unique(PennState1$Pitcher)
show(PitcherName)

for(PitcherName in PennState1){
rmarkdown::render('/Users/NoahThurm/Desktop/WeeklyReportTemplate.Rmd',
                  output_file =  paste("WeeklyReport_", PitcherName, '_', ".html", sep=''), 
                  output_dir = '/Users/NoahThurm/Desktop')
}

Here are lines 69-70 in the Rmd (error source):

print(VeloPlot)

Which reference the preceding chunk:

I've never used the dir.create() function, and it isn't in my code, is there something going on I'm not accounting for? Any other tips for smoothing out this forloop process would be much appreciated.

Hi, and welcome. A reproducible example, called a reprex is much more helpful than a screenshot, for future reference.

Here's what might be happening:

PitcherName <- "Sandy Kolfax"
paste("WeeklyReport_", PitcherName, '_', ".html", sep='')
#> [1] "WeeklyReport_Sandy Kolfax_.html"

Created on 2019-12-17 by the reprex package (v0.3.0)

The blank between the first and last name creates a filename problem that could throw your error.

1 Like

This worked, thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.