Giter Site home page Giter Site logo

blog's People

Contributors

atusy avatar dependabot[bot] avatar hoxo-m avatar

Stargazers

 avatar

Watchers

 avatar

blog's Issues

mutate_at と map_at %>% as.data.frame の違い

library(dplyr, warn.conflicts = FALSE)
library(purrr)
    
# mutate_at
data.frame(a = 1) %>%
  mutate_at("b", identity)
#> Error in mutate_impl(.data, dots): Evaluation error: object 'b' not found.

data.frame(a = 1) %>%
  mutate_at("b", function(x) "b")
#>   a b
#> 1 1 b

# map_at
data.frame(a = 1) %>%
  map_at("b", identity) %>%
  as.data.frame
#>   a
#> 1 1

data.frame(a = 1) %>%
  map_at("b", function(x) "b") %>%
  as.data.frame
#>   a
#> 1 1

Created on 2019-02-05 by the reprex package (v0.2.1)

rocker で日本語pdf

ユーザーが tlmgr install を使えるようにするため, tinytex::install_tinytex し直す

Dockerfile

FROM rocker/verse

RUN Rscript -e "tinytex::uninstall_tinytex()" \ 
  && sudo -u rstudio Rscript -e "tinytex::install_tinytex()"

すると以下のような Rmd を PDF にできる.
(要ipaex)

---
output:
  pdf_document: 
    latex_engine: xelatex 
header-includes: 
  - \usepackage{bookmark} 
  - \usepackage{xltxtra} 
  - \usepackage{zxjatype} 
  - \usepackage[ipaex]{zxjafont} 
---

あああ

idea: ラッパー関数作成変態編

  • 引数は後から formals(wrapper) <- formals(original) して作る
  • 既定値を変えるには formals(wrapper)$hoge <- value
    • alistを使ってsymbolの既定値を弄る
  • 関数内部で使われている変数 (関数含む) を変更したい時は,
    対象の変数名を関数の引数名にしてしまう

devtools::build_win は deprecated

Warning message:
'devtools::build_win' is deprecated.
Use 'check_win_*()' instead.
See help("Deprecated") and help("devtools-deprecated").

CSS grid

row/col のサイズはマージンやgapを抜いたもの
コンテナの高さを指定しておくと, fr 単位でアイテムを作れる

preに行番号

head-custom.html

<link rel="stylesheet" href="//use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

footer.html

<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/languages/r.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.3.0/highlightjs-line-numbers.min.js"></script>

<script>
  hljs.initHighlightingOnLoad();
  hljs.initLineNumbersOnLoad();
</script>

html_document におけるコードブロックの行番号表示

行番号の背景を無色にする

コードブロックには source, output, warning, error それぞれの場合で異なるクラスを与えておきたい.

これにより,行番号表示を有効にすると source 以外の背景色が source と同じになってしまう問題を CSS でカンタンに解決できる.

knitr で source に Pandoc の fenced code attributes を利用する

---
output: 
  html_document:
    highlight: pygments
---

```{r setup, include = FALSE}
block_class = function (x) {
  if(length(x) == 0) return()
  paste0(".", strsplit(x, "\\s+"), collapse = " ")
}
hook.r = function(x, options, fence = "```") {
  language = tolower(options$engine)
  if (language == "node") language = "javascript"
  if (!options$highlight) language = "text"
  classes = block_class(options$class.source)
  attrs = options$attr.source
  paste0("\n\n", fence, "{.", language, " ", classes, " ", attrs, "}\n", x, fence, "\n\n")
}
hook_source = function(x, options) {
  x = knitr:::hilight_source(x, "markdown", options)
  hook.r(paste(c(x, ""), collapse = "\n"), options)
}
knitr::knit_hooks$set(source = hook_source)
```

```{r, attr.source=".numberLines startFrom='10'"}
library(magrittr)
iris %>%
  .$Sepal.Length %>%
  density %>%
  plot
```

fig.cap 内で使える markdown syntax

*italic*
**bold**

などは OK

コードハイライト

```{r iris, fig.cap = "`include_graphics`"}
knitr::include_graphics(system.file("img", "Rlogo.jpg", package = "jpeg"))
```

は pdf_document2 で

\begin{figure}\caption{include_graphics} \end{figure}Figure 1:1

といったナゾの事態に.

相互参照

  • \@ref(fig:iris) : Error: '@' is an unrecognized escape in character string starting ""@"
  • \\@ref(fig:iris) : Undefined control sequence.
  • @ref(fig:iris) : GOOD

どうする?

(ref:foo) figure caption 形式を利用する.

(ref:foo) r var` とすると var の中身を評価してキャプションに入れられる.
glue を併用して改行つきのキャプションを作れる.

ちなみに

(ref:foo) foo
(ref:bar) bar

