How to utilise data from another package as parameter in rmarkdown - r

For my rmarkdown I wish to be able to parameterise data from the palmerpenguins package. However I am unable to utilise it when I specify it as a parameter and go to knit my file. params$data only prints out the word penguins when I want it to print out the whole data table, and params$data$species fails. Attached below is also the error code produced. Any clue what I'm doing wrong?
---
title: "Project_Report"
author: "Caitlin Luo"
date: "`r Sys.Date()`"
output: pdf_document
params:
data: !r penguins
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(palmerspenguins)
```
```{r penguins}
params$data
params$data$species
```

If the data is from another package you have to use the package::function notation in the YAML.
e.g.
---
title: "Project_Report"
author: "Caitlin Luo"
date: "`r Sys.Date()`"
output: pdf_document
params:
data: !r palmerpenguins::penguins
---

Related

How to change code output background colour when knitting rmarkdown to pdf?

I'm preparing a pdf document with rmarkdown. I'd like to know whether there is a way to change the code output's background color, so that the output is more distinguishable from other texts.
My current solution only changes the color of the code chunk itself. Thanks!
---
title: "Untitled"
output:
pdf_document:
highlight: default
header-includes: \usepackage{xcolor}
---
\definecolor{shadecolor}{RGB}{225, 225, 225}
Plain text.
```{r}
a <- c(1,2,3,4,5)
b <- c(1,2,3,4,5)
df <- data.frame(a, b)
# take a look at our data frame
df
```
Plain text.
A nice trick is to pass a class name to class.output, and then shadecolor will also be applied on chunk output too.
I have used "shadebox" as a class name but it can be any valid string.
---
title: "Untitled"
output:
pdf_document:
highlight: default
header-includes: \usepackage{xcolor}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, class.output="shadebox")
```
\definecolor{shadecolor}{RGB}{225, 225, 225}
Plain text.
```{r}
a <- c(1,2,3,4,5)
b <- c(1,2,3,4,5)
df <- data.frame(a, b)
# take a look at our data frame
df
```
Plain text

Cross-referencing in R bookdown makes the figure name incorrect

I am producing plotly charts in Rmarkdown and give them name and caption. but as soon as I cross reference them in the document, their name is ruined.
Here is the code:
---
title: "Preliminary Statistics"
author: "Babak"
date: "6/28/2021"
output:
bookdown::html_document2:
fig_caption: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
fig.width=9,
fig.height=5 ,
message=FALSE,
warning=FALSE)
```
```{r Area_duplicate, fig.cap = "Area of duplicated and unique properties by municipality"}
<PLOT CODES>
```
Plot \#ref(fig:Area_duplicate) shows the area od duplicated properties in the dataset grouped by municipality.
result:
screenshot of the caption
What should I do to resolve this issue?
Replace the underscore in the name with a "-".

How do I get a paged_table without column types displayed?

I need to print a paged_table in a HTML Rmd document, but I don't want the column types displayed.
?paged_table indicates that there are printing options, but the only options I can find documented are about the maximum numbers of rows/columns to print and whether or not to print row names.
Reproducible example in RMD:
---
output:
html_document:
df_print: paged
editor_options:
chunk_output_type: inline
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
datasets::mtcars
```
Ensure to install the DT package:
install.packages('DT')
The following is the RMD code:
---
output:
html_document:
df_print: paged
editor_options:
chunk_output_type: inline
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
library(DT)
datasets::mtcars
datatable(mtcars)
```

rMarkdown trouble loading csv in R

I am trying to create an R markdown using the csv NFL_DATA.csv. However when I try to use knitr I get the error:
Error in nrow(NFL_DATA): object 'NFL_DATA' not found Calls:
... in_dir -> inline_exec -> withVisible -> eval -> eval ->
nrow Execution halted
Here is the beginning of my code:
title: "XXXXXX"
author: "XXXXXX"
date: "December 11, 2017"
output: html_document
font-family: "Arial"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
---
## Introduction
XXXXXXXXXXXXX.
---
## Data
We used the site data.world.com to find my data. The dataset can be retrieved at [NFL_DATA.csv](https://data.world/alice-c/nfl-fines-and-suspensions/workspace/file?filename=All+Penalties.csv).
There are `r nrow(NFL_DATA)` observations in the NFL data set and `r length(NFL_DATA)` variables. The variables are:
```{r echo=FALSE, comment=""}
names(NFL_DATA)
```
When I run it the knitr does not work and gives an error message. However when I just run the last piece of code for names(NFL_DATA) it does work and it displays all the variable names. How can I resolve this?
Try this after editing the code to point to wherever you stored the csv file locally.
---
title: "NFL Fines and Suspensions Analysis"
author: "anAuthor"
date: "12/8/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Read Data
Here is example code to read the nfl data
```{r readData,echo=FALSE}
nfldata <- read.csv("./data/All Penalties.csv",header=TRUE)
```
There are `r nrow(nfldata)` observations in the NFL data set and `r length(nfldata)` variables. The variables are:
```{r}
names(nfldata)
```

R, list all packages and versions used in a markdown file

Hi I'm using R and R studio. Is there a way I can have my R markdown file list all the packages and their respected versions at the end of the documents? thanks! For example,
---
title: "test"
output: pdf_document
---
## R Markdown
```{r cars}
library(ggplot2)
library(gplots)
summary(cars)
```
You can get the names of loaded non-base packages with names(sessionInfo()$otherPkgs), so maybe something like this:
---
title: "test"
output: pdf_document
---
## R Markdown
```{r}
library(ggplot2)
library(data.table)
summary(cars)
```
```{r}
installed.packages()[names(sessionInfo()$otherPkgs), "Version"]
```

Resources