简体   繁体   中英

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

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?

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")

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

filepath <- "C:/test"
render(file.path(filepath, "test.Rmd"))
browseURL(file.path("file:/", filepath, "test.docx"))

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')) })
---

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM