Figures showing at the end of the document in R markdown - r

I have been working on this issue since yesterday and have checked almost all the solutions but I'm still not able to solve this issue. All the figures and tables I put in the document shows at the end of the document instead of where I place them.
fig_cap : yes
keep_tex : yes
floatsintext : no
figurelist : no
tablelist : no
footnotelist : no
linenumbers : no
mask : no
draft : no
documentclass : "apa6"
classoption : "man"
output : papaja::apa6_pdf
header-includes:
\usepackage{float}
\floatplacement{figure}{H}
I have added float package as well as fig_cap in yaml above as recommended in some other solutions. Also I have globally defined fig.pos = H
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'H')
```
This is the histogram I'm trying to showin between my paragraphs (it moves down to the end of document as soon as I use fig.cap)
```{r Histogram, out.width = "90%", out.height = "100%", fig.align = "center", fig.pos='H', out.extra = '',fig.cap= "eee", echo=FALSE}
histogram_plot <- ggplot(dataset1, aes(x=SocialNetworkUse))+
geom_histogram(bins = 30) +
labs(x="Social Network Use (Hours)", y = "Respondents")
histogram_plot
```
Now this is the what I'm getting in my tex file
\begin{figure}[H]
{\centering \includegraphics[width=0.9\linewidth,height=1\textheight,]{ANLY-699_files/figure-latex/Histogram-1}
}
\caption{eee}\label{fig:Histogram}
\end{figure}
However the fff files shows this output
\begin{figure*}[hbt]
\ifnextchar[{\eatarg}{}
[H]
{\centering \includegraphics[width=0.9\linewidth,]{ANLY-699_files/figure-latex/Histogram-1}
}
\caption{foff}\label{fig:Histogram}
\end{figure*}
Fairly new to Rmarkdown and not sure what's causing the figures to not stay in place. Any help would be appreciated.

I just had to add figsintext : yes in my Yaml file to solve the issue.

Related

Custom language engine on Knitr

I have added a custom language engine on Knitr according the documentation.
require(knitr)
knit_engines$set(upper = function(options) {
code <- paste(options$code, collapse = "\n")
if (options$eval)
toupper(code) else code
})
And when I add text a following command to Rmd-file I can run a current chunk in RStudio.
```{upper}
Hello, **knitr** engines!
```.
HELLO, KNITR ENGINES!
But If I try to Knit a whole file to a html-page I can't see the result of chunked code. And I will get the following warning message on R Markdown tab.
Warning message:
In get_engine(options$engine) :
Unknown language engine 'upper' (must be registered via knit_engines$set()).
So how can I register the engine so that the program will see it later on?
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::knit_engines$set(upper = function(options) {
code <- paste(options$code, collapse = "\n")
if (options$eval)
toupper(code) else code
})
```
```{upper}
Hello, **knitr** engines!
```

Text (not syntax) highlighting in Rmarkdown/Papaja

I'm compiling a report using Papaja and Rmarkdown, and I want to highlight various text throughout. Doing so with latex or pure Rmarkdown is easy, but I'm receiving an "undefined control sequence" error when I compile the report to PDF using Papaja's application of Knitr.
A similar question about text highlighting within a single Rmarkdown file was answered here: RMarkdown / pandoc fails to knit Pdf with latex color commands. I'd like to know if an answer exists for multiple Rmarkdown files using Papaja.
I'll include a minimal example below.
1) File called papaja_file.Rmd
---
# https://crsh.github.io/papaja_man/index.html
title : "Some Title"
shorttitle : "HEADER"
author:
- name : "Name R Here"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Include addresss"
email : "randomemail#usa.edu"
affiliation:
- id : "1"
institution : "Any University"
author_note: |
....
abstract: |
Text here for abstract.
keywords : "Keyword1, keyword2, keyword3"
bibliography : ["references.bib"]
figsintext : no
figurelist : no
tablelist : no
footnotelist : no
lineno : yes
lang : "english"
class : "man"
output : papaja::apa6_pdf
---
```{r load_packages, include = FALSE}
library("papaja")
```
```{r analysis_preferences}
# Seed for random number generation
set.seed(42)
```
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', echo = TRUE, warning = FALSE, message = FALSE)
```
```{r child = 'child_document.Rmd'}
```
\newpage
# References
```{r create_references, echo = F}
r_refs(file = "references.bib")
```
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}
Notice that it references a single child Rmarkdown document.
2) The child document with text, called child_document.Rmd
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
One sentence without highlighting. \hl{Another with highlighting.}
3) The preamble, called preamble.tex
\usepackage{color}
\usepackage{soul}
\definecolor{lightblue}{rgb}{0.90, 0.95, 1}
\sethlcolor{lightblue}
Knitting the main papaja file produces the error. Removing the \hl command within the child document allows the pdf to compile without issue.
Result after putting YAML header within the main papaja document rather than the child:
The YAML header in your child document is not evaluated, c.f.
The master document, for example, consists of the YAML front matter and includes the children, which are themselves R Markdown documents without a YAML front matter.
https://crsh.github.io/papaja_man/tips-and-tricks.html#splitting-an-r-markdown-document
But you can include your preamble.tex in you master file using:
output:
papaja::apa6_pdf:
includes:
in_header: preamble.tex
c.f. https://github.com/rstub/stackoverflow/commit/a92a67d4721ee9c06e995b08adbf8cb89daebcb4
Result:

R markdown document display codes nicely

I am preparing R markdown document and I would like to display postgre sql codes nicely in R markdown document of course the codes should only be displayed not runned. How can I do that? . Below I put some code examples.
Thank you.
title: "POSTGRE sql "
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r1, eval=F}
with data as (select * from (values
('03-05-2019'::date,'{"color": true,"view": [181] ,"school":
[805,812,852,856,857]}'::jsonb),
('06-08-2019'::date,'{"color": false,"view": [184,185],"school":
[805,855,859]}'::jsonb),
('04-07-2019'::date,'{"color": true,"view": [184,185,189],"school":
[855,859]}'::jsonb)
) as v(published_date,attributes))
```
In the header add this to your output
output:
html_document:
highlight: pygments
and in the body use this line
{r, engine = 'sql', eval = FALSE}
I am not sure the major differences in SQL and PostgreSQL, but the syntax highlighting does work.
Here is a link to show other languages you can use with pygments
http://pygments.org/languages/

