0

I've tried looking at various places now and I can't figure out what the knit_hook$set function does exactly. I read the R documentation and it didn't really make much sense to me.

I'm trying to understand someone else's R code and there's a line at the beginning that's this

library(knitr)
knit_hooks$set(inline = function(x) { x = as.character(x) }

I searched the code and I couldn't find any other reference to inline, or a version of knit_hook$get. The R documentation says that it takes arguments and returns an output, which sounds exactly the same to me as a generic R function. I've searched online and it looks like some kind of functionality that automatically runs in a chunk of R code within a Sweave document?

I've looked through the other chunks of R code in the script I'm analyzing and I don't see anything that says "inline = True" which is what I expected to see after reading this: https://yihui.org/knitr/hooks/

I'm having a really difficult time understanding what this does. Could someone please explain the functionality and purpose of knit_hook$set with a more simple example?

Edit:This is direct text from the documentation I read

% small.mar does not have to be TRUE, it can be any non-null value

<<myplot, small.mar=TRUE>>=
hist(rnorm(100), main = '')  # no main title
@

That's a line from the documentation I read that I linked above. The code I have does not have anything that says << inline = True>>=

Is it safe to assume if that's not there then this knit_hook$set is superflous then?

Also in the example above, where is the small.mar function being called? I don't see it being used anywhere, does it automatically get applied to the hist function in the example?

Jed Bartlet
  • 1,963
  • 2
  • 11
  • 12
  • 1
    @r2evans I did. I guess maybe I can refine my question. Let me edit – Jed Bartlet Nov 14 '19 at 22:41
  • The hooks shouldn't be logicals, a hook is generally a function. It says that the output hooks *"are used to customize and polish the raw output from chunks"*; specifically, `inline` handles the *"output of inline R code"*. To me, I don't know why this one function would be necessary. – r2evans Nov 14 '19 at 22:41
  • For giggles, have you tried removing it and comparing the output to a previous version of the report? – r2evans Nov 14 '19 at 22:42
  • @r2evans yeah i tried, unfortunately it crashed because of other changes, I'm going to try it again with an older more stable version. I just figured it was worth a shot asking here, because even though I read the Rdocumentation page and the example you linked I still couldn't figure it out, was basically just hoping for a dirt simple example. In the examples in the link you linked, I don't actually see the function being explicitly called (the small.mar and the rgl example) so I'm confused as to how it works if that makes any sense – Jed Bartlet Nov 14 '19 at 22:46
  • You won't see the function being explicitly called, it is called by `knitr` behind the scenes during rendering of the document. A "hook" is a way for you to give `knitr` a function of your creation to be called on specific "things" during rendering. It might be helpful to see https://stackoverflow.com/q/467557, though some of the answers seem confusing. (https://en.wikipedia.org/wiki/Hooking is okay, too.) In general, it's a way for you to control some of the rendering in ways that simple options cannot ... so you write a function to make your changes *programmatically/dynamically*. – r2evans Nov 14 '19 at 22:50
  • Ok, I looked at those, think I have better understanding of what it is. I'm still confused how it's implemented. It looks like the line in my code automatically converts things into characters (this makes sense given what I understand the purpose of the code) to do. What I'm confused about is the syntax/how it specifics what it is and isn't applied to – Jed Bartlet Nov 14 '19 at 22:54
  • Inline rmarkdown is something within a normal not-code paragraph encompassed in single backticks, such as `\`r mean(x)\``. Whenever `knitr` finds that in the Rmd, it executes the R code, which then returns some R object to `knitr`; typically, I believe that `knitr` will automatically do some string conversion (perhaps just implicitly), but this config you have says *"run this function to convert the results from each of those tiny-little inline calls through my function ... and my function converts them to character"*. – r2evans Nov 14 '19 at 23:01
  • If you have no inline-code in your rmarkdown document, then this anonymous function assigned to `inline=` there *is not called*. Otherwise, it is called once for each inline block you have, with the object's output as its sole argument (`x`). – r2evans Nov 14 '19 at 23:02

0 Answers0