knitr icon indicating copy to clipboard operation
knitr copied to clipboard

Split call_block into a function block_params

Open muschellij2 opened this issue 4 years ago • 9 comments

call_block does all the extraction of the parameters necessary, but executes the block. Regarding our email of getting a data.frame of parameters/hash/etc, I think that is what we'd need. I split call_block into a block_params function that extracts the parameters, and then call block_params in call_block.

muschellij2 avatar Jun 26 '20 15:06 muschellij2

# remotes::install_github('muschellij2/knitr')
library(knitr)

knit_df = function(file) {
  x = xfun::read_utf8(file)
  on.exit(knitr::knit_code$restore(), add = TRUE)
  res = knitr:::split_file(x, patterns = knitr::all_patterns$md)
  classes = sapply(res, attr, "class")
  blocks = res[classes %in% "block"]
  params = lapply(blocks, function(el) {
    if (attr(el, "class") %in% "block") {
      knitr:::block_params(el, verbose = FALSE)
    } else {
      NULL
    }
  })
  df = lapply(params, function(x) {
    if (!is.null(x)) {
      keep_names = c("eval", "echo", "engine", 
                     "label", "code", "hash", "cache",
                     "cache.path", "cache.vars", 
                     "cache.lazy", "fig.path", "fig.ext",
                     "fig.env", "error", "warning", "message",
                     "include", "params.src")
      x$code = paste(x$code, collapse = "\n")
      x = lapply(keep_names, function(r) {
        xx = x[[r]]
        if (is.null(xx)) {
          xx = NA
        }
        xx
      })
      names(x) = keep_names
      as.data.frame(x[keep_names])
    } else {
      NULL
    }
  })
  df = do.call("rbind", df)
  return(df)
}
tfile = tempfile(fileext = ".Rmd")
curl::curl_download(
  "https://raw.githubusercontent.com/muschellij2/neuroc/master/brain_extraction/index.Rmd",
  destfile = tfile)

res = knit_df(tfile)
head(res, 10)
#>     eval echo engine           label
#> 1   TRUE TRUE      R           setup
#> 2  FALSE TRUE      R unnamed-chunk-1
#> 3   TRUE TRUE      R            data
#> 4   TRUE TRUE      R         t1_plot
#> 5   TRUE TRUE      R     t1_naive_ss
#> 6   TRUE TRUE      R   t1_naive_plot
#> 7   TRUE TRUE      R           t1_ss
#> 8   TRUE TRUE      R      t1_ss_plot
#> 9   TRUE TRUE      R     t1_ss_plot2
#> 10  TRUE TRUE      R       t1_ss_red
#>                                                                                                                                                                                                                                                                                                                                                                              code
#> 1  library(kirby21.fmri)\nlibrary(kirby21.base)\nlibrary(dplyr)\nlibrary(fslr)\nlibrary(neurobase)\nlibrary(ANTsR)\nlibrary(R.utils)\nlibrary(RColorBrewer)\nlibrary(matrixStats)\nlibrary(ggplot2)\nlibrary(reshape2)\nlibrary(animation)\nlibrary(zoo)\nknitr::opts_chunk$set(echo = TRUE, cache = TRUE, comment = "",\n                      cache.path = "index_cache/html/")
#> 2                                                                                                      source("https://neuroconductor.org/neurocLite.R")\npackages = installed.packages()\npackages = packages[, "Package"]\nif (!"kirby21.base" %in% packages) {\n  neuroc_install("kirby21.base")  \n}\nif (!"kirby21.t1" %in% packages) {\n  neuroc_install("kirby21.t1")  \n}
#> 3                                                                                                                                             library(kirby21.t1)\nlibrary(kirby21.base)\nfnames = get_image_filenames_df(ids = 113, \n                    modalities = c("T1"), \n                    visits = c(1),\n                    long = FALSE)\nt1_fname = fnames$T1[1]
#> 4                                                                                                                                                                                                                                                                                                                             t1 = readnii(t1_fname)\northo2(t1)\nrm(list = "t1")
#> 5                                                                                                                                      library(fslr)\noutfile = nii.stub(t1_fname, bn = TRUE)\noutfile = paste0(outfile, "_SS_Naive.nii.gz")\nif (!file.exists(outfile)) {\n  ss_naive = fslbet(infile = t1_fname, outfile = outfile)\n} else {\n  ss_naive = readnii(outfile)\n}
#> 6                                                                                                                                                                                                                                                                                                                                                                ortho2(ss_naive)
#> 7                                                                                                                   outfile = nii.stub(t1_fname, bn = TRUE)\noutfile = paste0(outfile, "_SS.nii.gz")\nif (!file.exists(outfile)) {\n  ss = extrantsr::fslbet_robust(t1_fname, \n    remover = "double_remove_neck",\n    outfile = outfile)\n} else {\n  ss = readnii(outfile)\n}
#> 8                                                                                                                                                                                                                                                                                                                                                                      ortho2(ss)
#> 9                                                                                                                                                                                                       alpha = function(col, alpha = 1) {\n  cols = t(col2rgb(col, alpha = FALSE)/255)\n  rgb(cols, alpha = alpha)\n}      \northo2(t1_fname, ss > 0, col.y = alpha("red", 0.5))
#> 10                                                                                                                                                                                                                                                                                                                          ss_red = dropEmptyImageDimensions(ss)\northo2(ss_red)
#>                                                                                  hash
#> 1                                                                                <NA>
#> 2                                                                                <NA>
#> 3                                                                                <NA>
#> 4        reprex_reprex_cache/markdown_strict/t1_plot_1237e2a884712bffa363745c73aaa7f5
#> 5                                                                                <NA>
#> 6  reprex_reprex_cache/markdown_strict/t1_naive_plot_2bcd17d374dad557a2c9b5a588b7f9ad
#> 7                                                                                <NA>
#> 8     reprex_reprex_cache/markdown_strict/t1_ss_plot_cb7fa3ba47fae618f174f2bf40e2e415
#> 9    reprex_reprex_cache/markdown_strict/t1_ss_plot2_c6fa4be8d26d1758967f5541180b9e81
#> 10                                                                               <NA>
#>    cache                           cache.path cache.vars cache.lazy
#> 1      0 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 2      0 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 3      0 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 4      3 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 5      0 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 6      3 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 7      0 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 8      3 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 9      3 reprex_reprex_cache/markdown_strict/         NA       TRUE
#> 10     0 reprex_reprex_cache/markdown_strict/         NA       TRUE
#>                                       fig.path fig.ext fig.env error warning
#> 1  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 2  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 3  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 4  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 5  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 6  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 7  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 8  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 9  reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#> 10 reprex_reprex_files/figure-markdown_strict/      NA  figure  TRUE    TRUE
#>    message include                  params.src
#> 1     TRUE   FALSE        setup, include=FALSE
#> 2     TRUE    TRUE                eval = FALSE
#> 3     TRUE    TRUE                        data
#> 4     TRUE    TRUE       t1_plot, cache = TRUE
#> 5     TRUE    TRUE  t1_naive_ss, cache = FALSE
#> 6     TRUE    TRUE t1_naive_plot, cache = TRUE
#> 7     TRUE    TRUE        t1_ss, cache = FALSE
#> 8     TRUE    TRUE    t1_ss_plot, cache = TRUE
#> 9     TRUE    TRUE   t1_ss_plot2, cache = TRUE
#> 10    TRUE    TRUE    t1_ss_red, cache = FALSE

Created on 2020-06-28 by the reprex package (v0.3.0)

muschellij2 avatar Jun 28 '20 16:06 muschellij2

And R CMD check failed on Travis. Please also make sure the PR passes the checks (or state it if the failure is irrelevant to your PR).

yihui avatar Jul 23 '20 20:07 yihui

I think those changes were from a merge upstream, but I will delete. Best, John

On Thu, Jul 23, 2020 at 4:13 PM Yihui Xie [email protected] wrote:

And R CMD check failed on Travis. Please also make sure the PR passes the checks (or state it if the failure is irrelevant to your PR).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/yihui/knitr/pull/1870#issuecomment-663209415, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLTSIQJM5KONJBJD5CLR5CKWPANCNFSM4OJOFBCA .

muschellij2 avatar Jul 23 '20 20:07 muschellij2

I don't know what this failure really is - it seems as though it's in the examples, but I'm unsure if that's anything of my code.

muschellij2 avatar Aug 07 '20 19:08 muschellij2

Examples 087 and 112 failed: https://travis-ci.com/github/yihui/knitr/jobs/369838241#L3167-L3168

You may try reproduce locally and fix the errors: https://github.com/yihui/knitr-examples

yihui avatar Sep 21 '20 21:09 yihui

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Sep 22 '20 21:09 CLAassistant

Seems odd - why would that not be triggered on rhub: https://builder.r-hub.io/status/knitr_1.30.1.tar.gz-f7627f31e985472f80ab5dff570ac11d

muschellij2 avatar Sep 24 '20 18:09 muschellij2

I don't have time this week to review, but maybe next

muschellij2 avatar Apr 12 '21 19:04 muschellij2

That's okay. I plan to make a CRAN release in the next few days. If this PR won't catch the next release, I can place it in a future release. Thanks!

yihui avatar Apr 13 '21 04:04 yihui