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

Specify working directory #162

Closed
gabysbrain opened this issue Mar 3, 2012 · 10 comments
Closed

Specify working directory #162

gabysbrain opened this issue Mar 3, 2012 · 10 comments
Labels
feature Feature requests
Milestone

Comments

@gabysbrain
Copy link

I have an R script that reads a source markdown from stdin, saves it to a temporary file (in /tmp), runs it through knitr, and then writes the processed file to stdout. This lets me get a live preview of my R computations in Marked.app on the mac.

I used to set the input.dir option before calling knit to change the working directory in which knit processes the R code. This way I could keep data references relative to my source document. With version 0.3.0 input.dir looks like it's always set to the directory of the input file. This doesn't work in my case where the input and output file are temporary files.

Would it be possible to check first if input.dir is set and only change it if it's not? The other option is to have a separate option for specifying the working directory.

@yihui
Copy link
Owner

yihui commented Mar 3, 2012

Thanks for the suggestion! I agree I should make the input/output more flexible and should not tie them to physical files. I will see what I can do about this.

@gabysbrain
Copy link
Author

That would be great! Right now my workaround is to create the temporary files in the working directory I want to use which isn't ideal as it clutters up the directory.

Thanks for looking into this!

@yihui yihui closed this as completed in 6bab658 Mar 5, 2012
yihui added a commit that referenced this issue Mar 5, 2012
@yihui
Copy link
Owner

yihui commented Mar 5, 2012

Now it supports stdin() and stdout(). Here is an example to read input from stdin() and write output in stdout().

library(knitr)
pat_html()  # patterns for reading input
knit(stdin(), stdout())

this is a markdown example in stdin():

<!--begin.rcode setup, include=FALSE
render_markdown() # setup output hooks
## can also try render_gfm()
end.rcode-->

try some code:

<!--begin.rcode hello
1+1
rnorm(5)
end.rcode-->

OK, time to press Ctrl+D now.

@chiphogg
Copy link

Another important use-case is people writing scripts. Example: I'm building a website with nanoc, and I want it to handle .Rmd source for blog posts. Basically, I want to use knitr like a text filter (though one with the side-effect of producing plots, etc.).

I had great difficulty getting your code to work. A few tips:

  1. To work with Rscript, replace stdin() with file("stdin").
  2. Problem: stdout is polluted with progress bars by default!
    • The solution is to add the line opts_knit$set(progress=FALSE, verbose=FALSE)
    • New Problem: Rscript -e now fails no matter what I do! (Even if I preface with require(methods); require(utils))
      Error message:
      Error: could not find function "opts_knit"
    • New Solution: put the R code in a separate script file; use Rscript myscript.R instead of Rscript -e "mycode"

I'm adding this here for posterity in case anyone comes here by googling "knitr stdin" (as I did), hoping to turn knitr into a text filter.

@yihui
Copy link
Owner

yihui commented Sep 29, 2012

Thanks for the exploration! I think the error message about opts_knit indicates you wrote wrong code like opts_knit() but it is not a function; instead you should use opts_knit$set().

The primary use of knitr is through files, and I do not quite encourage you to rely on stdout in R, because it is very easy to "pollute" it through a careless cat() or print().

@chiphogg
Copy link

I agree it's easy to do that, but I think it's probably OK for a standalone script which I can maintain and reuse many times.

BTW, I was using opts_knit$set(), which made the error very puzzling to me! After much googling, I found others had encountered similar problems. It seems to be related to differences between Rscript's runtime and R's. Though as I mentioned, the usual fix (i.e., require(methods); require(utils)) didn't seem to work for me.

@yihui
Copy link
Owner

yihui commented Sep 30, 2012

Yes, you wrote opts_knit$get() but what was passed R was opts_knit(); the reason is you used double quotes after Rscript -e, and your shell did variable expansion on $set (which was empty).

http://www.howtogeek.com/howto/29980/whats-the-difference-between-single-and-double-quotes-in-the-bash-shell/

So the solution is Rscript -e 'library(knitr); opts_knit$set(...); knit(...)'.

@chiphogg
Copy link

chiphogg commented Oct 1, 2012

Total facepalm. Rookie mistake. I can't believe I used the double quotes. Thanks for pointing that out -- that had been bugging me!

@yihui
Copy link
Owner

yihui commented Oct 1, 2012

That is fine. I have been bitten by $ for many times, especially in Makefiles (I often forget to escape it or escape it in the wrong way).

@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
feature Feature requests
Projects
None yet
Development

No branches or pull requests

3 participants