title: knitr: Modifying plot hook -- inconsistent
body: <p>I am trying to modify plot hook. However, the behaviour is for some reason dependent on whether I make this modification in compiled file or before that. I would like to do it before that (more files). </p>
  
  <p>Minimal example:</p>
  
  <pre><code>```{r test}
     print("this is test")
     plot(1:10)
  ```
  </code></pre>
  
  <p>With this hook modification:</p>
  
  <pre><code>```{r setting, echo=FALSE}
     plot_hook = knit_hooks$get("plot")
     new_plot_hook = function(x, options){
         x = paste0("{random-change}", x)
         plot_hook(x, options)
         }
    knit_hooks$set(plot=new_plot_hook)
  ```
  </code></pre>
  
  <p>If set in file, before code-chunk, it compiles into:</p>
  
  <pre><code>```r
     print("this is test")
  ```
  
  ```
  ## [1] "this is test"
  ```
  
  ```r
     plot(1:10)
  ```
  ![plot of chunk test]({random-change}figure/test-1.png) 
  </code></pre>
  
  <p>However, when I do modification before running knit, I get:</p>
  
  <pre><code> print("this is test")## [1] "this is test"
   plot(1:10){random-change}figure/test-1.pdf
  </code></pre>
  
  <p>Why? What is going wrong? Why knitr won't let me modify hook before file compilation? How to solve it?</p>
  
  <p>edit: I probably found an answer:
  <a href="https://stackoverflow.com/questions/28268584/trying-to-set-knitr-document-output-hook-results-in-code-chunk-line-breaks-bei?rq=1">Trying to set Knitr &#39;document&#39; output hook results in code chunk line breaks being lost</a></p>
  
  <p>So I first need to call <code>render_markdown()</code> to load markdown chunk setting, which notably changes code in knit_hooks$get("plot"). Then I can run my code and knit my Rmakrdown with required output.</p>
  
  <p>However, I know how, but I still don't know why. Yihui in linked answer mentions, that hooks should be set only inside file. My theory is that while calling knit, knitr detects file type and thus required pattern type and set hooks for that specific format. But not if I change it prior to calling knit.</p>