I am practicing data analysis using R programming. I want my files to be on github. I am unable to figure out why github is not showing the output of .rmd files.
Here is the link to the file in my github repository Data Analysis Practice
I want the output including plots to be shown on github.
Instead of:
output: html_document
make it:
output: rmarkdown::github_document
(assuming you have the latest rmarkdown installed which you should if you're using RStudio — which I suspect you are — and keep it updated or update packages regularly).
It will create Exploring_one_variable.md and render the images as files.
When you browse to that markdown file, the images will render.
An alternative is to use:
output:
html_document:
keep_md: true
and it will render to both Exploring_one_variable.md and Exploring_one_variable.html so you'll have the best of both worlds without the local github-esque preview that the former provides.
You can get fancier and put something like the following as the first code section in the Rmd:
```{r, echo = FALSE}
knitr::opts_chunk$set(
fig.path = "README_figs/README-"
)
```
which will put the figures in a directory of your choosing.
You can see this in action here as the README.md was generated with knitr from the README.Rmd in the same directory.
There is now output: github_document
Related
I am trying to render a .qmd file to HTML specifying an output file. I am compiling it from a Quarto Project from RStudio on MacOS 12.6. I also have a bootstrap theme in the YAML header.
---
title: "quarto_output_experiment"
format:
html:
theme: journal
---
If I render it with Cmd + Shift + K from RStudio, the HTML document appears in the same directory as the .qmd and looks fine. However if I render it from the Quarto CLI specifying the output directory (a folder called output), the HTML can't render plots and lacks any sort of formatting and style. The command is
quarto render quarto_output_experiment.qmd --output output/report.html
Here are the results.
This is when rendered from RStudio
This is when render via Quarto CLI
What am I missing? How can I render the HTML in a different directory and have plots and proper style?
For reference, this is the document itself
---
title: "quarto_output_experiment"
format:
html:
theme: journal
---
## Quarto
Quarto enables you to weave together content and executable code into a finished document.
To learn more about Quarto see <https://quarto.org>.
```{r}
1 + 1
```
# Lorem ipsum
```{r}
library(ggplot2)
ggplot(data.frame(x = rnorm(100)), aes(x)) + geom_density()
```
UPDATE
Specifying --output-dir intead of --output produces a document with plots and style, but the question still remains if there is a way to change the name of the output HTML file? Probably not mission critical, but I'm still curious.
That is a really weird bug and it seems to be related to the path (see here. Usually, you can use quarto::render_quarto() like this,
quarto::quarto_render(input = "quarto_output_experiment.qmd", output_file = "report.html")
This works, but when you add the folder name
quarto::quarto_render(input = "quarto_output_experiment.qmd", output_file = "results/report.html")
it does not work anymore. The problem is that the folder (quarto_output_experiment_files) where the libs and images are stored, is not automatically also placed where the output-file is located.
I would file an issue here.
A possible solution for the time being would be the self-contained param that produces self-contained html files, e.g.
---
title: "quarto_output_experiment"
format:
html:
theme: journal
self-contained: true
---
then you could use file.copy to move that file in the correct folder.
Edit: The option self-contained is depreciated, use embed-resources instead! Link
I need the output to be portable so all the file paths need to be relative to the root directory of the project not absolute paths. I also need to set the figure size so I can't fall back on markdown and just use but knitr insists on creating an absolute path in my output. I've tried setting root.dir & base.dir and fig.path in various combinations but I keep getting an absolute path in my output. Setting root.dir = "/" does change the path in my output but setting it to "" or "./" still results in an abs path so this is not helping produce a portable output.
My rmarkdown::render is being run from the same directory as the Rmd file, the output is to this same directory and the graphics files are in a sub-directory of the one with the notebook. Is the a way I can make knitr just us the paths I give it and not try to transform them in any way?
Issues with knitr & paths seem to be a common problem and there are a lot of stub Q&As out there in search results which often don't satisfactorily resolve the problem, or if they do resolve it the questioner and/or answer seem unclear on why it worked.
I've referred to https://yihui.org/knitr/ & https://bookdown.org/yihui/rmarkdown/ and I still don't have a satisfactory understanding of how to control output paths with knitr could someone help me understand how to control this or point me in the direction of a resource that can?
OK so calmed down and I tried to make a reprex, I found some interesting behaviour.
The graphics folder is a symlink to a folder in the parent directory and this works fine if I use as basic html_document output but when I use the revealjs::revealjs_presentation output I get different results in the basics document the img tag contains:
src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD......."
and in the presentation contains:
src="/home/richardjacton/project/graphics_test/example.jpg"
Now this does not work because I need to be able to move the directory containing the presentation and it's assets to somewhere else and have it work once built.
If I go in with inspect element and edit it to src="./graphics/example.jpg" that works.
Also If I make a real directory instead of a symlink to the a directory in the parent then I get src="graphics/example.jpg" in the revealjs output
---
title: "Example"
author: "Richard J. Acton"
date: "`r Sys.Date()`"
# output:
# html_document:
# df_print: paged
output:
revealjs::revealjs_presentation:
theme: dark
highlight: zenburn
center: true
self_contained: false
reveal_plugins: ["notes"]
reveal_options:
slideNumber: true
previewLinks: true
fig_caption: true
---
# Example
```{r, echo=FALSE, include=FALSE, message=FALSE}
fs::dir_create("../graphics_test/")
download.file("https://i.ytimg.com/vi/0hMr5-05bFc/maxresdefault.jpg", "../graphics_test/example.jpg")
fs::link_create("../graphics_test", "graphics")
# fs::dir_create("graphics")
# download.file("https://i.ytimg.com/vi/0hMr5-05bFc/maxresdefault.jpg", "graphics/example.jpg")
```
```{r, out.width='80%', echo=FALSE}
knitr::include_graphics("graphics/example.jpg")
```
In my real case I'm creating a presentation from inside a {targets} project that's also has a {bookdown} project in it that produces a report based on the pipeline. So the presentation lives in a sub-directory so I can exclude it from the bookdown build. I explicitly built the presentation with an rmarkdown::render call as the knit button in the gui wants to try and render the book: rmarkdown::render("test.Rmd",envir = new.env()). Also this is all in a directory mounted to a docker container that's running my RStudio server instance so I have a portable and reproducible working environment for any non-R dependencies of my pipeline.
So this is why I was looking for a resource with a more in depth explanation of what exactly happens when I make a knitr::include_graphics call because this is the sort of weird edge case I seem to encounter a lot and get stuck trying to debug. Thanks for the response #YihuiXie I love your work and use it daily - sorry for offering you the opportunity to engage with this particular headache.
I'm having some trouble to compile an entire document from many Rmd files by using the bookdown approach.
If I knit individual .Rmd files then 'preamble.tex' included in YAML options is taken into account.
If I render the book (with both approaches described here), then 'preamble.tex' is ignored.
To make things concrete, consider the following mwe:
preamble.tex:
\usepackage{times}
index.Rmd:
---
title: "My paper"
site: "bookdown::bookdown_site"
output:
bookdown::pdf_document2:
includes:
in_header: "preamble.tex"
---
01-intro.Rmd:
# Introduction
This chapter is an overview of the methods that we propose to solve an **important problem**.
Then, by knitting 'index.Rmd' or '01-intro.Rmd' the font indicated in 'preamble.tex' is used.
However when rendering with bookdown::render_book('index.Rmd',"bookdown::pdf_book", new_session = T) it is simply ignored.
What is more, in my actual project there are other output options that end up ignored. For example, I use toc: false and it works when knitting single files, but fails when rendering the document.
In this simple example it would be okay to use a single file, but my actual project has many chapters with R chunks within each of them. Thus, building a single file doesn't seem a good idea.
I appreciate any hints on what I am missing here.
Thanks in advance.
What you are missing here is that in your YAML header, preamble.tex is included for the bookdown::pdf_document2 output format and not bookdown::pdf_book, the format you pass to the output_format argument in bookdown::render_book(). For this reason, other YAML options (like toc: true) do not work either.
Running
bookdown::render_book('index.Rmd', "bookdown::pdf_document2", new_session = T)
instead should work.
Is it possible to use pandoc-crossref in bookdown?
I tried to change the yaml header to:
output:
bookdown::tufte_book2:
toc: yes
highlight: tango
pandoc_args: -F /usr/local/bin/pandoc-crossref
Which should pass the filter to pandoc, but I get the error:
pandoc: Error running filter pandoc-crossref:
Could not find executable ' pandoc-crossref'.
The above error does not make sense, as I entered the correct path. What kind of env is bookdown using, which is precluding the access to the filter file?
Here is an example
---
output: bookdown::html_document2
---
# Section name {#id}
```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. To cross-reference the figure, use `\#ref(fig:pressure)` to produce Figure \#ref(fig:pressure). All this is found within the section \#ref(id).
Which produces...
See https://bookdown.org/yihui/bookdown/figures.html for the official documentation.
I had a similar issue while I was trying to put numbers to equations in Word files (SO question). I got the same error Could not find executable 'pandoc-crossref'.
My RStudio installation (on Windows) did not come with pandoc-crossref. Here is what I did:
Downloaded pandoc-crossref from here.
Find the path where RStudio saved the file pandoc.exe:
rmarkdown::find_pandoc()
Put the pandoc-crossref.exe in the folder I got in (2).
Using RStudio's "Compile Notebook to html" feature, I noticed that a temporary .md file was created in the process, but deleted automatically. At one point I was lucky enough to see its content, and it is exactly what I need: the code chunks alternate with output chunks, all perfectly formatted.
So my question is: how do I generate such an .md file directly form an R script?
You could also run knitr::spin() directly from the R console.
If you run "Knit to HTML", a markdown file is temporarily created and deleted. To keep the markdown file, add the following to the YAML code at the top of the RMD file.
---
output:
html_document:
keep_md: true
---
Here is a helpful reference: https://bookdown.org/yihui/rmarkdown/html-document.html#keeping-markdown