Knitr / R error when saving to a relative directory - r

I try to save a variable to a relative path. The R code runs without a problem in RStudio, but as soon as I compile the RMarkdown file i get a reproducible error (only if the destination path is relative).
This is my RMarkdown file:
---
title: "Untitled"
output: html_document
---
```{r test}
x<-"test"
save(x,file="./raw_data/test.r")
```
This is the resulting error:
processing file: Untitled.Rmd
Quitting from lines 7-9 (Untitled.Rmd)
Error in gzfile(file, "wb") : cannot open the connection
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> save -> gzfile
Execution halted
Is this a bug / does knitr not support relative directory paths?
Thanks

Related

Error in read.dcf(...) :Line starting '--- ... is malformed

I am getting an error at the time of publishing a Shiny app in R.
When i run the code locally, everything works. But at the time of publishing to shiny, i am getting the following error:
Error in read.dcf(...) : :Line starting '--- ... is malformed!
Calls: <Anonymous> -> rstudioEncoding -> readDcf -> read.dcf
How can this be fixed? Is this related to the header of the file?
The lines i have at the beginning are:
---
title: "Mapa de Corresponsales Bancarios Bancolombia"
output: html_document
runtime: shiny
---

Knitr Error in Namepace: Cannot knit to PDF or other format

I am trying to create a PDF export of some of my R outputs (tables and graphs). I created a .Rmd document and I have tried to run the simplest of code setting up the document, but I immediately get an error. The same error pops up when I click the "Knit" button at the top of the R studio window.
The code:
---
title: "Example"
author: "Me"
output: pdf_document
---
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
The error:
Error in namespaceExport(ns, exports) :
undefined exports: Sweave2knitr, all_labels, all_patterns, all_rcpp_labels, asis_output, cache_engines, clean_cache, combine_words, current_input, dep_auto, dep_prev, engine_output, extract_raw_output, fig_chunk, fig_path, hook_ffmpeg_html, hook_gifski, hook_mogrify, hook_movecode, hook_optipng, hook_pdfcrop, hook_plot_asciidoc, hook_plot_custom, hook_plot_html, hook_plot_md, hook_plot_rst, hook_plot_tex, hook_plot_textile, hook_pngquant, hook_purl, hook_r2swf, hook_scianimator, hooks_asciidoc, hooks_html, hooks_jekyll, hooks_latex, hooks_listings, hooks_markdown, hooks_rst, hooks_sweave, hooks_textile, image_uri, imgur_upload, include_app, include_graphics, include_url, inline_expr, is_html_output, is_latex_output, is_low_change, kable, kables, knit, knit2html, knit2pandoc, knit2pdf, knit2wp, knit_child, knit_code, knit_engines, knit_exit, knit_expand, knit_filter, knit_global, knit_hooks, knit_meta, knit_meta_add, knit_params, knit_params_yaml, knit_patterns, knit_print, kni
Calls: loadNamespace ... namespaceImportFrom -> asNamespace -> loadNamespace -> namespaceExport
In addition: Warning message:
S3 methods '$.knitr_strict_list', 'is_low_change.default', 'knit_print.default', 'knit_print.knit_asis', 'knit_print.knit_asis_url', 'knit_print.knitr_kable', 'print.knitr_kable', 'process_group.block', 'process_group.inline', 'process_tangle.block', 'process_tangle.inline', 'sew.character', 'sew.default', 'sew.error', 'sew.html_screenshot', 'sew.knit_asis', 'sew.knit_embed_url', 'sew.knit_image_paths', 'sew.list', 'sew.message', 'sew.recordedplot', 'sew.source', 'sew.warning' were declared in NAMESPACE but not found
Execution halted
I've tried updating all of my packages and restarting R. I've also tried changing the working directory.
It also seems like Knitr package isn't loading (it's not checked in the packages tab). I just tried checking the box and I received a "fatal error" message and R needed to restart. Once it restarted, I was able to check the box without a "fatal error" but the original error still appears in the console.
To solve this issue, I tried uninstalling the Knitr package but received an error:
cannot delete reparse point 'C:\Users\R\win-library\4.1/knitr/R', reason 'There is a mismatch between the tag specified in the request and the tag present in the reparse point'
Warning in install.packages :
cannot remove prior installation of package ‘knitr’
Warning in install.packages :
cannot delete reparse point 'C:\Users\R\win-library\4.1/knitr/R', reason 'There is a mismatch between the tag specified in the request and the tag present in the reparse point'
Warning in install.packages :
restored ‘knitr’
To resolve this, I changed the name of the folder for the package to xxx and then installed the package again without any issues.
Once I had reinstalled, a new error appeared prompting me to install latexpdf.
I tried to Knit the document again and a new error appeared which lead me to this post: Failed to compile test.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See test.log for more info. Execution halted
As stated in that StackOverflow answer, I ran tinytex::install_tinytex()
After doing this I was able to knit a PDF

Rmarkdown does not follow same paths as R script and console commands

In Rmarkdown, I cannot read files that I can read from the console and in an R script, because Rmarkdown is not following the same paths as my R scripts and console commands.
Here is a minimum reproducible example:
Create new project test.Rproj
Create a subdirectory called scripts
Run the following R Script scripts/test.R:
test <- as.data.frame(c(1, 2, 3))
dir.create("data")
write.csv(test, "data/test.csv")
rm(test)
test <- read.csv("data/test.csv")
Quit R, and reopen test.Rproj.
Knit the following Rmarkdown document (scripts/test.Rmd):
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
_```
```{r read-data}
test <- read.csv("data/test.csv")
_```
Yields the following error: Quitting from lines 12-13 (test.Rmd)
Error in file(file, "rt") : cannot open the connection
Calls: ... withVisible -> eval -> eval -> read.csv -> read.table -> file
Execution halted
(Note, the backticks in the .Rmd file are properly specified -- I added underscores above so that the backticks appeared in the code block.)
Two seemingly related issues:
I can read the test.csv file via Rmarkdown if it is in the scripts subdirectory, rather than the data subdirectory.
When I run list.files() from the console or script, I receive of list of files in the top-level directory (i.e., where test.Rproj is located), including the data and scripts subdirectories. When I run list.files() from Rmarkdown, I get a list of files in the scripts subdirectory.
How can I fix this problem?
Session info:
R version 4.1.0 (2021-05-18)
RStudio version 1.4.1717
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.5.1
Try something that looks like this as I am not sure of the nature of your `R Markdown.
test <- readRDS(here::here("data/test_data.rds"))
The bottom line is to use the here function from the here package.
When you knit a document in RStudio, by default the working directory is set to the current directory of the Rmd document (so that would be the "scripts" folder). Since the "scripts" folder does not contain the "data" directory, you get that error. You can change the default to use the project root directory if you prefer. That's an option in the RStudio Global Options menu.
See See https://bookdown.org/yihui/rmarkdown-cookbook/working-directory.html for more info

Execution halted

I'm new with R and I'm trying to make a pdf file out of a RnW file. I installed MaxTex, restarted my RStudio and tried to compile my Rnw file. For some reason it says 'execution halted' (after R loaded the librabries).
2 : echo keep.source term verbatim (label = setup, 25.3.Rnw:30)
Error in match.arg(options$results, c("verbatim", "tex", "hide")) :
'arg' should be one of “verbatim”, “tex”, “hide”
Calls: -> SweaveParseOptions -> check -> match.arg
Execution halted
What should I do?

knitr returns error on read.csv, works fine on console

I am trying to knit a document but I am getting an error on the read.csv function which works fine if I run it from the RMarkdown or from the console.
I have searched but could only find solutions related to reading a csv from a URL.
R read.csv from URL error in knitr
```{r}
# This analysis uses the National Names dataset
national_names_raw <- read.csv("NationalNames.csv")
```
This is the error I get...
Quitting from lines 33-35 (Popularity_of_Kier.Rmd)
Error in file(file, "rt") : cannot open the connection
Calls: <Anonymous> ... withVisible -> eval -> eval -> read.csv -> read.table -> file
Execution halted
Any help is appreciated.

Resources