Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help/Suggestion: How to save a Rmarkdown chunk to file in place of evaluation #1598

Closed
siddbhatia opened this issue Aug 24, 2018 · 2 comments
Milestone

Comments

@siddbhatia
Copy link

siddbhatia commented Aug 24, 2018

Is it possible to save a particular chunk (named or otherwise) as a script file in place of evaluating it ?
Something along the lines:

```{r save_to='script.r'}
a_function<-function(){
    print('Something')
}
```

```{r}
source('script.r')
a_function()
```
@siddbhatia
Copy link
Author

Found this for now

```{r}
knitr::knit_hooks$set(write_chunk = function(before, options, envir) {
    if (before) {
      fileConn<-file(paste0(options$label))
      writeLines(options$code, fileConn)
      close(fileConn)
    }
})
```

```{r script.r ,write_chunk=TRUE}
```


@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants