Knit2html not replicating functionality of Knit HTML button in R Studio - r

I'm trying to write a Bash script in Ubuntu 10.04 that opens a Python file which exports a CSV, and then runs the following Rscript with the goal of exporting a HTML with plots from Dashboard.Rmd:
require(knitr)
setwd('/home/sensors/Desktop/')
knit2html('Dashboard.Rmd')
browseURL('Dashboard.html')
Dashboard.Rmd is an R markdown that calls read.csv on the csv from the first step, makes a data frame and creates plots, but that part's working fine. According to this, I figure that Rscript should replicate the action of pressing "Knit HTML" in R Studio. However, the html it creates is identical to the last time Knit HTML was pressed; i.e. even if the CSV is different, the html doesn't reflect the change.
I also tried using a separate line for knit and markdownToHTML with the same effect. It seems like it doesn't source the code from the Rmd when performing knit. It does update the html properly when I enter the commands from that Rscript into the console of R Studio with Dashboard.Rmd open. However I'm not sure how to translate that into a Bash script. I also tried knit2html with envir=new.env(), envir=R_GlobalEnv, and envir=parent.frame() with no luck. Any help would be appreciated!

So it turns out that this was an artifact of cache=TRUE -- the HTML file was not changed because everything was cached.

Related

Execute all R chunks of an external Rmd file from an R script

I have a .Rmd file called f1.Rmd (containing a mix of text and R chunks) and an R script called f2.R.
I would like to insert a set of R instructions in f2.R that would execute all the R chunks contained in f1.Rmd, in such a way that all variables created in f1.Rmd would be created in my current R session if I source f2.R
(similarly to what happens when clicking on "Run" -> "Run all chunks below from the Rstudio menu").
If you render the f1.Rmd file from your current environment, this should happen.
You can use rmarkdown::render() from the console or a .R script. This will create all the variables in your current environment. It will also have the side effect of making the document.
When you use the knit button in RStudio, this launches the render in a new r session as a background process.
See also the envir option for render.
See also this answer for other options. knitr: run all chunks in an Rmarkdown document
It also depends what your .Rmd is doing.

Can't Knit R Markdown File When I Use ggplot with it

I'm currently doing an R Markdown File of an analysis I recently finished using R. With that, whenever I add details, I click Knit to ensure everything I added goes well on the HTML knit file.
I noticed that when I used ggplot on my R Markdown file, the file doesn't knit anymore and it says:
Error while opening file - No Such File or directory.
But when I deleted the ggplot command, it gets knitted again.
But this time, without the visualization, it gets knitted again.
UPDATE: I see this error message on the console tab everytime I use ggplot on my .rmd file.
So with that, I call on install.packages("ggplot2") & library("ggplot2") on my .rmd file. However, I get this error instead.
How will I be able to retain the ggplot command and at the same time be able to knit the file? As an added reference, the ggplot command executes on the .rmd file, it just doesn't knit. Can someone help me please?

Run only R commands in rmd file

I have a large Markdown file (*.Rmd) and want to execute only the R commands in that file (not the text) in RStudio. How can this be done?
If I mark the whole document and try to execute it, R tries to execute also the text in the Markdown file, which, of course, results in errors.
Click on the arrows at the top right of the chunks:

rmarkdown running child chunks from inside RStudio

I've been moving some of the code for a report I'm writing to child .rmd files. I want to run these chunks by clicking on the green arrow (top right):
But this doesn't work in RStudio, is this a feature or a bug?
This has not been implemented in RStudio yet, and probably won't be for some time.
However, you can write your R code in a separate file, reference it in R Markdown chunks, and execute those chunks interactively in RStudio. The way to do this is with knitr's code externalization feature. You can read about how to use it in R Markdown notebooks here:
https://rmarkdown.rstudio.com/r_notebooks.html#executing_code (scroll down a bit to Executing External Chunks)
More on code externalization with knitr here:
https://yihui.name/knitr/demo/externalization/

How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?

What commands are run when pressing "Knit HTML" on an R Markdown file in Rstudio 0.96?
My motivation is that I might want to run the same command when I'm in another text editing environment or I might want to combine the command in a larger makefile.
Basic Script
So now that the R markdown package has been released, here is some code to replicate the features of Knit to Html.
require(knitr) # required for knitting from rmd to md
require(markdown) # required for md to html
knit('test.rmd', 'test.md') # creates md file
markdownToHTML('test.md', 'test.html') # creates html file
browseURL(paste('file://', file.path(getwd(),'test.html'), sep='')) # open file in browser
where test.rmd is the name of your R markdown file.
Note that I'm not 100% confident about the browseURL line (hence my question here about opening files in a web browser).
markdownToHTML Options
The good thing about markdownToHTML is that there are heaps of options in how the HTML is created (see ?markdownHTMLOptions). So for example, if you want just a code fragment without all the header information, you could write:
markdownToHTML('test.md', 'test.html', options='fragment_only')
or if you don't like hard wrapping (i.e., inserting line breaks when there are single manual line breaks in the markdown source), you can omit the 'hard_wrap' option.
# The default options are 'hard_wrap', 'use_xhtml',
# 'smartypants', and 'base64_images'.
markdownToHTML('test.md', 'test.html',
options=c('use_xhtml', 'base64_images'))
Makefile
This could also all be added to a makefile perhaps using Rscript -e (e.g., something like this). Here's a basic example makefile I put together, where test indicates that the rmd file is called test.rmd.
RMDFILE=test
html :
Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); markdownToHTML('$(RMDFILE).md', '$(RMDFILE).html', options=c('use_xhtml', 'base64_images')); browseURL(paste('file://', file.path(getwd(),'$(RMDFILE).html'), sep=''))"
The makefile uses my preferred markdown options: i.e., options=c('use_xhtml', 'base64_images')
Put Sys.sleep(30) in a chunk and you will see clearly what commands are called by RStudio. Basically they are
library(knitr); knit() to get the markdown file;
RStudio has internal functions to convert markdown to HTML;
The second step will be more transparent in the next version of the markdown package. Currently you can use knitr::knit2html('your_file.Rmd') to get a similar HTML file as RStudio gives you.
Update on 2019/09/17: The above answer applies to RStudio v0.96 (in the year 2012). Now R Markdown is compiled through rmarkdown::render(), which uses Pandoc instead of the retired R package markdown. See the post Relationship between R Markdown, Knitr, Pandoc, and Bookdown for more details.
Very easy command line method from knitr in a knutshell:
R -e "rmarkdown::render('knitr_example.Rmd')"
This requires rmarkdown to be installed with install.packages(rmarkdown) and that pandoc is installed (apparently it comes with Rstudio, see knitr in a knutshell for more details).
So far when I've used this it nicely puts all the plots in the HTML file rather than as images in a figure directory and cleans up any intermediate files, if any; just like compilation in RStudio does.
It seems you should call rmarkdown::render() instead of knitr::knit2html() because a.rmd appears to be an R Markdown v2 document.

Resources