Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get output file to open automatically using render() function in RMarkdown

Tags:

r

r-markdown

This is a really small point, but it's important for something bigger that I'm creating.

When I run a .Rmd file within RStudio by pressing the "Knit Word" button, the Word file that is created opens automatically.

However, when I run the file using the render() function, the file is created but it does not open -- I have to navigate to the file location and open it manually.

How can I get the output file to open automatically using the render() function?

like image 771
user33102 Avatar asked Apr 29 '15 15:04

user33102


3 Answers

I still don't know how to open the file directly from render() but another option is to use the following, where "example.Rmd" is a .Rmd file:

render("example.Rmd")    
system2("open","example.docx")
like image 108
user33102 Avatar answered Oct 26 '22 12:10

user33102


You cannot do it directly with the render function. But you can easily open the document using e.g. browseURL.

filepath <- "C:/test"
render(file.path(filepath, "test.Rmd"))
browseURL(file.path("file:/", filepath, "test.docx"))
like image 4
shadow Avatar answered Oct 26 '22 12:10

shadow


I am using the params option in my YAML and want users to be forced to choose the desired parameter. Therefore, I'm using the knit option in my YAML so that I can specify to the file that I want it to ask users for parameter input. I also wanted the file to open automatically. I used the following code to:

  1. ask my user which parameter choice to use
  2. specify a file name and save location
  3. open the file automatically when done.

Code sample

---
title: "Clean data"
subtitle: "Step 2 and 3"
output:
html_notebook:
  theme: readable
  highlight: tango
params:
  Choose:
    value: Step 2
    choices:
      - Step 2
      - Step 3
    input: select
    multiple: no
knit: (function(inputFile, encoding) { 
    out_dir <- "";
    rmarkdown::render(inputFile, params = "ask",
        encoding = encoding, 
        output_file = file.path(dirname(inputFile), 
            out_dir, 'Clean data Step 2.html'));
        browseURL(file.path(dirname(inputFile), 
            'Clean data Step 2.html')) })
---
like image 1
Nova Avatar answered Oct 26 '22 13:10

Nova