Hi All, I have to compile a report for the management and decided to use RMarkdown and knitr. I compiled all needed plots (using separate R scripts) before compiling the report, thus all plots reside in my graphics directory. The RMarkdown report needs to access these files. I have defined ```{r setup, include = FALSE} knitr::opts_knit$set( echo = FALSE, xtable.type = "html", base.dir = "H:/2017/Analysen/Kundenzufriedenheit/Auswertung", root_dir = "H:/2017/Analysen/Kundenzufriedenheit/Auswertung", fig.path = "results/graphics") # relative path required, see http://yihui.name/knitr/options ``` and then referenced my plot using <img src = "email_distribution_pie.png"></img> because I want to be able to customize the plotting attributes. But that fails with the message "pandoc.exe: Could not fetch email_distribution_pie.png". If I give it the absolute path "H:/2017/Analysen/Kundenzufriedenheit/Auswertung/results/graphics/email_distribution_pie.png" it works fine as well if I copy the plot into the directory where the report.RMD file resides. How can I tell knitr to fetch the ready-made plots from the graphics directory? Kind regards Georg
On 08/06/2017 7:15 AM, G.Maubach at weinwolf.de wrote:> Hi All, > > I have to compile a report for the management and decided to use RMarkdown > and knitr. I compiled all needed plots (using separate R scripts) before > compiling the report, thus all plots reside in my graphics directory. The > RMarkdown report needs to access these files. I have defined > > ```{r setup, include = FALSE} > knitr::opts_knit$set( > echo = FALSE, > xtable.type = "html", > base.dir = "H:/2017/Analysen/Kundenzufriedenheit/Auswertung", > root_dir = "H:/2017/Analysen/Kundenzufriedenheit/Auswertung", > fig.path = "results/graphics") # relative path required, see > http://yihui.name/knitr/options > ``` > > and then referenced my plot using > > <img src = "email_distribution_pie.png"></img> > > because I want to be able to customize the plotting attributes. > > But that fails with the message "pandoc.exe: Could not fetch > email_distribution_pie.png". > > If I give it the absolute path > "H:/2017/Analysen/Kundenzufriedenheit/Auswertung/results/graphics/email_distribution_pie.png" > it works fine as well if I copy the plot into the directory where the > report.RMD file resides. > > How can I tell knitr to fetch the ready-made plots from the graphics > directory?Put the path in a variable, and use that in the img tag, e.g. ```{r} gdir <- file.path(knitr::opts_knit$get("base.dir"), "graphics") ``` <img src = `r file.path(gdir, "email_distribution_pie.png")`></img> If you think that is still too much typing, you could make a little function to turn a string like "email_distribution_pie" into the fully specified path. HOWEVER: Your workflow may not be ideal. It is almost always better to produce the graphs in code that's in the document, rather than in separate R scripts. This makes it *much* easier to modify the plots and be sure that the latest version makes it into the report. If the plots require lots of code and you think it would be distracting in the source of your document, then it's time to make a private package, and put the code in there. In the document, just include calls to functions from that package. Duncan Murdoch
Why do you have to set the base.dir option? Regards, Yihui -- https://yihui.name On Thu, Jun 8, 2017 at 6:15 AM, <G.Maubach at weinwolf.de> wrote:> Hi All, > > I have to compile a report for the management and decided to use RMarkdown > and knitr. I compiled all needed plots (using separate R scripts) before > compiling the report, thus all plots reside in my graphics directory. The > RMarkdown report needs to access these files. I have defined > > ```{r setup, include = FALSE} > knitr::opts_knit$set( > echo = FALSE, > xtable.type = "html", > base.dir = "H:/2017/Analysen/Kundenzufriedenheit/Auswertung", > root_dir = "H:/2017/Analysen/Kundenzufriedenheit/Auswertung", > fig.path = "results/graphics") # relative path required, see > http://yihui.name/knitr/options > ``` > > and then referenced my plot using > > <img src = "email_distribution_pie.png"></img> > > because I want to be able to customize the plotting attributes. > > But that fails with the message "pandoc.exe: Could not fetch > email_distribution_pie.png". > > If I give it the absolute path > "H:/2017/Analysen/Kundenzufriedenheit/Auswertung/results/graphics/email_distribution_pie.png" > it works fine as well if I copy the plot into the directory where the > report.RMD file resides. > > How can I tell knitr to fetch the ready-made plots from the graphics > directory? > > Kind regards > > Georg > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
I'd say it is an expert-only option. If you do not understand what it means, I strongly recommend you not to set it. Similarly, you set the root_dir option and I don't know why you did it, but it is a typo anyway (should be root.dir). Regards, Yihui -- https://yihui.name On Fri, Jun 9, 2017 at 4:50 AM, <G.Maubach at gmx.de> wrote:> Hi Yi, > > many thanks for your reply. > > Why I do have to se the base.dir option? Cause, to me it is not clear from > the documentation, where knitr looks for data files and how I can adjust > knitr to tell it where to look. base.dir was a try, but did not work. > > Can you give me a hint where I can find information/documentation on this > path issue? > > Kind regards > > Georg > > > > Gesendet: Donnerstag, 08. Juni 2017 um 15:05 Uhr > > Von: "Yihui Xie" <xie at yihui.name> > > An: G.Maubach at weinwolf.de > > Cc: "R Help" <r-help at r-project.org> > > Betreff: Re: [R] Paths in knitr > > > > Why do you have to set the base.dir option? > > > > Regards, > > Yihui > > -- > > https://yihui.name > > > > > > On Thu, Jun 8, 2017 at 6:15 AM, <G.Maubach at weinwolf.de> wrote: > > > Hi All, > > > > > > I have to compile a report for the management and decided to use > RMarkdown > > > and knitr. I compiled all needed plots (using separate R scripts) > before > > > compiling the report, thus all plots reside in my graphics directory. > The > > > RMarkdown report needs to access these files. I have defined > > > > > > ```{r setup, include = FALSE} > > > knitr::opts_knit$set( > > > echo = FALSE, > > > xtable.type = "html", > > > base.dir = "H:/2017/Analysen/Kundenzufriedenheit/Auswertung", > > > root_dir = "H:/2017/Analysen/Kundenzufriedenheit/Auswertung", > > > fig.path = "results/graphics") # relative path required, see > > > http://yihui.name/knitr/options > > > ``` > > > > > > and then referenced my plot using > > > > > > <img src = "email_distribution_pie.png"></img> > > > > > > because I want to be able to customize the plotting attributes. > > > > > > But that fails with the message "pandoc.exe: Could not fetch > > > email_distribution_pie.png". > > > > > > If I give it the absolute path > > > "H:/2017/Analysen/Kundenzufriedenheit/Auswertung/results/ > graphics/email_distribution_pie.png" > > > it works fine as well if I copy the plot into the directory where the > > > report.RMD file resides. > > > > > > How can I tell knitr to fetch the ready-made plots from the graphics > > > directory? > > > > > > Kind regards > > > > > > Georg >[[alternative HTML version deleted]]