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)
```
Related
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
---
How can I extract all the code (chunks) from an RMarkdown (.Rmd) file and dump them into a plain R script?
Basically I wanted to do the complementary operation described in this question, which uses chunk options to pull out just the text (i.e. non-code) portion of the Rmd.
So concretely I would want to go from an Rmd file like the following
---
title: "My RMarkdown Report"
author: "John Appleseed"
date: "19/02/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents.
For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
Some text description here.
```{r cars}
a = 1
print(a)
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Some more comments here.
To an R script containing just the code portions of the above, as the following:
knitr::opts_chunk$set(echo = TRUE)
a = 1
print(a)
summary(cars)
plot(pressure)
You could use knitr::purl, see convert Markdown to R script :
knitr::purl(input = "Report.Rmd", output = "Report.R",documentation = 0)
gives Report.R:
knitr::opts_chunk$set(echo = TRUE)
a = 1
print(a)
summary(cars)
plot(pressure)
Another way is to set the purl hook in your Rmd:
```{r setup, include=FALSE}
knitr::knit_hooks$set(purl = knitr::hook_purl)
knitr::opts_chunk$set(echo = TRUE)
```
Then the R script is generated when you knit. You can exclude some chunks with the chunk option purl = FALSE.
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 "-".
I use the package captioner to automatically reference tables, figures, supplementary data, codes, etc in my normal work. Althoug I can use it efficiently, I would like to create a section in the RMarkdown document which contains the list (index) of all the elements of the same style.
Let's say I have 2 tables in a RMarkdown document.
---
title: "Untitled"
author: "Mario Modesto Mata"
date: "6/7/2020"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
library(captioner)
table_number <- captioner(prefix = "Table")
```
The data frame of mtcars is in `r table_number("table1", display="cite")`.
```{r}
data(mtcars)
print(mtcars)
```
`r table_number(name = "table1", caption = "Caption of table1 (mtcars)")`
The data frame of mtcars is in `r table_number("table2", display="cite")`.
```{r}
data(iris)
print(iris)
```
`r table_number(name = "table2", caption = "Caption of table2 (iris)")`
I would like to include at the beginning an index of the tables that were defined with the function table_number().
Is there a way to do this? I would prepare later the equivalent index for figures and other topics...
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"]
```