ユーザ用ツール

サイト用ツール


メモ:rmarkdown

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン両方とも次のリビジョン
メモ:rmarkdown [2020/05/12 12:10] – [テーブルなどのラベルを改行する] adminメモ:rmarkdown [2020/07/16 17:25] – [回帰分析の結果出力] Wiki Editor
行 224: 行 224:
   )   )
  
 +===== 図のテキスト回り込み =====
 +
 +例によってRmarkdownからPDFを出力する際の細かい設定は、RmarkdownからLatexを制御しなければならないため、やや複雑になる。他に利用しているパッケージによってはうまく動かないなど、実用レベルではない気もする。とりあえず現状のメモ。
 +
 +  * [[https://stackoverflow.com/questions/54100806/r-markdown-how-do-i-make-text-float-around-figures]]
 +
 +まず、Latexでwrapfigを読み込む。
 +
 +  header-includes:
 +    - \usepackage{wrapfig}
 +
 +ただし、titlesecパッケージを読み込むとうまく動かない。
 +
 +  - \usepackage[compact]{titlesec}
 +
 +
 +bxjsarticleのクラスオプションにeveryparhook=compatを指定しないとうまく動かない。
 +
 +  documentclass: bxjsarticle
 +  classoption: xelatex,ja=standard,everyparhook=compat
 +
 +
 +次に、Rmarkdownで図を出力する際にLatex側に指定する環境を書き換える(たぶん)
 +
 +  ```{r, include = F}
 +  defOut <- knitr::knit_hooks$get("plot" # save the default plot hook 
 +  knitr::knit_hooks$set(plot = function(x, options) {  # set new plot hook ...
 +    x <- defOut(x, options)  # first apply the default hook
 +    if(!is.null(options$wrapfigure)) {  # then, if option wrapfigure is given ...
 +      # create the new opening string for the wrapfigure environment ...
 +      wf <- sprintf("\\begin{wrapfigure}{%s}{%g\\textwidth}", options$wrapfigure[[1]], options$wrapfigure[[2]])
 +      x  <- gsub("\\begin{figure}", wf, x, fixed = T)  # and replace the default one with it.
 +      x  <- gsub("{figure}", "{wrapfigure}", x, fixed = T)  # also replace the environment ending
 +    }
 +    return(x)
 +  })
 +  ```
 +
 + 図を出力するチャンクでwrapfigureオプションを指定する。ここでは図を右寄せで行幅の70%の大きさに指定している。
 +
 +  ```{r echo = F, warning = F, message = F, fig.width=7, fig.height = 6, out.width = ".7\\textwidth", fig.cap = "My Flowchart", fig.align="right", wrapfigure = list("R", .7)}
 +  plot(mpg ~ hp, data = mtcars)
 +  ```
 +
 +ということで、何とか回り込みを設定できるものの、いろいろと問題が多い。素直にRmarkdownから指定できるようになってほしい。
  
メモ/rmarkdown.txt · 最終更新: 2023/06/22 11:00 by Wiki Editor

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki