R Function for Reading an Image from a URL - r

I am trying to read a url and get the image from it in order to knit it to a pdf using R Markdown. I've tried using the image_read() function in the magick package, but whenever I knit to my R Markdown pdf, it prints the metadata which I don't want. I tried the strip = TRUE option as well, but it didn't seem to work. Code Below:
library(magick)
url <- "https://a.espncdn.com/combiner/i?img=/i/headshots/mens-college-basketball/players/full/4280043.png"
temp <- image_read(url, strip = TRUE)
temp <- image_scale(temp, "200")
print(temp)
Any help is appreciated!

It works (with a current version of Magick) once you knit your pdf. Try:
---
title: "Magick_test"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## The image
```{r, echo=FALSE, message=FALSE}
library(magick)
url <- "https://a.espncdn.com/combiner/i?img=/i/headshots/mens-college-basketball/players/full/4280043.png"
temp <- image_read(url)
image_scale(temp, "200")
```

Related

How to export gt table as rtf from Markdown

I need to generate an HTML document which displays a gt table. At the same time, it should export the table as a rtf file. I have the following process, but when I knit the document I get an unexpected output in the rtf file which is different from when I run the code interactively.
---
title: "rtf test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r results='asis'}
library(tidyverse)
library(gt)
out <- mtcars %>%
head() %>%
gt()
print(out)
gtsave(out, filename = "rtf_test.rtf")
```
The rtf output look like the following:
!!!!!RAW-KNITR-CONTENT{\rtf\ansi\ansicpg1252{\fonttbl{\f0\froman\fcharset0\fprq0 Calibri;}{\f1\froman\fcharset0\fprq0 Courier;}}{\colortbl;\red211\green211\blue211;}
\trowd\trrh0\trhdr
\pard\plain\uc0\qr\clvertalc\clpadb50\clpadfb3\clpadr50\clpadfr3\clpadl50\clpadfl3\clpadt50\clpadft3\clbrdrt\brdrs\brdrw40\brdrcf1\clbrdrb\brdrs\brdrw40\brdrcf1\clbrdrl\brdrs\brdrw20\brdrcf1\clbrdrr\brdrs\brdrw20\brdrcf1\cellx861
\intbl {\f0 {\f0\fs20 mpg}}\cell
\pard\plain
...
Instead of the desired table in the rtf file which I get when I run the code interactively:
I have tried using writeLines instead of gtsave and removing results = 'asis' from the code chunk but nothing I do seems to export the table as I want it. Any help is appreciated.

How to extract all code from an RMarkdown (.Rmd) file?

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.

When I knit a flextable to html there is no output with the table

The table prints nicely in markdown but is not present in the knitted html file. I noticed that it is classified as a list but don't know how to change it to an acceptable file type. The knitted output is not formatted as a table. I appreciate the help.
library("crosstable") #important package crosstable() function
library('dplyr')
library("flextable")
tbl1 = crosstable(mtcars2, c(1), by = 2) %>%
as_flextable(keep_id=FALSE)
print(tbl1)
According to ?print.flextable
Note also that a print method is used when flextable are used within R markdown documents. See knit_print.flextable().
Therefore, if we want to print in Rmarkdown, either use knitr::knit_print or remove the print as the ?knit_print.flextable documentation shows
You should not call this method directly. This function is used by the knitr package to automatically display a flextable in an "R Markdown" document from a chunk.
---
title: "Testing"
author: "akrun"
date: "09/12/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo = FALSE}
suppressPackageStartupMessages(library("crosstable")) #important package crosstable() function
suppressPackageStartupMessages(library('dplyr'))
suppressPackageStartupMessages(library("flextable"))
tbl1 = crosstable(mtcars2, c(1), by = 2) %>%
as_flextable(keep_id=FALSE)
# either use knit_print or remove the print wrapper
#knitr::knit_print(tbl1)
tbl1
```
-output

Looping Flextables in R Markdown documents with Word output

I am looking to print multiple flextables in a single R Markdown document. This is not challenging when using html output, but I need Word output.
With html output, the following R Markdown code (example taken from https://davidgohel.github.io/flextable/articles/offcran/examples.html#looping-in-r-mardown-documents) produces a document with multiple Flextables:
---
output:
html_document: default
---
```{r}
library(htmltools)
library(flextable)
ft <- flextable(head(iris))
tab_list <- list()
for(i in 1:3){
tab_list[[i]] <- tagList(
tags$h6(paste0("iteration ", i)),
htmltools_value(ft)
)
}
tagList(tab_list)
```
I have been unable to get an equivalent output using Word document output.
The solution proposed in
How to knit_print flextable with loop in a rmd file similarly works fine with html output, but I have failed to get this to render correctly with Word output.
Any advice on a R Markdown Word document output equivalent of the above example would be very helpful!
You need to use flextable::docx_value and the chunk option results='asis'
---
title: "Untitled"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
```
```{r results='asis'}
ft <- flextable(head(iris))
for(i in 1:3){
cat("<w:p/>")## add this to add an empty new paragraph between tables
flextable::docx_value(ft)
}
```

How to generate rmarkdown chunk within a chunk

I'm trying to generate a rmarkdown chunk using code. I've read similar questions and their solutions, such as using pander or using cat. I just can't seem to generate it. I also tried knitting the output manually. Here's an example of a Rmd file:
---
title: "test"
output: pdf_document
---
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}",
"2+2",
"```")
pander::pander(txt)
```
When I knit this, I get a verbatim {r} 2+2. I would like to see the number four instead. In my real example, I'm using bookdown and trying to generate a block2 chunk.
Any ideas how to generate this chunk that gets evaluated as R code?
Does this do what you want?
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}",
2+2,
"```")
pander::pander(txt)
```
This evalutates to
```{r} 4 ```
in your markdown document.
You using a string literal "2+2" as opposed to the expression 2+2. This is the first issue, I guess.
If you want it correctly parsed you need to add an sep = "\n" argument to paste or manually add the newline breaks.
I.e.
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}\n",
2+2,
"\n```", sep = "")
pander::pander(txt)
```
This evalutates to
```{r}
4
```
which is interpreted as R code in the markdown document.

Resources