としておくと (ref:foo) (ref:bar)foo bar になるので,複数のテキストを簡単に合体させられる.

Gnome3 のアイコン

Adwaita はわかりやすいがクッソださい.
Papirus 系はよさげだが,Inkscape のアイコンが意味不明.
Newaita 中間的でよい.Document Viewer など,一部ごちゃっとしたアイコンもある.

Rmd + XeLaTeX でのフォント設定

mainfont: Noto Serif
sansfont: Noto Sans
monofont: Noto Mono
mathfont: Noto Serif
CJKmainfont: Noto Serif CJK JP

といった具合にする.数式も Noto にできてシームレス.

mainfontoptions, sansfontoptions, monofontoptions, mathfontoptions, CJKoptions

によってオプション設定も可能.

https://www.pandoc.org/MANUAL.html#fonts

WARN: .RSSLink is deprecated

Building sites … WARN 2019/04/16 19:40:35 Page's .RSSLink is deprecated and will be removed in a future release. Use the Output Format's link, e.g. something like:
{{ with .OutputFormats.Get "RSS" }}{{ . RelPermalink }}{{ end }}.
WARN 2019/04/16 19:40:35 Page's .URL is deprecated and will be removed in a future release. Use .Permalink or .RelPermalink. If what you want is the front matter URL value, use .Params.url.

図表リストでショートキャプションを利用する

fig.scap

https://yihui.name/knitr/options/

Arbitary

```{r foo, fig.cap="Long caption.", fig.scap="Short caption", out.extra=''}

First sentence

```{r foo, fig.cap="Short caption. Additional sentences.", fig.scap=NULL, out.extra=''}

The following won't work.

(ref:hoge) Short caption. Additional sentences.

```{r foo, fig.cap="(ref:hoge)", fig.scap=NULL, out.extra=''}

knitr::kable

kable(table, caption = "Long", short.caption = "Short")

rstudio/bookdown#543

xtable::xtable

xtable(table, caption = c("Long", "Short")

https://cran.r-project.org/web/packages/xtable/xtable.pdf

syntax highlight を有効にした時のコードブロックのマージンについて

pandoc 由来の syntax highlight を利用すると,pre が pre.sourceCode に変わり,div.sourceCode で囲われる.
このため,pre.sourceCode の margin をなくし,div.sourceCode に margin 1em 0 を与えるようだ.

html_document では bootstrap が pre { margin: 0 0 10px} を指定しているので,これに合わせて div { margin: 0 0 10px } にした方が見た目の変化が最小限に済む

Shiny document

input と output は異なるチャンクに分けられる

以下は等価

1つに纏めたもの

textInput("text", label = "text")
renderText(input$text)

2つに分けたもの

textInput("text", label = "text")
renderText(input$text)

ある output に応じて他の output を変更する

output$foo に render 結果を保存しておき、明示的に UI として出力させるようにしておく

output$mtcars <- renderPlot(plot(mtcars$wt, mtcars$mpg))
plotOutput("mtcars", dblclick = dblclickOpts(id = "mtcars_info"))
renderPrint(input$mtcars_info)

shiny::req

req() に渡した変数が NULLFALSE の場合、 reactive() の評価を中断する。
中断された reactive な変数を利用した plot などは表示されない。

---
runtime: shiny
output: 
  html_document: default
---

```{r, include = FALSE}
knitr::opts_chunk$set(echo = FALSE)
```


```{r}
x <- NULL
y <- reactive({
  req(x)
  1
})
output$y <- renderPlot(plot(y()))
plotOutput("y")
```

Roxygen2でリンクを貼る

#' @seealso [Object] # パッケージ内
#' @seealso \link[Package]{Object} #パッケージ外
  • パッケージ内の場合は、関数であることを明治するために Object() という書き方ができる。
  • パッケージ外の場合、pkgdownは rdocumentation.org へのリンクを貼ってくれる

knitエンジン: text

knitr::knit_engines$set(
  text = function(options) {
    options$results <- 'asis'
    options$echo <- FALSE
    knitr::engine_output(options, NULL, options$code)
  }
)
あいうえお
かきくけこ

GatsbyJS

導入

sudo pacman -Syu nodejs npm git
sudo install -g gatsby-cli

linuxbrew で nodejs のインストールに失敗したので、システムレベルで導入

Hello world

gatsby new hello-world https://github.com/gatsbyjs/gatsby-starter-hello-world
cd hello-world
gatsby develop --host 0.0.0.0 --port 8000

リモートからアクセスできるよう、--host0.0.0.0 にした (既定値: localhost)

dplyr::mutate_if と purrr::modify_if の違い

mutate_if は適用する関数を名前付きリストで複数種指定できる.
リストに与えた名前は元の変数名の suffix になる.

iris %>% mutate_if(is.numeric, list(a = ~ rnorm(., .), b = ~ identity(.)))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.