How to render a "github_document" Rmd without generating the HTML preview? - r

I'm maintaining a package and have to rebuild the README.Rmd file every once in a while.
Using the "knit" button in RStudio, the effect is correct: the file README.md is generated at the root of the package, and a README.html preview is created in a temporary file and opened.
However, if I use the following command, the HTML preview is created at the root of the package, which is unnecessary.
rmarkdown::render(input="README.Rmd")
How to tell this preview file to be temporary, or even to not bother to exist at all?
I tried to set intermediates_dir=tempfile() with no effect, and I couldn't find what command RStudio is running on "knit" push. Moreover, it seems that this simple command does not have this side effect when run through Github Actions (link).
PS: Here is a minimal example of my Rmarkdown file (whole file here):
---
output: github_document
---
Hello Word

Set the html_preview option to false, like so:
---
output:
github_document:
html_preview: false
---
Hello World
This option is documented here. BTW, I suspect that a .html file is generated on the server where the render-rmarkdown action is performed; you don't see it locally because the action only commits changes to .md files:
$ git commit ${RMD_PATH[*]/.Rmd/.md} -m 'Re-build Rmarkdown files' || echo "No changes to commit"

Related

Rstudio save Rmd file failed

While I editing Rmd file using RStudio, "Ctrl+S" not work anymore for the current edited file. The unsaved file status is illustrated as fig 1. But both the "Ctrl+S" and "Clicking the save icon" not work. This occurred several hours after the RStudio started. I can't catch exactly when this is happened. To solve this problem, using "Ctrl +A" and copy all the content to another file. And then close the problem Rmd file without saving.
It is ok to open another new Rmd file and both "Ctrl+S" and "Clicking the save icon" worked normally for the new Rmd file.
fig.1 there is a "*" after the file name, indicating the file has been changed and not saved.
My OS is: Windows 10 home version

Debugging rmd file 'does not exist in current working directory'

Can someone clear up how the working directory affects debugging in rmd files. I have a project which includes an rmd file. The rmd file starts with some parameters:
params:
filtered_obs: "inst/data/filtered_obs.csv"
And in the code, let's take the following line:
wrangled_devices <- wrangle_devices(params$filtered_devices)
This works when I deploy the code and run it. I can also copy and paste the above command onto the console and it runs fine. The working directory is set to the project directory, which lines up correctly with the relative path given to the parameter.
However, when I step through the code in the rmd file, using ctrl + enter, I get the following error:
> getwd()
[1] "D:/MonkeySource/2 Cloud/cdk_stacks/docker-images/flexi/monkeyr"
> wrangled_devices <- wrangle_devices(params$filtered_devices)
Error: 'inst/data/filtered_devices.csv' does not exist in current working directory ('D:/MonkeySource/2 Cloud/cdk_stacks/docker-images/flexi/monkeyr/inst').
As you can see the wd is correct in the console, but not correct in the rmd file. It seems like the rmd file assumes the wd is it's own location? Is this correct? This seems really wrong...
If I then click the up arrow on the console and rerun the command (so literally rerunning the last command), it works fine. Obviously the actaul wd applies this time.
> wrangled_devices <- wrangle_devices(params$filtered_devices)
>
My question is - is there a way to control the effective working directory as you issue debug commands using ctrl+enter in rmd files?

Is there a "bitbucket_document" output setting for Rmarkdown?

I built an internal R package that I host on the company's bitbuckets. My Rmarkdown knits to
"github_document".
The image links are broken when I view the Readme on bitbucket.
When I visit the package's page, it tries to link to this file, and fails:
https://mybicketbucket.ca/users/myusername/repos/mypackage/man/figures/README-unnamed-chunk-7-1.png
left-clicking on readme.md, it links to this file and successfully displays the image:
https://mybicketbucket.ca/users/myusername/repos/mypackage/browse/man/figures/README-unnamed-chunk-7-1.png?raw=
any idea?
Just add in your first chunk.
knitr::opts_knit$set(base.url = "raw/")
The md file will not be pretty on your package repo but it will print the figure on bitbucket.

