Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot generate .Rmd files #11

Open
aaplab1 opened this issue May 1, 2014 · 1 comment
Open

cannot generate .Rmd files #11

aaplab1 opened this issue May 1, 2014 · 1 comment

Comments

@aaplab1
Copy link

aaplab1 commented May 1, 2014

When I do the initGitbook() command I get the following error and no .Rmd files are created.

Error in if (grepl("r knitsetup.+\n", lines)) { : 
argument is of length zero
@benmarwick
Copy link

Yes, I found the same error, I've submitted a pull request that fixes it for me. If I replace the initGitbook function with the code below, then it works fine.

dir = getwd()
dir <- normalizePath(dir)
checkForGitbook(quiet = TRUE)
oldwd <- setwd(dir)
test <- system(paste0("gitbook init ", dir))
if (test != 0) {
  stop("gitbook initalization failed")
}
mdfiles <- list.files(dir, "*.md", recursive = TRUE, full.names = TRUE)
mdfiles <- mdfiles[-c(grep("README.md$", mdfiles), grep("SUMMARY.md$", mdfiles))]
mdfiles2 <- gsub("\\.md$", ".Rmd", mdfiles)
file.rename(mdfiles, mdfiles2)
knitr.header <- c("```{r knitsetup, echo=FALSE, results='hide', warning=FALSE, message=FALSE, cache=FALSE}", 
                  "opts_knit$set(base.dir='./', fig.path='', out.format='md')", 
                  "opts_chunk$set(prompt=TRUE, comment='', results='markup')", 
                  "# See yihui.name/knitr/options for more Knitr options.", 
                  "##### Put other setup R code here", "", "",
                  "# end setup chunk",
                  "```")

for (rmd in mdfiles2) {
  print(rmd)
  file <- file(rmd)
  lines <- readLines(file)
  close(file)
  # don't inject knitr setup chunk if Rmd file 
  # already has one, ie. editing an existing Rmd
  # or the references Rmd, which has a different setup
  toMatch <- c("r knitsetup", "r setup")
  matches <- grepl(paste(toMatch,collapse="|"), lines)  
  suppressWarnings(if (!any(matches)) {
    lines <- c(knitr.header, lines)
  })
  file <- file(rmd)
  writeLines(lines, file(rmd))
  close(file)
}
setwd(oldwd)
invisible()

liyi-1989 added a commit to liyi-1989/Rgitbook that referenced this issue Dec 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants