How to set cache directory in knitr - r

I am trying to figure out how to change the cache directory for knitr. I already tested some approaches:
```{r setup, echo = FALSE}
library(knitr)
library(png)
library(grid)
library(tufte)
library(ggplot2)
library(xtable)
opts_chunk$set(cache.path("../.intermediates"))
Gives me:
Error in cache.path("../.intermediates") : não foi possível encontrar a função "cache.path"
```{r setup, echo = FALSE}
library(knitr)
library(png)
library(grid)
library(tufte)
library(ggplot2)
library(xtable)
knitr::cache.path("../.intermediates")
```
Results in:
Erro: 'cache.path' is not an exported object from 'namespace:knitr'
Using the yaml:
link-citations: true
lang: pt-BR
csl: chicago-fullnote-bibliography.csl
links-as-notes: yes
header-includes: |
\renewcommand{\figurename}{Figura}
knit: cache.path("../.intermediates")
Results in:
1: In file.rename(from, to) :
não foi possível renomear o arquivo '/home/luis/Dropbox/Documentos/Psiquiatria/Projetos/LIVRO-stats/reports/stats_files/figure-latex' para '/home/luis/Dropbox/Documentos/Psiquiatria/Projetos/LIVRO-stats/.intermediates//home/luis/Dropbox/Documentos/Psiquiatria/Projetos/LIVRO-stats/reports/stats_files/figure-latex', motivo 'Arquivo ou diretório não encontrado'
2: In file.rename(from, to) :
não foi possível renomear o arquivo '/home/luis/Dropbox/Documentos/Psiquiatria/Projetos/LIVRO-stats/.intermediates//home/luis/Dropbox/Documentos/Psiquiatria/Projetos/LIVRO-stats/reports/stats_files/figure-latex' para '/home/luis/Dropbox/Documentos/Psiquiatria/Projetos/LIVRO-stats/reports/stats_files/figure-latex', motivo 'Arquivo ou diretório não encontrado'
What is the proper way of setting the cache directory?

opts_chunk$set(cache.path = " ")

Related

Quarto and knitr to external file

I have a directory Project/Package/R with three files: script.R, knitr.Rmd and quarto.qmd.
This is knitr.Rmd:
---
title: "Essai"
output: pdf_document
---
```{r, include=FALSE}
library(here)
knitr::opts_knit$set(root.dir = here())
source(here("Package","R","script.R"), local = knitr::knit_global())
```
This is quarto.qmd:
---
title: "Essai"
format:
pdf:
toc: true
toc-depth: 2
---
```{r, include=FALSE}
library(here)
knitr::opts_knit$set(root.dir = here())
source(here("Package","R","script.R"), local = knitr::knit_global())
```
All is OK with knitr compilation, but quarto can't find the script R:
Quitting from lines 10-14 (stack.qmd)
Erreur dans readChar(con, 5L, useBytes = TRUE) :
impossible d'ouvrir la connexion
Appels : .main ... source -> withVisible -> eval -> eval -> load -> readChar
Exécution arrêtée
What is wrong? Thank you in advance.

How to scrape data from PDF with R?

I need to extract data from a PDF file. This file is a booklet of public services, where each page is about a specific service, which contains fields with the following information: name of the service, service description, steps, documentation, fees and observations. All pages follow this same pattern, changing only the information contained in these fields.
I would like to know if it is possible to extract all the data contained in these fields using R, please.
[those that are marked in highlighter are the fields with the information]
I've used the command line Java application Tabula and the R version TabulizeR to extract tabular data from text-based PDF files.
https://github.com/tabulapdf/tabula
https://github.com/ropensci/tabulizer
However, if your PDF is actually an image, then this becomes an OCR problem and needs different a tool.
Caveat: Tabula only works on text-based PDFs, not scanned documents. If you can click-and-drag to select text in your table in a PDF viewer (even if the output is disorganized trash), then your PDF is text-based and Tabula should work.
Here is an approach that can be considered to extract the text of your image :
library(RDCOMClient)
library(magick)
################################################
#### Step 1 : We convert the image to a PDF ####
################################################
path_TXT <- "C:\\temp.txt"
path_PDF <- "C:\\temp.pdf"
path_PNG <- "C:\\stackoverflow145.png"
path_Word <- "C:\\temp.docx"
pdf(path_PDF, width = 16, height = 6)
im <- image_read(path_PNG)
plot(im)
dev.off()
####################################################################
#### Step 2 : We use the OCR of Word to convert the PDF to word ####
####################################################################
wordApp <- COMCreate("Word.Application")
wordApp[["Visible"]] <- TRUE
wordApp[["DisplayAlerts"]] <- FALSE
doc <- wordApp[["Documents"]]$Open(normalizePath(path_PDF),
ConfirmConversions = FALSE)
doc$SaveAs2(path_Word)
#############################################
#### Step 3 : We convert the word to txt ####
#############################################
doc$SaveAs(path_TXT, FileFormat = 4)
text <- readLines(path_TXT)
text
[1] "Etapas:"
[2] "Documenta‡Æo:"
[3] "\a Consulte a se‡Æo documenta‡Æo b sica (veja no ¡ndice)."
[4] "\a Original da primeira via da nota fiscal do fabricante - DANFE - Resolu‡Æo Sefaz 118/08 para ve¡culos adquiridos diretamente da f brica, ou original da primeira via da nota fiscal do revendedor ou c¢pia da Nota Fiscal Eletr“nica - DANFE, para ve¡culos adquiridos em revendedores, acompanhados, em ambos os casos, da etiqueta contendo o decalque do chassi em baixo relevo;"
[5] "\a Documento que autoriza a inclusÆo do ve¡culo na frota de permission rios/ concession rios, expedido pelo ¢rgÆo federal, estadual ou municipal concedente, quando se tratar de ve¡culo classificado na esp‚cie \"passageiros\" e na categoria \"aluguel\","
[6] "\a No caso de inclusÆo de GRAVAME comercial, as institui‡äes financeiras e demais empresas credoras serÆo obrigados informar, eletronicamente, ao Sistema Nacional de GRAVAMEs (SNG), sobre o financiamento do ve¡culo. Os contratos de GRAVAME comercial serÆo previamente registrados no sistema de Registro de Contratos pela institui‡Æo financeira respons vel para permitir a inclusÆo do GRAVAME;"
[7] "\a Certificado de registro expedido pelo ex‚rcito para ve¡culo blindado;"
[8] "\a C¢pia autenticada em cart¢rio do laudo m‚dico e o registro do n£mero do Certificado de Seguran‡a Veicular (CSV), quando se tratar de ve¡culo adaptado para deficientes f¡sicos."
[9] "Taxas:"
[10] "Duda 001-9 (Primeira Licen‡a). Duda de Emplacamento: 037-0 para Carros; 041-8 para motos."
[11] "Observa‡Æo:"
[12] "1. A 1' licen‡a de ve¡culo adquirido atrav‚s de leilÆo dever ser feita atrav‚s de Processo Administrativo, nas CIRETRANS, SATs ou uma unidade de Protocolo Geral. (veja no ¡ndice as se‡äes Lista de CIRETRANS, Lista de SATs e Unidades de Protocolo Geral para endere‡os )"
Once you have the text in R, you can use the R package stringr.

R Markdownd: ! LaTeX Error: Two \documentclass or \documentstyle commands

I am trying to knit a pdf in Rmarkdown, but I receive the same error over and over again:
! LaTeX Error: Two \documentclass or \documentstyle commands.
I already different approaches like deleting some rows, putting \begin{document} in different places or changing the order of loading packages.
This is my code:
---
title: "Feedback"
author: "Me"
date: "09 02 2021"
toc: true
toc_depth: 2
toc-title: "Verzeichnis"
fig.caption: "Tabelle"
smooth_scroll: false
header-includes:
\documentclass{article}
\PassOptionsToPackage[usenames,dvipsnames]{xcolor}
\usepackage{fancyhdr}
\usepackage[T1]{fontenc}
\usepackage[default]{sourcesanspro}
\usepackage{tikz}
mainfont: SourceSansPro
output: pdf_document
---
\begin{document}
\addtolength{\headheight}{1.0cm}
\fancypagestyle{plain}{}
\thispagestyle{fancy}
\fancyhead[L]{\includegraphics[width = 500pt]{"/Users/lisak/One Drive/OneDrive/Dokumente/Masterarbeit/Erhebung/Anschreiben/alt/banner.png"}}
\renewcommand{\headrulewidth}{0pt}
{r, echo = FALSE, message = FALSE}
.onLoad <- function(libname = find.package("kableExtra"), pkgname = "kableExtra") {
if (knitr::is_latex_output()) {
load_packages <- getOption("kableExtra.latex.load_packages", default = TRUE)
if (load_packages) {
usepackage_latex("booktabs")
usepackage_latex("longtable")
usepackage_latex("array")
usepackage_latex("multirow")
usepackage_latex("wrapfig")
usepackage_latex("float")
usepackage_latex("colortbl")
usepackage_latex("pdflscape")
usepackage_latex("tabu")
usepackage_latex("threeparttable")
usepackage_latex("threeparttablex")
usepackage_latex("ulem", "normalem")
usepackage_latex("makecell")
usepackage_latex("xcolor")
}
}
auto_format <- getOption("kableExtra.auto_format", default = TRUE)
if (auto_format) auto_set_format()
if (!is.null(rmarkdown::metadata$output) &&
rmarkdown::metadata$output %in% c(
"ioslides_presentation", "slidy_presentation",
"gitbook", "bookdown::gitbook", "radix_article", "radix::radix_article",
"distill_article", "distill::distill_article"
)) {
options(kableExtra.html.bsTable = TRUE)
}
if (!is.null(knitr::opts_knit$get("rmarkdown.pandoc.to")) &&
knitr::opts_knit$get("rmarkdown.pandoc.to") %in% c("epub3", "epub")) {
options(kableExtra.knit_print.dependency = FALSE)
}
}
stuff....
\end{document}
Rmarkdown will automatically insert stuff like \documentclass{article} and \begin{document} when converting the rmarkdown document into a tex document. You must not insert it a second time
\PassOptionsToPackage[usenames,dvipsnames]{xcolor} must be used before the documentclass. As rmarkdown takes away your ability to insert the document class yourself, you can't use this
rmarkdown has trouble to correctly parse the square brackets of optional arguments. As a workaround, you can hide these commands in a separate .tex file
the syntax of your r chunk is wrong. You must surround it with backticks
---
title: "Feedback"
author: "Me"
date: "09 02 2021"
toc: true
toc_depth: 2
toc-title: "Verzeichnis"
fig.caption: "Tabelle"
smooth_scroll: false
mainfont: SourceSansPro
output:
pdf_document:
keep_tex: true
includes:
in_header: preamble.tex
---
```{r, echo = FALSE, message = FALSE}
.onLoad <- function(libname = find.package("kableExtra"), pkgname = "kableExtra") {
if (knitr::is_latex_output()) {
load_packages <- getOption("kableExtra.latex.load_packages", default = TRUE)
if (load_packages) {
usepackage_latex("booktabs")
usepackage_latex("longtable")
usepackage_latex("array")
usepackage_latex("multirow")
usepackage_latex("wrapfig")
usepackage_latex("float")
usepackage_latex("colortbl")
usepackage_latex("pdflscape")
usepackage_latex("tabu")
usepackage_latex("threeparttable")
usepackage_latex("threeparttablex")
usepackage_latex("ulem", "normalem")
usepackage_latex("makecell")
usepackage_latex("xcolor")
}
}
auto_format <- getOption("kableExtra.auto_format", default = TRUE)
if (auto_format) auto_set_format()
if (!is.null(rmarkdown::metadata$output) &&
rmarkdown::metadata$output %in% c(
"ioslides_presentation", "slidy_presentation",
"gitbook", "bookdown::gitbook", "radix_article", "radix::radix_article",
"distill_article", "distill::distill_article"
)) {
options(kableExtra.html.bsTable = TRUE)
}
if (!is.null(knitr::opts_knit$get("rmarkdown.pandoc.to")) &&
knitr::opts_knit$get("rmarkdown.pandoc.to") %in% c("epub3", "epub")) {
options(kableExtra.knit_print.dependency = FALSE)
}
}
```
stuff....
and preamble.tex:
\usepackage{fancyhdr}
\usepackage[T1]{fontenc}
\usepackage[default]{sourcesanspro}
\usepackage{tikz}
\addtolength{\headheight}{1.0cm}
\fancypagestyle{plain}{}
\thispagestyle{fancy}
\fancyhead[L]{\includegraphics[width = 500pt]{example-image}}
\renewcommand{\headrulewidth}{0pt}

Rmarkdown stop generating after a paragraph

So my problem is that after a specific paragraph Rmarkdown doesn't generating the rest of the code and I don't know why. Note that if I delete that paragraph the program will render untill the next text paragraph and then do the same thing.
Here is a minimal markdown example with the problematic paragraph. The code block and text after the code block do not show up in the PDF.
---
title: "Proiect Regresie"
author: "Andrei Alexandru; grupa 322"
output: pdf_document
---
# Estimarea parametrilor
Considerăm modelul de regresie liniară simplă $y=\beta_{0}+\beta_{1}x +\varepsilon$ (unde x venitul provenit din vanzarile in America de Nord iar
y venitul provenit din vanzarile la nivel mondial, $\varepsilon$ este repartizat normal de medie 0 și varianță sigma p\u atrat. Estimatorii parametrilor $\beta_{0}$ \c si $\beta_{1}$ ob\c tinu\c ti prin metoda celor mai mici pătrate sunt calcula\c ti \^in R.
```{r}
mtcars
```
Some more text.
Apparently, the special characters (\c, etc.) are causing this problem. You need to specify the language using babel. Since it is a LaTeX package, you need to put it in header-includes in YAML.
Here are two examples of the changes you need to do:
\c si needs to be rewritten \c{s}i
ob\c tinu\c ti needs to be rewritten ob\c{t}inu\c{t}i
However, I can't figure out why using \^i or \^{i} does not work.
Full example:
---
title: "Proiect Regresie"
author: "Andrei Alexandru; grupa 322"
output: pdf_document
header-includes:
- \usepackage[romanian]{babel}
---
# Estimarea parametrilor
Considerăm modelul de regresie liniară simplă $y=\beta_{0}+\beta_{1}x +\varepsilon$ (unde x venitul provenit din vanzarile in America de Nord iar
y venitul provenit din vanzarile la nivel mondial, $\varepsilon$ este repartizat normal de medie 0 și varianță sigma p\u{a}trat. Estimatorii parametrilor $\beta_{0}$ \c{s}i $\beta_{1}$ ob\c{t}inu\c{t}i prin metoda celor mai mici pătrate sunt calcula\c{t}i \^{i}n R.
```{r}
mtcars
```
Some more text.

knitr does not compile pdf

I'm new to coding and I`m trying to use knitr to print a PDF. I have installed R 3.5.3 and RStudio installed correctly as simple codes usually run.
I have both installed in the same folder as Miktex 2.9.7
As I create a new markdown file with a PDF output, incoded in UTF-8, I get the standard model text.
I've tried different encodings and reinstalled miktex several times.
When I run a code I used at my uni lab, it runs, but if I alter even 1 character on a string and save it, it wont compile the pdf anymore.
This is what I've been trying to compile:
---
title: "Lista 1"
date: "April 6, 2019"
output: pdf_document
---
```{r instalar pacotes necessarios}
# Carregando os pacotes necessários
if (!require("pacman")) install.packages("pacman")
pacman::p_load("tinytex", "tidyverse")
```
Questão 1
Use os conhecimentos adquiridos em sala de aula (e fora dela!) para realizar >as seguintes operações sobre o
banco de dados mpg:
```{r variaveis q1a}
posicao <- 100
item.a <- mpg[posicao, c("manufacturer","model")]
```
a) A partir do código acima podemos verificar que o veículo de posição `r posicao` banco de daados é um `r item.a`
```{r}
media.milhas.cidade <- mean(mpg$cty)
```
b) A partir do código acima, pode-se verificar que a média de milhas por galão na cidade é `r media.milhas.cidade`
I should just get the pdf but Rstudio prints this:
================================
processing file: lista1mkd.Rmd
|......... | 14%
ordinary text without R code
|................... | 29%
label: instalar pacotes necessarios
|............................ | 43%
ordinary text without R code
|..................................... | 57%
label: variaveis q1a
|.............................................. | 71%
inline R code fragments
# A tibble: 1 x 2
manufacturer model
<chr> <chr>
1 honda civic
|........................................................ | 86%
label: unnamed-chunk-1
|.................................................................| 100%
inline R code fragments
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS lista1mkd.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output lista1mkd.tex --template "C:\Users\CESARA~1\DOCUME~1\R\WIN-LI~1\3.5\RMARKD~1\rmd\latex\DEFAUL~3.TEX" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" --variable "compact-title:yes"
output file: lista1mkd.knit.md
Error: Failed to compile lista1mkd.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips.
In addition: Warning message:
In readLines(logfile) : incomplete final line found on 'lista1mkd.log'
Execution halted

Resources