How to keep temporary _main.Rmd file in bookdown (when there is no error)

I am building a PDF from several RMarkdown files using Bookdown in Rstudio. In the process, Bookdown first compiles the different Rmd files into one big Rmd file (_main.Rmd), and then knits it into a PDF. If no errors are thrown, this temporary _main.Rmd file is usually deleted. I would like to keep the _main.Rmd file even when the file has successfully knitted into a PDF.
Is there any option I can add to _bookdown.yml to achieve this?
I just added this feature in the development version of bookdown, you may try:
remotes::install_github('rstudio/bookdown')
and set delete_merged_file: false in the config file _bookdown.yml.

R issue when moving an rmd file from one project to another (working directory issue)

I have two projects in R. I've moved an .Rmd document from project 1 to project 2.
When I used the .Rmd file which I had moved to project 2 to try and read in some data I get the following error message:
cannot open file '"mydata.csv"': No such file or directoryError in file(file, "rt") : cannot open the connection.
This kind of error usually suggests to me it's a working directory issue, however when I run getwd() in the command line it's the correct working directory that is listed and points to where the csv is stored. I've also run getwd() within the rmd doc and again the wd is correct.
Does anyone else have this experience of moving one .Rmd file to another project and then it not working in the new project?
The code in the .Rmd file that I am trying to run is:
Data <- read.csv("mydata.csv", stringsAsFactors = T) and the data is definitely within the project and has the correct title, is a csv etc.
Has anyone else seen this issue when moving an RMarkdown document into another project before?
Thanks
This may not be the answer, but rmarkdown and knitr intentionally don't respect setwd(): the code in each block is run from the directory holding the .rmd file. So, if you've moved your .rmd file but are then using setwd() to change to the directory holding the data, that does not persist across code chunks.
If this is the issue, then one solution is to use the knitr options to set the root.dir to the data location:
opts_knit$set(root.dir = '/path/to/the/data')
See here.
Maybe not relevant but it seems to be the most likely explanation for what's happening here:
The project shouldn't really interfere with your code here. When opening the project it will set your working directory to the root location of the project. However, this shouldn't matter in this case since RMarkdown files automatically set the working directory to the location where the RMarkdown file is saved. You can see this when running getwd() once in the Console and once from the RMarkdown file via run current chunk.
The behavior is the same when the file is knitted. So except in the case when "mydata.csv" is in the same directory as the RMarkdown file, the code above won't work.
There are two workarounds: you can use relative or absolute paths to navigate to your data file. In a project I prefer relative paths. Let's say the rmd file is in a folder called "scripts" and your data file is in a folder called "data" and both are in the same project folder. Then this should work:
Data <- read.csv("../data/mydata.csv", stringsAsFactors = TRUE)
The other option, which I do not recommend, is to set the working diretory in the rmd file via:
opts_knit$set(root.dir = '../data/')
The reason why I wouldn't do that is because the working direcotry is only changed when knitting the document but using the rmd file interactivly, you have a different working directory (the location of the rmd file)
This is a great application of the here package for dealing with these types of issues.
here https://github.com/jennybc/here_here looks around the Rmd file (and all R files) for the .Rproj file and then uses that as an "anchor" to search for other files using relative references. If for instance you had the following project structure:
-data
|--mydata.csv
-src
|-00-import.R
|-01-make-graphs.R
|-02-do-analysis.R
-report
|--report.Rmd
-yourproject.Rproj
And you wanted to use mydata.csv in your report.Rmd you could do the following:
library(here)
dat <- read.csv(here("data", "mydata.csv"))
here will then convert this path to "~/Users/../data/mydata.csv" for you. Note that you have to be in the Rproject for this use case.
Here is such a great package and solves a lot of issues.

Resources