When I knit my .Rmd file to HTML, instead of saving the output to the same directory as the .Rmd file, RStudio saves the .html file and a copy of the original .Rmd file to my home directory instead of my working directory. Any idea how to fix this? Purely for organization purposes I'd like the output to be in the same directory as the oringinal .Rmd file.
I'd suggest using Dean Aatali's ezknitr package, which he wrote to correct this and a few other infelicities of knitr's input and output directory defaults. ezknitr::ezknit() "just works" as you'd like it to, writing its outputted HTML file to the directory contain the markdown file, instead of (as knitr::knit() would) to the current working directory:
## Reproducible e.g.
dir.create("path/to", recursive=TRUE)
cat("Hello world", "path/to/eg.Rmd")
## Check how it works
ezknitr::ezknit("path/to/eg.Rmd")
dir("path/to/")
[1] "eg.html" "eg.md" "eg.Rmd"
Related
I would like to keep the base folder of my R Markdown project tree as uncluttered as possible and still use the Knit button in RStudio to see some results as an HTML output in the RStudio's preview window. However, every time I knit, an HTML file is created to the base folder along my .Rmd files.
My folder structure might look like this:
If I've understood correctly - and do please correct me if I'm wrong - it is not very straightforward to output the HTML files to an upstream folder (here:"results" folder) from an R Markdown file by pressing the Knit button, so I would be willing to:
prevent the creation of HTML files altogether,
still see the results in an HTML format in RStudio's preview window, and
only when I'm really willing to save an HTML file, save (export) such a file and (manually) transfer it to the results folder.
So, if there is no easy way of directing the HTML files to the "results" folder, can I prevent the creation of HTML files altogether and only preview the results in the preview window?
I don't know of a way to attach this to the knit button, but you could write a function that does this:
get the current file open in the edit pane. This answer gives details on that.
run rmarkdown::render() with the output_dir argument set the way you want.
You can attach this function to a keyboard shortcut in RStudio using these instructions.
Here's a simple version of the function:
knit2 <- function(filename = rstudioapi::getSourceEditorContext()$path,
output_dir = tempdir()) {
result <- rmarkdown::render(filename, output_dir = output_dir)
getOption("viewer")(result)
}
Simply call knit2(), and the output will be written to tempdir(), which means the preview will appear in the Viewer pane. If you want it to write to "results", call it as
knit2(output_dir = "results")
If that's not a subdirectory of tempdir(), the preview will appear in an external browser.
I have 2 scripts. One is an R script and the other an rmarkdown script.
I'm using the following code in the R script to run the markdown script:
rmarkdown::render("my_md_file_path_and_name.Rmd"))
I want to have the .html file it creates output into a folder of my choosing. At the moment it outputs into the same folder where the markdown script is stored.
Is this possible? I've done a lot of googling and although there's a lot of talk on this, i can't find anything which actually works. I'm not very familiar with markdown, so possibly there's a working solution i've read, but didn't fully understand how to code it into my script.
You can use output_file argument.
rmarkdown::render("my_md_file_path_and_name.Rmd",
output_file = '/file/path/out.html')
I want to use spin() function for my R script to produce rmarkdown .Rmd file. Instead, RStudio returns .md file. This happens in RStudio Preview version 1.1.331.
My R file is from https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R. I downloaded it and put inside my working dir. I then typed spin("test.R", format = "Rmd") in the Console pane.
When I clicked enter, in the console, it showed processing file:knitr-spin.Rmd then output file: knitr-spin.md. Then knitr-spin.html and knitr-spin.mdwere visible, but there was no knitr-spin.Rmd in the working folder.
For the next try,
I clicked the Compile Report button
I chose MS Word outputs
Unfortunately, I still received a folder and an html file and no .Rmd file. Not even .md file was created.
The last time I used spin() or Compile Report was inside RStudio 1.0.153 and it worked nicely and I got .Rmd files, html and MS Word files.
What have I done wrong or I missed?
This should work:
spin("file.R", knit = FALSE) # convert to Rmd only
According to the documentation, argument spin appears to be what you're after:
precious
logical: whether intermediate files (e.g., .Rmd files when format is "Rmd")
should be preserved; default FALSE if knit == TRUE and input is a file
Next to the Knit button there's a sprocket. Click on the Advanced tab and check "Keep markdown source file".
I am using RStudio to knit an .Rnw file to .pdf. This .Rnw file is stored in directory that is under git version control. This directory also contains a .RProj file for the project.
I collaborate with colleagues who don't know the first thing about .Rnw files and git. These colleagues want to open a Word file and track change their hearts out. So I give the people what they want.
Everyone needs access, so storing the Word file on a cloud service like Box makes sense. In the past I created a subfolder in my repo that I shared—keeping everything within the root directory—but this time around I needed to store the file in a shared folder that someone else created. So my solution was to copy the Word file from this shared directory to my repository.
Technical Approach
I don't know how to make this a reproducible problem, but hopefully you will give me some latitude since I'm trying to make my work fully reproducible ;)
Let's say that my .Rnw file is stored in repoRoot/subfolder. Since knitr changes the working directory to subfolder where this .Rnw file is located, the first chunk sets the root.dir one level up at the project root.
<<knitr, include=FALSE>>=
library(knitr)
opts_knit$set(root.dir=normalizePath('../')) # go up 1 level
#
The next chunk copies the Word file from the shared folder to my git repo and runs the analysis file. The shared directory path is hard coded to my machine, which is the problem I'm writing for your help solving.
file.copy(from='/Users/ericpgreen/Box Sync/Project/Paper/draft.docx',
to='subfolder/draft.docx', # my repo
overwrite=TRUE)
source(scripts/analysis.R) # generates objects we reference in the .docx file
After adding \begin{document}, I include a chunk where I convert the .docx file to .txt and then rename it to .Rnw.
# convert docx to txt
system("textutil -convert txt 'subfolder/draft.docx'")
# rename txt to .Rnw
file.rename('subfolder/draft.txt',
'subfolder/draft.Rnw')
The next child chunk calls this .Rnw file that contains the text of the Word file with references to R objects included through \Sexpr{}:
<<include-draft, child='draft.Rnw', include=FALSE>>=
#
This works just fine for me. Whenever I knit the .Rnw file it grabs the latest version of the .docx file that my colleagues have edited (complete with track changes and comments) and, in a later step not shown here, returns the .pdf file to the shared folder.
Problem to Solve
This setup meets almost every need for me, except that the initial file.copy() command is hard coded to my machine. So if someone in my group clones my repo (e.g., research assistants who DO use version control), it won't run out of the box. Is there a workaround to hard coding in this type of case?
Ultimately you won’t get around hard-coding paths that are outside your control, such as paths to network shares. What you can and should avoid is hard-coding these paths in your documents.
Instead, relegate them to configuration files and/or environment variables (which, again, will be controlle by configuration files, to with .bashrc and similar). The simplest approach is then to use
network_share_path = Sys.getenv('NETWORK_SHARE_PATH',
stop('no network share path configured'))
file.copy(from = network_share_path, to = 'subfolder/draft.docx', overwrite = TRUE)
I would love to use some shell script based pdfcrop on all the graphics.pdf created from my Sweave report. Hence i'd be nice if all the graphics were stored to a subfolder instead of being dropped where all the latex stuff like .aux .log files (and so on) is located. If I just had to use the crop script on ALL files in a particular directory it would be much easier.
Here's my Sweave chunk:
\begin{figure}[htbp]
\begin{center}
<<fig=true,echo=false>>=
print(mygraph)
#
\caption{my graph}
\end{center}
\end{figure}
If I run the sweave code mygraph.pdf is stored in the same directory as the report.tex file itself.
Is there a way to store this file in an existing subfolder like /graphics ?
Insert this:
\SweaveOpts{prefix.string=foo/bar}
into your Sweave source file, preferably up the top in the preamble. The above line is taken from the Sweave FAQ, and indicates that the created figures will be stored in the subdirectory (of the directory where the Sweave source is) foo, and each image filename will begin with bar. This, and a whole lot more is discussed in the Sweave FAQ.
Okay RTFM, ran2.
Here's how:
\SweaveOpts{prefix.string=graphics/report}
See also: the manual.
graphics is directory and report is a substring you might wanna use. At least you'll find SO better on google :)
EDIT:
For the sake of completeness I'll add my several lines of shell script here:
#!/bin/sh
R CMD Sweave report.Rnw
for file in `ls graphics`;
do pdfcrop "$file" graphics/"$file"
done
pdflatex report.tex
open report.pdf
Maybe some of you want to use $1 as a directory argument.