Last updated: 2022-09-07

Checks: 7 0

Knit directory: muse/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20200712) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version af276fa. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    r_packages_4.1.2/
    Ignored:    r_packages_4.2.0/

Untracked files:
    Untracked:  analysis/cell_ranger.Rmd
    Untracked:  data/ncrna_NONCODE[v3.0].fasta.tar.gz
    Untracked:  data/ncrna_noncode_v3.fa

Unstaged changes:
    Modified:   script/run_rstudio.sh

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/r_backticks.Rmd) and HTML (docs/r_backticks.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd af276fa Dave Tang 2022-09-07 Backticks in R

The only times I used backticks in R was when a file was imported into R and the column names had spaces (as a side note, please don’t use spaces in your column or file names but use an underscore, i.e. _). For example, you can access col a by using backticks.

my_df <- data.frame(
  "col a" = 1:5,
  "col b" = letters[1:5],
  check.names = FALSE)

my_df$`col a`
[1] 1 2 3 4 5

However, I have seen backticks used in combination with symbols, especially with the square bracket. The following code let’s you subset (fourth item) each list item.

my_list <- list(
  l = letters[1:5],
  u = LETTERS[1:5]
)

lapply(my_list, `[`, 4)
$l
[1] "d"

$u
[1] "D"

It turns out that the use of backticks in the examples above are consistent, despite two very different applications: the backtick lets you refer to functions or variables that have otherwise reserved or illegal names. The first example with my_df shows the use of backticks to refer to an illegal column name and the second example with my_list shows the use of backticks to refer to a reserved function name. The square bracket is a function!

You can look up the help pages on the [ function, by typing ?"[" and the manual will indicate that [ is used to extract or replace parts of vectors, matrices, or arrays. In R a function is invoked using parentheses, so to use [ like a typical function call, we can perform the following to get the first five heights in the dataset:

`[`(women, 1:5, 1)
[1] 58 59 60 61 62

The code above is actually the same as how you would usually subset a data frame, and is probably the syntax that most people are familiar with.

women[1:5, 1]
[1] 58 59 60 61 62

But it turns out you do not have to use backticks and single- or double-quotes works the same. Despite this, I have seen backticks used more often, which could be due to the fact that in Bash (and Perl!) backticks are used to indicate that the text between the backticks should be executed as a command.

'['(women, 1:5, 1)
[1] 58 59 60 61 62
"["(women, 1:5, 1)
[1] 58 59 60 61 62

It also turns out that ( is a function as well, which was what I surmised in my previous post. I can run the first example of my previous post with backticks.

`(`(v_log <- c(TRUE, FALSE, FALSE, TRUE))
[1]  TRUE FALSE FALSE  TRUE

In closing, I’ll end with the following quote, which I found while researching this post, that helped me understand backticks in R.

“To understand computations in R, two slogans are helpful:

–John Chambers


sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.9     purrr_0.3.4    
 [5] readr_2.1.2     tidyr_1.2.0     tibble_3.1.8    ggplot2_3.3.6  
 [9] tidyverse_1.3.1 workflowr_1.7.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3     lubridate_1.8.0  getPass_0.2-2    ps_1.7.0        
 [5] assertthat_0.2.1 rprojroot_2.0.3  digest_0.6.29    utf8_1.2.2      
 [9] R6_2.5.1         cellranger_1.1.0 backports_1.4.1  reprex_2.0.1    
[13] evaluate_0.15    httr_1.4.3       pillar_1.8.1     rlang_1.0.4     
[17] readxl_1.4.0     rstudioapi_0.13  whisker_0.4      callr_3.7.0     
[21] jquerylib_0.1.4  rmarkdown_2.14   munsell_0.5.0    broom_0.8.0     
[25] compiler_4.2.0   httpuv_1.6.5     modelr_0.1.8     xfun_0.31       
[29] pkgconfig_2.0.3  htmltools_0.5.2  tidyselect_1.1.2 fansi_1.0.3     
[33] crayon_1.5.1     tzdb_0.3.0       dbplyr_2.1.1     withr_2.5.0     
[37] later_1.3.0      grid_4.2.0       jsonlite_1.8.0   gtable_0.3.0    
[41] lifecycle_1.0.1  DBI_1.1.2        git2r_0.30.1     magrittr_2.0.3  
[45] scales_1.2.0     cli_3.3.0        stringi_1.7.6    fs_1.5.2        
[49] promises_1.2.0.1 xml2_1.3.3       bslib_0.3.1      ellipsis_0.3.2  
[53] generics_0.1.3   vctrs_0.4.1      tools_4.2.0      glue_1.6.2      
[57] hms_1.1.2        processx_3.5.3   fastmap_1.1.0    yaml_2.3.5      
[61] colorspace_2.0-3 rvest_1.0.2      knitr_1.39       haven_2.5.0     
[65] sass_0.4.1