How to justify (to both sides) text in R Markdown when knitting in pdf output

I have looked for ways to control the alignment of the text, however I could not find anything for PDF outputs.
There is an existing answer, but related to HTML output only: How to justify the text to both sides when knitting html in rmarkdown.
R Markdown should default to using justified text. However, if you only want to export to PDF, we can directly use LaTeX commands within the document.using the standard arguments \centering \raggedright and \raggedleft, as explained here.
Here is a minimal example:
---
output: pdf_document
---
```{r, include = FALSE}
devtools::install_github("coolbutuseless/lipsum")
library(lipsum)
```
**Default**
`r lipsum[1]`
\centering
**Centered Text**
`r lipsum[1]`
\raggedright
**Ragged Right**
`r lipsum[1]`
\raggedleft
**Ragged Left**
`r lipsum[1]`
If you want to revert to justified text, you can use the ragged2e LaTeX package. You will need to load this within the YAML by adding:
---
output: pdf_document
header-includes:
- \usepackage[document]{ragged2e}
---
\raggedleft
**Ragged Left**
`r lipsum[1]`
\justifying
**Revert to Justified**
`r lipsum[1]`
Edit
If you are using the papaja template you need to include all the YAML. Not providing an author, shorttitle or another other field will cause it to crash.
---
title : "The title"
shorttitle : "Title"
author:
- name : "First Author"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Postal address"
email : "my#email.com"
- name : "Ernst-August Doelle"
affiliation : "1,2"
affiliation:
- id : "1"
institution : "Wilhelm-Wundt-University"
- id : "2"
institution : "Konstanz Business School"
author_note: |
Add complete departmental affiliations for each author here. Each new line herein must be indented, like this line.
Enter author note here.
abstract: |
Enter abstract here. Each new line herein must be indented, like this line.
keywords : "keywords"
wordcount : "X"
bibliography : ["r-references.bib"]
figsintext : no
figurelist : no
tablelist : no
footnotelist : no
lineno : yes
mask : no
class : "man"
output : papaja::apa6_pdf
header-includes:
- \usepackage[document]{ragged2e}
---
```{r load_packages, include = FALSE}
library(lipsum)
```
\justifying
**Default**
`r lipsum[1]`

Chunk option class.output is not working on Error Message

I am preparing a tutorial for a course and I want to change the colour of the error to be red. I am using BookDown and gitbook as my output format. But I found that the option class.output is not working. I want to add a class to the output for the error message I get. How can I do that? You can use this as an example:
---
title: "Test Book"
author: "therimalaya"
site: bookdown::bookdown_site
output: bookdown::gitbook
---
# Hello World
```{r, error = TRUE, class.output="red"}
rnorm(-10)
```
This works if there is no error.
class.output is not applied to errors (see here).
Following this answer, I suggest you to use an error hook:
```{r error-hook, echo=FALSE}
knitr::knit_hooks$set(error = function(x, options) {
paste0(
"```{",
ifelse(is.null(options$class.error),
"",
paste0(" .", gsub(" ", " .", options$class.error))
),
"}\n",
x,
"\n```"
)
})
```
Now, you can use a "new" class.error option in your chunk.
```{r, error = TRUE, class.error="red"}
rnorm(-10)
```
Feel free to open a feature request here.
The ability to use custom CSS classes for errors, warnings, and messages was just added to knitr, so you will be able to use the following syntax.
```{r error = TRUE, class.error = "bg-danger text-danger"}
rnorm(-10)
```
Here I'm using Bootstrap classes, but you can pass any class(es) you need to class.error. The chunk options class.message and class.warning also work. Note that class.output is applied only to standard code outputs.

Resources