Skip to content

Instantly share code, notes, and snippets.

@rmflight
Last active July 24, 2020 00:26

Revisions

  1. rmflight revised this gist Mar 26, 2020. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,21 @@
    # ComplexHeatmap

    ## Modify the font sizes in the heatmap

    If you want to get the font sizes right for a plot, you will need to play with these font sizes

    ```r
    # add the following, add col* if you want to modify both
    row_names_gp = gpar(fontsize = 24, fontfamily = "FreeMono")
    ```

    ## Saving a PNG

    ```r
    Cairo("filename", width = 2600, height = 6210, dpi = 300, bg = "#FFFFFF", fontfamily = "FreeMono")
    Heatmap(heatmap, colormap, "Expression", cluster_rows = FALSE, cluster_columns = FALSE, row_names_gp = gpar(fontsize = 24, fontfamily = "FreeMono"), column_names_gp = gpar(fontsize = 30))
    dev.off()
    ```
    # ggplot2

    ## Rotate axis tick labels 90 degrees
  2. rmflight revised this gist Oct 1, 2019. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion code_counting.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,13 @@
    If you want to know how many lines of code are in
    If you want to know how many lines of code are in an Rmd file:

    ```
    rmarkdown::render("file.Rmd", output_format = "md_document")
    cat file.md | grep -c '^ \{4\}'
    ```

    And then how many lines of code in .R files:

    ```
    wc --lines file.R
    ```
  3. rmflight revised this gist Oct 1, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions code_counting.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    If you want to know how many lines of code are in
  4. rmflight revised this gist Oct 31, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -104,6 +104,8 @@ correctly_round_numbers <- function(number_of_things, fraction){

    ## Anonymize Things While Retaining Order

    Courtesy of [David Robinson](https://twitter.com/drob/status/1057681171105173504)

    ```
    as.integer(fct_inorder(x))
    ```
  5. rmflight revised this gist Oct 31, 2018. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -101,3 +101,9 @@ correctly_round_numbers <- function(number_of_things, fraction){
    floor_value
    }
    ```

    ## Anonymize Things While Retaining Order

    ```
    as.integer(fct_inorder(x))
    ```
  6. rmflight revised this gist Sep 19, 2017. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -87,3 +87,17 @@ ggplot(rf_sep, aes(x = dim1, y = dim2, color = class)) + geom_point()
    * BioCann: https://bioconductor.org/packages/3.4/data/annotation
    * BioCexp: https://bioconductor.org/packages/3.4/data/experiment
    * BioCextra: https://bioconductor.org/packages/3.4/extra

    ## Easily switch between fraction and number of things

    ```
    correctly_round_numbers <- function(number_of_things, fraction){
    assertthat::assert_that(number_of_things > 0)
    if (fraction < 1) {
    floor_value <- floor(number_of_things * fraction)
    } else {
    floor_value <- number_of_things
    }
    floor_value
    }
    ```
  7. rmflight revised this gist Aug 7, 2017. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,22 @@ ggsave("filename.svg", plot_to_save)
    system("inkscape -z -d 300 -e outfile.png infile.svg")
    ```

    ## Include cowplot in package deps

    If we want to use the cowplot theme, but keep from generating warnings, etc in R CMD check of when `loading` the package, then we should structure things like so:

    ```r
    #' @import ggplot2

    theme_set(cowplot::theme_cowplot())
    ```

    And in DESCRIPTION:

    ```
    imports: ggplot2, cowplot
    ```

    # Inkscape

    ## Add a bullet
  8. rmflight revised this gist Jul 28, 2017. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,21 @@ ggsave("filename.svg", plot_to_save)
    system("inkscape -z -d 300 -e outfile.png infile.svg")
    ```

    # Inkscape

    ## Add a bullet

    ```
    ctrl+u, 2022, enter
    ```

    ## Flow Text Around an Object

    * Draw a path around the object using whatever tool you want
    * Select the previous written text's box
    * Select the path object
    * Text -> Flow into Frame

    # Random Forest

    ## Separability plot
  9. rmflight revised this gist Jul 28, 2017. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,22 @@
    + theme(legend.position="none")
    ```

    ## Make COWPLOT fontsize a bit bigger

    ```r
    mod_theme <- theme_cowplot(font_size = 16)
    theme_set(mod_theme)
    ```

    ## Make PNG of high-density SVGs

    SVG is great for posters, can resize at will. But if there are a lot of points, then we really want PNG. This workflow we export the point figure to SVG at the set size, and then convert to PNG using InkScape at the command line.

    ```r
    ggsave("filename.svg", plot_to_save)
    system("inkscape -z -d 300 -e outfile.png infile.svg")
    ```

    # Random Forest

    ## Separability plot
  10. rmflight revised this gist Jun 27, 2017. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions testing_stuff.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    ## working with temp temp directories

    ```r
    create_in_temp <- function(dir_loc, create_it = TRUE) {
    temp_path <- tempfile(pattern = paste0("copyfiles-test-", dir_loc))
    if (create_it) {
    dir.create(temp_path)
    }
    temp_path
    }
    erase <- function(path) unlink(path, recursive = TRUE)
    ```

    ## skipping tests conditionally

    - in ci have `export skip_variable=TRUE`

    ```r
    skip_variable <- as.logical(Sys.getenv("skip_variable"))
    if (is.na(skip_variable)) {
    skip_variable <- FALSE
    }

    test_that("test we want to skip conditionally", {
    skip_if_not(skip_variable)
    expect_true(test condition)
    })

    test_that("other tests", {

    })
  11. rmflight revised this gist Mar 24, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,12 @@
    + theme(axis.text.x = element_text(angle = 90))
    ```

    ## Remove the legend

    ```r
    + theme(legend.position="none")
    ```

    # Random Forest

    ## Separability plot
  12. rmflight revised this gist Mar 7, 2017. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,12 @@ names(rf_sep) <- c("dim1", "dim2")
    rf_sep$class <- factor_of_classes

    ggplot(rf_sep, aes(x = dim1, y = dim2, color = class)) + geom_point()
    ```
    ```

    ## Install Specific Bioconductor Package Versions

    * modify the `repos` argument based on `BiocInstaller::biocinstallRepos`:
    * BioCsoft: https://bioconductor.org/packages/3.4/bioc
    * BioCann: https://bioconductor.org/packages/3.4/data/annotation
    * BioCexp: https://bioconductor.org/packages/3.4/data/experiment
    * BioCextra: https://bioconductor.org/packages/3.4/extra
  13. rmflight revised this gist Feb 17, 2017. 2 changed files with 21 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions graphing_errors.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +0,0 @@
    ## Get an error about X11 Missing font

    `X11 font -adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*, face 1 at size 12 could not be loaded`

    The solution is:

    `X11.options(type = "cairo")`
    21 changes: 21 additions & 0 deletions graphing_issues.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    ## Get an error about X11 Missing font

    `X11 font -adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*, face 1 at size 12 could not be loaded`

    The solution is:

    `X11.options(type = "cairo")`

    ## Want to use a particular font in a graphic

    For example, to add a Monospaced font to a graphic instead of a proportional font:

    ```
    library(showtext)
    font.add("FreeMono", "FreeMono.ttf")
    showtext.auto()
    Cairo("graphic.png", width = 2600, height = 3600, dpi = 300, bg = "#FFFFFF", fontfamily = "FreeMono")
    plot(..., gpar(fontsize = 24, fontfamily = "FreeMono"))
    dev.off()
    ```
  14. rmflight revised this gist Jan 31, 2017. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion install_problems.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,14 @@
    R CMD javareconf -e
    ```

    Restart R / RStudio (i.e. complete shutdown and restart)
    Restart R / RStudio (i.e. complete shutdown and restart)

    # rJava, no jni.h

    If you've confirmed that jni.h can be found, and that

    `R CMD javareconf -n`

    still shows an error, and it seems to be mainly due to using the incorrect directory for JAVA, one solution may be to make a link from the directory `R` wants to use, to the actual one.

    `sudo ln /usr/lib/jvm/right_java_dir /usr/lib/jvm/wrong_java_dir`
  15. rmflight revised this gist Jan 30, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions graphing_errors.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    ## Get an error about X11 Missing font

    `X11 font -adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*, face 1 at size 12 could not be loaded`

    The solution is:

    `X11.options(type = "cairo")`
  16. rmflight revised this gist Nov 1, 2016. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -52,4 +52,22 @@ xtable::caption(tmp_table) <- "caption text"
    xtable::align(out_table) <- "llp{2in}ll" # assuming 4 column table, second column would only take up 2 inches of space
    print(out_table, comment = FALSE, floating = FALSE, tabular.environment = "longtable") # important at end, to make sure that the environment is correct, because otherwise the tables won't display right, believe me
    ```

    ## Setting Parameters

    ```r
    parameters = read_delim(

    'Parameter | Value | Units
    #--------------|-------|-------
    herd_size | 20.00 | camels
    max_age | 25.00 | years
    fertile_age | 5.00 | years ',

    delim = '|', trim_ws = TRUE, comment = "#')
    input = structure(as.list(parameters$Value),
    names = parameters$Parameter)
    kable(parameters, align = c('l', 'r', 'l'), caption = 'Model Parameters')
    ```
  17. rmflight revised this gist Oct 11, 2016. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -13,10 +13,9 @@ output:
    ## Knitr chunk setup

    ```
    ```{r setup, echo=FALSE}
    {r setup, echo=FALSE}
    knitr::opts_chunk$set(cache = TRUE, warning = FALSE, message = FALSE)
    ```
    ```

    ## Data Checking

  18. rmflight revised this gist Oct 11, 2016. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,14 @@ output:
    ---
    ```

    ## Knitr chunk setup

    ```
    ```{r setup, echo=FALSE}
    knitr::opts_chunk$set(cache = TRUE, warning = FALSE, message = FALSE)
    ```
    ```
    ## Data Checking
    ```{r data_checking}
  19. rmflight revised this gist Oct 10, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions using_reporters.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    ```r
    library(ReporteRs)
    doc <- pptx()

    p <- ggplot() ....

    doc <- addSlide(doc, slide.layout = "Content with Caption")
    doc <- addPlot(doc = doc, fun = print, x = p, vector.graphic = TRUE)

    writeDoc(doc = doc, file = "file.pptx")
    ```
  20. rmflight revised this gist Oct 5, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -32,13 +32,17 @@ knitr::kable(pkg_info$packages)
    ---
    header-includes:
    - \usepackage{longtable} # allows use of tables spanning pages
    - \usepackage{caption} # used if you need to define your own captions
    - \captionsetup{labelformat = empty} # for empty captions
    output: pdf_document
    classoption: landscape # turns output to landscape
    ---
    ```

    ```{r table_output}
    out_table <- xtable::xtable(df, type = "latex")
    xtable::caption(tmp_table) <- "caption text"
    xtable::align(out_table) <- "llp{2in}ll" # assuming 4 column table, second column would only take up 2 inches of space
    print(out_table, comment = FALSE, floating = FALSE, tabular.environment = "longtable") # important at end, to make sure that the environment is correct, because otherwise the tables won't display right, believe me
    ```
  21. rmflight revised this gist Oct 5, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -28,12 +28,14 @@ knitr::kable(pkg_info$packages)

    ## Wide Format PDF Tables in RMarkdown

    ```
    ---
    header-includes:
    - \usepackage{longtable} # allows use of tables spanning pages
    output: pdf_document
    classoption: landscape # turns output to landscape
    ---
    ```

    ```{r table_output}
    out_table <- xtable::xtable(df, type = "latex")
  22. rmflight revised this gist Oct 5, 2016. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -25,3 +25,18 @@ pkg_info <- devtools::session_info()
    pkg_info$platform
    knitr::kable(pkg_info$packages)
    ```

    ## Wide Format PDF Tables in RMarkdown

    ---
    header-includes:
    - \usepackage{longtable} # allows use of tables spanning pages
    output: pdf_document
    classoption: landscape # turns output to landscape
    ---

    ```{r table_output}
    out_table <- xtable::xtable(df, type = "latex")
    xtable::align(out_table) <- "llp{2in}ll" # assuming 4 column table, second column would only take up 2 inches of space
    print(out_table, comment = FALSE, floating = FALSE, tabular.environment = "longtable") # important at end, to make sure that the environment is correct, because otherwise the tables won't display right, believe me
    ```
  23. rmflight revised this gist Jul 26, 2016. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -10,13 +10,18 @@ output:
    ---
    ```

    ## Data Checking

    ```{r data_checking}
    library(assertthat)
    library(assertr)
    ```


    ## Software Used
    ## Packages Used

    ```{r software_used}
    dev_info <- devtools::session_info()
    dev_info$platform
    knitr::kable(dev_info$packages)
    ```{r packages_used, echo = FALSE}
    pkg_info <- devtools::session_info()
    pkg_info$platform
    knitr::kable(pkg_info$packages)
    ```
  24. rmflight revised this gist Jul 26, 2016. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    # Vignette title

    ```
    ---
    title: "a good title"
    @@ -10,4 +8,15 @@ output:
    pdf_document:
    toc: yes
    ---
    ```
    ```




    ## Software Used

    ```{r software_used}
    dev_info <- devtools::session_info()
    dev_info$platform
    knitr::kable(dev_info$packages)
    ```
  25. rmflight revised this gist May 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -24,5 +24,5 @@ names(rf_sep) <- c("dim1", "dim2")
    # add your class information so can see how classes behave relative to one another using coloring (or shape, etc)
    rf_sep$class <- factor_of_classes

    ggplot(rf_sep, aes(x = dim1, y = dim2, color = class) + geom_point()
    ggplot(rf_sep, aes(x = dim1, y = dim2, color = class)) + geom_point()
    ```
  26. rmflight revised this gist Apr 7, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions install_problems.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # rJava: can't load .so object

    ```
    R CMD javareconf -e
    ```

    Restart R / RStudio (i.e. complete shutdown and restart)
  27. rmflight revised this gist Apr 7, 2016. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions vignette_snippets.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # Vignette title

    ```
    ---
    title: "a good title"
    author: "Robert M Flight"
    date: "`r Sys.time()`"
    commit: "`r substr(git2r::branch_target(git2r::head(git2r::repository())), 1, 8)`"
    output:
    pdf_document:
    toc: yes
    ---
    ```
  28. rmflight revised this gist Apr 7, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,8 @@ Use `cmdscale` on the proximities to look at how the samples behave in `RF`.
    rf_model <- randomForest(x ...., proximity = TRUE)
    # generate MDS scaling of proximities
    rf_sep <- stats::cmdscale(1 - rf_model$proximity, k = 2, eig = TRUE) # two components, using eigenvalue based
    # data.frames are nicer to work with
    rf_sep <- as.data.frame(rf_sep)
    # data.frames are nicer to work with, but just need the points from cmdscale
    rf_sep <- as.data.frame(rf_sep$points)
    names(rf_sep) <- c("dim1", "dim2")

    # add your class information so can see how classes behave relative to one another using coloring (or shape, etc)
  29. rmflight revised this gist Apr 7, 2016. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,25 @@

    ```r
    + theme(axis.text.x = element_text(angle = 90))
    ```

    # Random Forest

    ## Separability plot

    Use `cmdscale` on the proximities to look at how the samples behave in `RF`.

    ```r
    # generate model
    rf_model <- randomForest(x ...., proximity = TRUE)
    # generate MDS scaling of proximities
    rf_sep <- stats::cmdscale(1 - rf_model$proximity, k = 2, eig = TRUE) # two components, using eigenvalue based
    # data.frames are nicer to work with
    rf_sep <- as.data.frame(rf_sep)
    names(rf_sep) <- c("dim1", "dim2")

    # add your class information so can see how classes behave relative to one another using coloring (or shape, etc)
    rf_sep$class <- factor_of_classes

    ggplot(rf_sep, aes(x = dim1, y = dim2, color = class) + geom_point()
    ```
  30. rmflight revised this gist Apr 7, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions tips_and_tricks.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # ggplot2

    ## Rotate axis tick labels 90 degrees

    ```r
    + theme(axis.text.x = element_text(angle = 90))
    ```