As you can see, I write my output files in the output\ directory, which is defined in the first R chunk as
output_dir <- "../output"
and then use file.path to build my path names accordingly. The .Rmd file is in code\ directory, thus the HTML report is saved there. Is this possible to save the HTML output into output\, instead than into code\?
If you set your project directory (I'd recommend using an .Rproj file to do so— see this Jenny post for more on that) above where you're keeping the Rmd file, you should be able to still set the output directory to whatever path using path = here::here("output", "output_filename") (or some variation thereupon). I wouldn't recommend separating your product documents from their source (I write out results, as you do, to a different folder, too), but it should be possible.
... with the second calling rmarkdown::render, which has an output_dir argument, on the first. There's the downside that when working interactively I need to run the second script and can't simply click the RStudio IDE "knit" button when editing the first one.
Interestingly, it looks like @Yihuidoesn't endorse this usage, but I guess I'll stick with it until it visibly breaks something.
Would you mind providing small reprex of what you mean? I'm having a hard time understanding what exactly to do. I'm familiar with here::here() maybe a small example .RMD file could help?
@mara yes, the project.Rproj file is in the project\ directory, which is one level above the directory code\, where the .Rmd file resides. Probably my first post wasn't clear enough:
project\
project.Rproj
code\
myreport.Rmd
myreport.html
data\
fakedata.csv
output\
results.csv
Hopefully my terrible attempt at representing a folder hierarchy in Markdown clarifies things
I know about here, but I'm not sure how I can use it to tell knitr to put the myreport.html in the output\ folder...can you show me how? I usually use the "Knit" button to render my report, but I can use the CLI if you show me how.
@Frank his sounds interesting. However, since I've always used the RStudio "Knit" button to generate my report, I'm not sure how to render it from command line. I guess the review.r should look something like:
Right? It's annoying to have to define the same variable (output_dir) in two different files (review.r and myreport.Rmd) because surely once I change it in one of them, I'll forget to make the corresponding change in the other one. I guess that if I want to avoid this kind of problem, I'll have to learn about RMarkdown parametric reports....
I understand this is not the preferred approach, but I don't see any other way.
PS just for clarity in case someone else like me is not familar with R Markdown parametric reports the value output_dir: "../output" in the YAML header is just a default. If I change output_dir in review.r I don't need to change it again in the YAML header: the new parameter value is automatically passed to the .Rmd doc.
Ah, based on your question, I thought output_dir was something you had figured out, but weren't able to get the correct path there.
I don't know of anything that would allow this to happen by hitting the knit button. The best suggestion I have would be to look into using make files.
The ezknitr package may do exactly what you want. Look at the motivation and simple use case, I may be wrong but it seems your situation is similar to the exact problem aimed to be solved by that package
Sh*t! You're right: this doesn't work the way I expected. params is working in the sense that the .Rmd file writes its output files to the correct directory, but the HTML report is still written to the wrong directory! But I think I'm getting there...just you wait
@Frank note that the default value for params$output_dir is now a non-existent directory! So if parameters substitution didn't work, then I would get an error.
After running render_report.r, I get:
project\
project.Rproj
code\
render_report.r
myreport.Rmd
output\
myreport.html
results.csv
Phew! It was harder than I thought. @mara you were right! There's no way to do this with the "Knit" button. However, this solution at least doesn't require me to leave RStudio. Actually, the "Knit" button could place myreport.html in the project\ dir, which is also fine (better than in the code dir, IMO). Still, I have an output dir, why not use it and keep everything nice and tidy ?
You can use a function to define where the output of of your rendered report goes.
I sometimes use this project workflow by Joris Muller and find it can be really helpful.
The make.reports.R script allows you to choose where the output file goes.
You can also change the output style of the html if you want - an example of my own use of this is here. Note the report_dir = "~\my_reports_folder" argument under the "Main function" heading.
@daattali cool! I didn't know about your ezknitr. It seems to fit my use case very well. I'm only a bit worried about leaving knitr (and rmarkdown, which depends on it), which are widely adopted, for another package...I'll think about that. Thanks!
Wooow! There's even a command to directly open the HTML file in the browser (btw, that would be a useful option for RStudio). At a first look, it seems like a more advanced version of my solution. I'll study that. Thanks!
It's not leaving knitr and rmarkdown, it uses knitr and rmarkdown (you can
look at the source code), all it does it take care of setting all the
directories correctly. It's very lightweight
Haha don't worry as a package developer you get a lot of actual rude
feedback, but any feedback is always useful, even just understanding
potential user concerns:)
I try my best to never be rude to OS developers. I really dislike when people are impolite in GitHub issues. Sometimes it looks like they feel so entitled, instead than grateful as they should be...I hope it doesn't happen often! And thanks again for your package!