Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split call_block into a function block_params #1870

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

muschellij2
Copy link
Contributor

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
Copy link
Contributor Author

# 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)

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but please discard the changes in inst/misc/tweak_bib.csv (and avoid committing irrelevant changes in the future; you may want to use a git gui and make sure you select the changes that you actually want to make to commit). Thank you!

@yihui
Copy link
Owner

yihui commented Jul 23, 2020

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).

@muschellij2
Copy link
Contributor Author

muschellij2 commented Jul 23, 2020 via email

@muschellij2
Copy link
Contributor Author

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.

@yihui
Copy link
Owner

yihui commented Sep 21, 2020

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

@CLAassistant
Copy link

CLAassistant commented Sep 22, 2020

CLA assistant check
All committers have signed the CLA.

@muschellij2
Copy link
Contributor Author

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

@cderv cderv mentioned this pull request Mar 24, 2021
Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muschellij2
Copy link
Contributor Author

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

@yihui
Copy link
Owner

yihui commented Apr 13, 2021

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants