Question:
I have navigated to Tools>Global Options...>Code --> Edit Snippets... and can see the available snippets that I should have access to, edit the file to add more of my own. I have added a snippet
snippet tryCatch
${1:variable} <- tryCatch({
${2}
}, warning = function(w) {
message(sprintf("Warning in %s: %s", deparse(w[["call"]]), w[["message"]]))
${3}
}, error = function(e) {
message(sprintf("Error in %s: %s", deparse(e[["call"]]), e[["message"]]))
${4}
}, finally = {
${5}
})
but it fails to be recognized in my interactive()
R session. How do I add a snippet, any ideas why editing isn't working?
Background:
I have recently been introduced to R snippets
, pre-written sections of code to retrieve with a string while on the R console. I have seen multiple people on YouTube do this with no issue. I am trying to edit the file to customize my own snippets to speed the writing process up of commonly used calls to assert::assert()
and tryCatch()
. I have looked at the file location of the snippets to see the content in ~\AppData\Roaming\RStudio\snippets\r.snippets
. I have edited the file in both places and resarted the IDE and my R session to no avail and the only snippets I can access are the one preloaded from shinysnippets
.
r.snippets
file;
snippet module
${1:name}ui <- function(id){
ns <- NS(id)
tagList(
${0}
)
}
${1:name} <- function(input, output, session){
ns <- session\$ns
}
# Copy in UI
#${1:name}ui("${1:name}ui")
# Copy in server
#callModule(${1:name}, "${1:name}ui")
snippet observe_event
observeEvent( ${1:event} , {
${0}
})
snippet with_progress
withProgress( message = "${1:message}" , {
${0}
})
snippet aa
${1:dataset} <- ${1:dataset} %>% ${0}
snippet sidebar_page
library(shiny)
ui <- fluidPage(
titlePanel( "${1:title}" ),
sidebarLayout(
sidebarPanel(
),
mainPanel(
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
snippet tryCatch
${1:variable} <- tryCatch({
${2}
}, warning = function(w) {
message(sprintf("Warning in %s: %s", deparse(w[["call"]]), w[["message"]]))
${3}
}, error = function(e) {
message(sprintf("Error in %s: %s", deparse(e[["call"]]), e[["message"]]))
${4}
}, finally = {
${5}
})
snippet checkData
checkmate::checkDate(
from,
upper = today(),
len = 1
)
snippet assert
assert::assert(
${1},
msg = "\n\033[31mx\033[39m Error:\n ${2}"
)
When typing observe_event
into the cmd-line, you are greeted with a choice to input the above chunk of code, with the cursor automatically switching between the $
anchors.
Session Info
devtools::sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] assert_1.0.1 rameritrade_0.1.5 httr_1.4.2
[4] quantmod_0.4.18 RVerbalExpressions_0.1.0 PerformanceAnalytics_2.0.4
[7] tbl2xts_1.0.4 bizdays_1.0.9 tictoc_1.0.1
[10] glue_1.6.1 here_1.0.1 polite_0.1.1
[13] rvest_1.0.2 Riex_1.0.2 TTR_0.24.3
[16] xts_0.12.1 zoo_1.8-9 lubridate_1.8.0
[19] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.8
[22] purrr_0.3.4 readr_2.1.2 tidyr_1.2.0
[25] ggplot2_3.3.5 tidyverse_1.3.1 plyr_1.8.6
[28] furrr_0.2.3 future_1.24.0 tidyjson_0.3.1
[31] reshape2_1.4.4 ProjectTemplate_0.10.2 tibble_3.1.6
[34] digest_0.6.29
loaded via a namespace (and not attached):
[1] utf8_1.2.2 tidyselect_1.1.2 lme4_1.1-28
[4] grid_4.1.0 robotstxt_0.7.13 munsell_0.5.0
[7] codetools_0.2-18 effectsize_0.6.0.1 miniUI_0.1.1.1
[10] withr_2.5.0 colorspace_2.0-3 config_0.3.1
[13] knitr_1.37 rstudioapi_0.13 listenv_0.8.0
[16] emmeans_1.7.2 httr2_0.1.1 urltools_1.7.3
[19] roll_1.1.6 polyclip_1.10-0 farver_2.1.0
[22] datawizard_0.3.0 rprojroot_2.0.2 parallelly_1.30.0
[25] vctrs_0.3.8 generics_0.1.2 xfun_0.30
[28] ipred_0.9-12 R6_2.5.1 shinysnippets_0.0.0.9000
[31] cachem_1.0.6 assertthat_0.2.1 promises_1.2.0.1
[34] scales_1.1.1 nnet_7.3-16 ggExtra_0.9
[37] gtable_0.3.0 miceadds_3.11-6 globals_0.14.0
[40] timeDate_3043.102 rlang_1.0.1 berryFunctions_1.21.0
[43] splines_4.1.0 lazyeval_0.2.2 keyring_1.3.0
[46] broom_0.7.12 checkmate_2.0.0 yaml_2.2.1
[49] abind_1.4-5 modelr_0.1.8 backports_1.4.1
[52] httpuv_1.6.5 tools_4.1.0 lava_1.6.10
[55] usethis_2.1.5 ellipsis_0.3.2 tsibble_1.1.1
[58] Rcpp_1.0.8 timetk_2.7.0 rpart_4.1-15
[61] slider_0.2.2 haven_2.4.3 fs_1.5.2
[64] magrittr_2.0.2 data.table_1.14.2 brochure_0.0.0.9023
[67] warp_0.2.0 triebeard_0.3.0 reprex_2.0.1
[70] mvtnorm_1.1-3 sjmisc_2.8.9 pkgload_1.2.4
[73] hms_1.1.1 mime_0.12 xtable_1.8-4
[76] XML_3.99-0.9 sjstats_0.18.1 readxl_1.3.1
[79] testthat_3.0.4 compiler_4.1.0 mice_3.14.0
[82] crayon_1.5.0 minqa_1.2.4 htmltools_0.5.2
[85] later_1.3.0 tzdb_0.2.0 RcppParallel_5.1.5
[88] DBI_1.1.2 tweenr_1.0.2 sjlabelled_1.1.8
[91] dbplyr_2.1.1 MASS_7.3-54 rappdirs_0.3.3
[94] boot_1.3-28 Matrix_1.3-3 cli_3.2.0
[97] mitools_2.4 runner_0.4.1 quadprog_1.5-8
[100] parallel_4.1.0 insight_0.16.0 gower_1.0.0
[103] golem_0.3.2 pkgconfig_2.0.3 microbenchmark_1.4.9
[106] recipes_0.2.0 roxygen2_7.1.2 xml2_1.3.3
[109] hardhat_0.2.0 estimability_1.3 prodlim_2019.11.13
[112] anytime_0.3.9 attempt_0.3.1 parameters_0.16.0
[115] tidyquant_1.0.3 RQuantLib_0.4.15 cellranger_1.1.0
[118] curl_4.3.1 shiny_1.7.1 gtools_3.9.2
[121] rjson_0.2.21 nloptr_2.0.0 bench_1.1.2
[124] lifecycle_1.0.1 nlme_3.1-152 jsonlite_1.7.3
[127] desc_1.4.1 fansi_1.0.2 pillar_1.7.0
[130] lattice_0.20-44 fastmap_1.1.0 survival_3.2-11
[133] ratelimitr_0.4.1 bayestestR_0.11.5 ggforce_0.3.3
[136] class_7.3-19 stringi_1.6.2 performance_0.8.0
[139] Quandl_2.11.0 rsample_0.1.1 memoise_2.0.1
[142] future.apply_1.8.1
CodePudding user response:
The snippet file is sensitive to spaces, use tabs instead.
The first snippet should work correctly. The second one will show up in autocompletion but won't do anything.
New snippets are recognized by RStudio on saving the snippets file, restarting RStudio is not needed. This can also be done programmatically if you want the tryCatch
snippet to be populated with specific content for different use cases.
CodePudding user response:
R Snippets are notorious for being sensitive to spaces vs tabs as @DonaldSeinen has pointed out. You need to ensure that there are no red spaces in the text fields when editing your R snippet, this will render the snippet useless.
TIP
when writing snippet code, please use tab
in order to remedy this problem.