Q:

assigning an R object to run a chain of command

I am learning to use bookdown to render a PDF and Word document simultaneously from the same Rmd file. Since I want to view the PDF output using Sumatra PDF reader, which cannot auto reload modified documents for documents shown in the ebook UI (refer to ReloadModified Documents in https://www.sumatrapdfreader.org/settings.html). I have created a single line to run everytime when I render the PDF file and view it in Sumatra PDF reader like below:
bookdown::render_book('index.Rmd',output_format = 'all',new_session = TRUE,preview=TRUE);
system('cmd.exe',input='taskkill /IM SumatraPDFPortable.exe');
system('cmd.exe',input='"C:/PortableApps/SumatraPDFPortable/SumatraPDFPortable.exe" "E:/output.pdf"')

I would like to know if it is possible to assign a R object (say: rendernow) to this line so that every time when I type "rendernow" and enter in R console, the above line will be executed.
Thanks!

A:

Just make it a function:
rendernow <- function() {
  bookdown::render_book('index.Rmd',output_format = 'all',new_session = TRUE,preview=TRUE);
  system('cmd.exe',input='taskkill /IM SumatraPDFPortable.exe');
  system('cmd.exe',input='"C:/PortableApps/SumatraPDFPortable/SumatraPDFPortable.exe" "E:/output.pdf"')
}

Then you can invoke it via rendernow() (so you need the parenthesis) 

Just for the fun of it, here's a solution where you do not need the parenthesis:
rendernow <- structure("", class = "rendernow")
print.rendernow <- function(x, ...) {
  bookdown::render_book('index.Rmd',output_format = 'all',new_session = TRUE,preview=TRUE);
  system('cmd.exe',input='taskkill /IM SumatraPDFPortable.exe');
  system('cmd.exe',input='"C:/PortableApps/SumatraPDFPortable/SumatraPDFPortable.exe" "E:/output.pdf"')
}

Then you just type rendernow and your steps are invoked.

