I am trying to write a Jekyll converter for R Markdown files. I created RMarkdownConverter.rb and placed it in my _plugins directory. I have verified that other plugins are working but this one is not. I also don't see any error messages, including the ones I put in myself. It seems this is not being used. However, Jekyll is generating an HTML file for my .Rmd file but simply processes the R chuck as code chuck. Any help or thoughts would be greatly appreciated.
RMarkdownConverter.rb file:
module Jekyll
class RMarkdownConverter < Converter
safe true
priority :low
def setup
STDERR.puts "Setting up R Markdown..."
return if #setup
require 'rinruby'
#setup = true
rescue
STDERR.puts 'do `gem install rinruby`'
raise FatalException.new("Missing dependency: rinruby")
end
def matches(ext)
ext =~ /Rmd/i
end
def output_ext(ext)
'.html'
end
def convert(content)
setup
STDERR.puts "Using R Markdown..."
R.eval "require(knitr)"
R.eval "render_markdown(strict=TRUE)"
R.assign "content", content
STDERR.puts content
R.eval "out <- knit(text=content)"
R.eval "print(out)"
end
end
end
The contents of my first R Markdown post:
---
layout: post
title: Using (R) Markdown, Jekyll, and Github for Blogging
published: true
tags: R R-Bloggers Jekyll github
type: post
status: publish
---
First, we need to install [RinRuby](https://sites.google.com/a/ddahl.org/rinruby-users/) to call R from Ruby. In the terminal, execute:
gem install rinruby
First R chuck:
```{r}
2 + 2
```
Try replacing the last few lines with the following
R.assign "content", content
R.eval "knitr::render_markdown(strict = TRUE)"
R.pull "(knitr::knit2html(text = content, fragment.only = TRUE))"
I think you need R.pull to copy the contents of R output to Ruby. Moreover, I would recommend directly converting from Rmd to html. I have used this strategy successfully in working with Ruhoh which is another ruby based blogging platform.
UPDATE. It is very odd but using the extension rmd seems to conflict with md. I changed it randomly to ram and jekyll seems to pick it up correctly. I am not sure why.
Related
The problem
I'm running into a conflict error and a strange issue where my Homepage title is modified and "- Page 2" appears in the tab itself when I place a certain post inside the _posts folder. When I take this specific post out, the problem is resolved. The blog post itself works properly and I'm not encountering other issues. I'm running Minimal Mistakes theme and I'm knitting .Rmd reports in RStudio with GitHub flavored markdown.
Link to my main repo
Link to the specific post inside repo
Link to config.yml
YAML header for knitting .Rmd
title: "Data visualisation"
date: "`r Sys.Date()`"
output:
md_document:
variant: gfm
Knitting options for Rmd
library(knitr)
# macOS
# setwd("~/Developer/mkruisbrink.github.io/_reports/R-for-data-science/01-explore/01-data-visualisation/")
# Windows
setwd("D:/Max Kruisbrink/Developer/mkruisbrink.github.io/_reports/R-for-data-science/01-explore/")
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
# define knitr paths for macOS
#base.dir <- "~/Developer/mkruisbrink.github.io/"
# define knitr paths for Windows
base.dir <- "D:/Max Kruisbrink/Developer/mkruisbrink.github.io/"
base.url <- "/"
fig.path <- "assets/reports/R-for-data-science/01-explore/01-data-visualisation/"
# set knitr parameters
opts_knit$set(base.dir = base.dir, base.url = base.url)
opts_chunk$set(fig.path = fig.path)
I get a neat .md file which I can place inside my _posts folder with some yaml header. All other posts are processed fine, the error only appears when I place this specific post inside the folder.
YAML header for post
title: "The basics of data visualisation in R"
excerpt: "These are the basics of visualizing data in R, using only the tidyverse functionality"
header:
overlay_image: /assets/images/midjourney-optimised/big-computer-screen-financial-dashoard-optimised.jpg
category:
- Exploration
What I've tried so far
I've tried the following:
Spent at least 2 hours on online research, didn't find any proper relevant sources to help me out here
Manually removing from and adding back all posts from the _posts folder to figure out the problem lies with this specific post
Running bundle update and bundle install
Double checking YAML headers for posts and the config file
Checking .Rmd settings for other reports that are knitted in the same way and cause no issues
Removing the post content from the .md file to see if the bug came from something in the code perhaps
Replacing the whole YAML header with that of a post that causes no issues
Ran --trace in combination with bundle exec jekyll serve for the same conflict results
Ran --verbose in combination with bundle exec jekyll serve and I could find ONE extra instance of something writing index.html to _site. So the following appears twice in the log. I know the culprit is this post but how to get more info on what is causing it?
Writing: D:/Max Kruisbrink/Developer/mkruisbrink.github.io/_site/index.html
Conclusions and questions
I'm starting to run out of options for my current skill level of debugging.
Could it be something relating to _includes or _layouts? I might have tweaked layouts but pretty sure I left the includes folder alone. But why would other posts be unaffected?
Could it be plugin related? I had no problems up until last week suddenly and didn't change any plugin.
Should I update to the latest theme, removing any customization I've done to rule that out
I'm running this in my gemspec
source "https://rubygems.org"
gemspec
gem "webrick"
gem "jekyll-remote-theme"
gem "tzinfo" #windows dependancy?
gem "tzinfo-data"
Can someone point me in a direction here? Getting slightly clueless and I want to be a little cautious with my re-rolling. Thanks in advance!
Edit 2022-12-28
When I remove the permalink: \ from the main index.html file, the error is resolved but when building the site with bundle exec jekyll serve a strange page2 folder is created in _site with its own index.html
However, I still think something is off with certain posts as the error is resolved when I move them to _drafts.
Anyone any thoughts?
There was an issue with the _config.yml file where I was mixing liquid syntax and the pagination plugin.
# Outputting
permalink: pretty
paginate: 5 # amount of posts to show
paginate_path: /page:num/
timezone: Europe/Amsterdam # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
# Plugins (previously gems:)
plugins:
- jekyll-paginate
changing the outputting into the following solved my issue.
# Outputting
permalink: /:categories/:title/
timezone: Europe/Amsterdam # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
My final cue was the page2 folder that was being built and the fact that EVERY post that I added to my _posts folder above the number 5 was causing issues.
When I try to render the latest version of the book R for Data Science (R4DS), I get as far as LaTeX compilation, then am stopped by the following error message.
! Text line contains an invalid character.
l.406 #> -- ^^[
[1mAttaching packages^^[[22m --------------------------------...
Error: LaTeX failed to compile _main.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See _main.log for more info.
>
This corresponds to the part of the R4DS book where we are shown how to load the tidyverse and, looking at the _main.tex file, I see many lines with what look like ANSI escape sequences starting on this line. They have the form ^[[1m, ^[[22m, and so on. I manually compiled the LaTeX output using lualatex and found that there are dozens if not hundreds of examples of this throughout the book. I suspected it was because I was using the colorout package in R, but it appears that that package is required, so others who are rendering successfully must be using it too. I believe I have successfully updated all relevant packages.
It looks like I "solved" the problem by changing an option in the _common.R file from crayon.enabled=TRUE to crayon.enabled=FALSE. This removed the ANSI escape sequences from the book. Previously I had tried setting options(crayon.enabled=FALSE) in my R session, but this was evidently being overridden by the setting in _common.R.
Update: 23 Nov 2022
The process for rendering the files is completely different now because of the switch to Quarto. Here's how I did it.
Rscript -e 'update.packages()'
Rscript -e 'install.packages('quarto')'
Rscript -e 'devtools::install_github("hadley/r4ds")
git clone https://github.com/hadley/r4ds.git
cd r4ds
Next, I wrote a small perl script to avoid the error messages I was getting about trying to render html material to pdf. (I'm omitting a lot of dead-ends I encountered in the process.)
#!/usr/bin/perl
use File::Slurp qw(prepend_file);
my #files = glob( '*.qmd' );
my $header = "\n---\nprefer-html: true\n---\n\n";
foreach my $file (#files) {
prepend_file($file, $header);
}
I ran the above script in the r4ds directory.
Next I loaded R and did the following:
library(quarto)
quarto_render("index.qmd", output_format = "pdf")
The above failed with the error message: "\begin{document} not found". Luckily, the aborted process leaves an index.tex file I can process and also gives a line number for the error. I went to that line number in the index.tex file and deleted the block of html I found there.
After that, I ran
lualatex index.tex
twice and got a successful render, minus the cover page. (You could presumably run xelatex index.tex instead.) There are a lot of problems with my render, such as the plots being too large to fit on the page. If I decide to spend time fixing them (unlikely, since Hadley seems to want us to use the online version) I'll modify this answer.
I'm trying to run an R file from the command line (from Terminal -- on macOS 10.13.3).
Following the directions in this helpful Q & A, I added #! [path/to/file.R] as the first line of the R file.
When I then type Rscript [path/to/file.R] in Terminal, a part of the file to test that it worked seems to run (I added a print() statement), however, the next line:
rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8')
Leads to this error:
Error in rmarkdown::render_site(output_format = "bookdown::pdf_book", :
No site generator found.
Why does the print() statement appear to work fine, but this line - to render a bookdown book - does not?
The file I'm trying to run is on GitHub here.
For Markdown I have had the following script in the examples directory of littler for about 1 1/2 years, and rendered hundreds of times with it.
Note it calls rmarkdown::render() yet you want a bookdown format, so methinks you may need to write a very similar script calling bookdown::render_book() or a similar function.
You can of course do exactly the same as a one-liner from Makefiles, and many people do. Here is one which will map all Rmd file to pdf documents (which is my standard workflow, I don't do much html):
sources := $(wildcard *.Rmd)
slides := $(sources:.Rmd=.pdf)
all: ${slides}
%.pdf: %.Rmd
Rscript -e "rmarkdown::render(\"$<\", clean=TRUE)"
Because my editor has a shortcut for invoking make this is particularly handy.
Lastly, for Rscript (available everyhere) you'd do #!/usr/bin/env Rscript or possibly the direct path to Rscript.
You could use a simple script like this that you save to compile.book.sh for example.
#!/bin/sh
## Get the path to the book
BOOK_PATH=$1
## Get the current path
CURRENT_PATH=$(pwd)
## Get to the right path
cd ${BOOK_PATH}
## Compile the book
R -e 'rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8')'
## Get back to the previous path
cd ${CURRENT_PATH}
You can then execute the compilation as follows from wherever in your machine (from the terminal):
sh compile.book.sh path/to/Rfolder
With path/to/Rfolder being the folder where your index.Rmd is.
I am trying to knit together 3 files using this example: http://rmarkdown.rstudio.com/rmarkdown_websites.html
My files all share the same directory, which has been set in the RStudio build options. I literally copied the YML from the examples with my own file names. Even if I copy the examples directly, I get the same error.
I even ran the site_generator function from the RMarkdown Github: https://github.com/rstudio/rmarkdown/blob/master/R/render_site.R#L157
I noticed there are options for custom generators, which I do not want. All I would like to do is be able to knit together several markdown files into a single webpage and then create a navbar with tabs for each page/section.
I got the same error when attempting to build with an incorrectly named YAML config file. Specifically, the RStudio build option for a website requires that the config file be called _site.yml.
Verify that this file exists and that you're using the extension .yml rather than .yaml (even though the latter is preferred).
What helped for me was putting the line site: bookdown::bookdown_site back to index.Rmd.
lord, my issue was that i didn't set my working directly to where my _site.yml file and index.Rmd files are stored - the little things!
I fixed this error using this steps.
1-) RStudio-Tools-InstallPackages
2-) Install From should be CRAN Repository.
3-) write this command --> install.packages("install.packages("rmarkdown")")
4-) Click Install
And problem has gone... And this is output.
This error also appears when there is a format issue in the yaml section of index.Rmd.
In my case, I encountered the error when the opening "---" in index.Rmd had a couple of white spaces " ---".
If you are having this problem, I would recommend knitting index.Rmd by itself (not building the book) before trying anything more drastic.
I have a global ~/.Rprofile file and another .Rprofile file located in my project's current working directory and both of the have the following contents:
.First() <- function() {
options(rstudio.markdownToHTML =
function(inputFile, outputFile) {
system(paste("pandoc", shQuote(inputFile), "-s --webtex -o", shQuote(outputFile)))
}
)
}
Unfortunately, when I open the RStudio app neither of them appear to be working. The aim of what I'm trying to do is to make the "Knit HTML" button render the Markdown file, which has inline LaTeX, process through Pandoc using webtex as the LaTeX renderer.
Does anyone know how I check whether my .Rprofile files are loading at startup?
Thanks for any help!
POST ANSWER EDIT (after Josh's answer):
For clarity, my working project's .Rprofile file (which works) now reads as such:
options(rstudio.markdownHTML =
function(inputFile, outputFile) {
system(paste("pandoc", shQuote(inputFie), "-s --webtex -o", shQuote(outputFile)))
}
)
\\ you will need to end with a blank carriage return underneath
The R docs should help to see how to deal with .Rprofiles. Execute the following at the console:
> ?Startup
The relevant portion of this indicates that you need to put your project .Rprofile in the initial working directory that will be loaded when starting the project. Thus if your project is ~/foo/foobar.Rproj, then you should have your profile be ~/foo/.Rprofile and make sure that when starting up, the initial working directory is ~/foo/. You can see this in the title bar at the top of the console pane in RStudio.
Also to confirm that the correct .Rprofile is actually being loaded, I would personally put in a test to see which (if any) profile is being picked up. For example, include:
print("This is the Rprofile inside the foo project!")
Here is another example about getting this to work:
http://support.rstudio.org/help/discussions/suggestions/1095-different-rprofile-for-a-project#comment_15690293
Finally, if the correct .Rprofile is being loaded inside the project, then there must be something wrong with your code. Looks like you got this from our docs though, so if you get the profile loaded, and continue to have problems, please let us know. You can post a new discussion on our support thread.
Josh
Product Manager - RStudio