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

Issue 10 snippet helper #28

Merged
merged 10 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Imports:
digest,
R6,
visNetwork,
methods
methods,
snippr
m-l-1 marked this conversation as resolved.
Show resolved Hide resolved
RoxygenNote: 7.0.2
Roxygen: list(markdown = TRUE)
URL: https://github.com/Novartis/tidymodules
Expand All @@ -34,4 +35,6 @@ Suggests:
RColorBrewer,
shinyWidgets,
plotly
Remotes:
dgrtwo/snippr@29c1813
VignetteBuilder: knitr
10 changes: 10 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,11 @@ export("%>>10%")
export(ModStore)
export(Store)
export(TidyModule)
export(add_tm_snippets)
export(callModules)
export(check_and_load)
export(combine_ports)
export(defineEdges)
export(getCacheOption)
export(getMod)
export(getSessionId)
Expand All @@ -442,5 +444,13 @@ export(race_ports)
export(session_type)
export(showExamples)
import(R6)
import(dplyr)
import(shiny)
import(snippr)
importFrom(cli,cat_bullet)
importFrom(fs,path_ext_set)
importFrom(fs,path_home_r)
importFrom(methods,is)
importFrom(purrr,discard)
importFrom(purrr,keep)
importFrom(purrr,map)
2 changes: 1 addition & 1 deletion R/TidyModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ TidyModule <- R6::R6Class(
isolate(x)
},
#' @description
#' Function wrapper for port assignement expression.
#' Function wrapper for port assignment expression.
#' @param x expression
assignPort = function(x){
observe({
Expand Down
62 changes: 62 additions & 0 deletions R/snippets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#'
#' @title Add `{tm}` snippets to RStudio
#'
#' @description This function adds useful `{tm}` code snippets to RStudio.
#'
#' @param force Force the re-installation when the snippets are already installed.
#'
#' @import snippr
#' @import dplyr
#' @importFrom fs path_home_r path_ext_set
#' @importFrom cli cat_bullet
#' @importFrom purrr keep discard map
#' @export
add_tm_snippets <- function(force = FALSE){
m-l-1 marked this conversation as resolved.
Show resolved Hide resolved
# R snippets file
path <- path_home_r(".R", "snippets", path_ext_set("r","snippets"))
# retrieve current and new snippets
current_all_snippets <- snippets_get(path = path)
current_non_tm_snippets <- current_all_snippets %>% discard(grepl("^tm\\.",names(.),perl = TRUE))
current_tm_snippets <- current_all_snippets %>% keep(grepl("^tm\\.",names(.),perl = TRUE))
new_tm_snippets <- snippets_read(path = system.file("rstudio/r.snippets",package = "tidymodules"))
# calculate differences
del_snippets <- setdiff(names(current_tm_snippets),names(new_tm_snippets))
keep_snippets <- intersect(names(current_tm_snippets),names(new_tm_snippets))
add_snippets <- setdiff(names(new_tm_snippets),names(current_tm_snippets))
# print some informations
if(length(del_snippets)>0){
cat_bullet(paste0("Deleting ",length(del_snippets)," snippet(s):"),
bullet_col = "orange",
bullet = "bullet")
invisible(map(del_snippets,cat_bullet,bullet = "dot"))
}
existing_snippets <- current_non_tm_snippets
save_snippets <- NULL
if(length(keep_snippets) > 0)
if(force){
cat_bullet(paste0("Re-installing ",length(keep_snippets)," existing snippet(s):"),
bullet_col = "green",
bullet = "tick")
invisible(map(keep_snippets,cat_bullet,bullet = "dot"))
save_snippets <- new_tm_snippets[keep_snippets]
}else{
cat_bullet(paste0("Skip installation of ",length(keep_snippets)," existing snippet(s):"),
bullet_col = "red",
bullet = "bullet")
invisible(map(keep_snippets,cat_bullet,bullet = "dot"))
existing_snippets <- c(existing_snippets,current_tm_snippets[keep_snippets])
}
if(length(add_snippets)>0){
cat_bullet(paste0("Installing ",length(add_snippets)," new snippets:"),
bullet_col = "green",
bullet = "tick")
invisible(map(add_snippets,cat_bullet,bullet = "dot"))
save_snippets <- c(save_snippets,new_tm_snippets[add_snippets])
}

final_snippets <- existing_snippets
if(!is.null(save_snippets))
final_snippets <- c(final_snippets,save_snippets)

snippets_write(final_snippets,path = path)
}
15 changes: 15 additions & 0 deletions R/utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ callModules <- function(){
})
lapply(calls,function(m) m$callModule())
}
#'
#' @title Function wrapper for ports connection expression.
#'
#' @description Used in server functions to define how modules are connected to each other.
#'
#' @param x expression
#'
#' @export
defineEdges <- function(x){
observe({
isolate(x)
})
}


#'
#' @title Retrieve cache option from the environment
#'
Expand Down
70 changes: 70 additions & 0 deletions inst/rstudio/r.snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
snippet tm.mod.new
m-l-1 marked this conversation as resolved.
Show resolved Hide resolved
#'
#' ${1:MyModule} title here.
#'
#' @description
#' ${1:MyModule} long description here.
#'
#' @details
#' More details here.
#'
#' @export
${1:MyModule} <- R6::R6Class(
classname = "${1:MyModule}",
inherit = ${2:TidyModule},
public = list(
#' @description
#' Module's initialization function.
#' @return An instance of ${1:MyModule}
initialize = function(...){
# DON'T REMOVE the line below, it's mandatory!
super\$initialize(...)
# Ports definition starts here...
${0}
},
#' @description
#' Module's ui function.
#' @return HTML tags list.
ui = function(){
# Module's representation starts here ...
tagList()
},
#' @description
#' Module's server function.
#' @param input Shiny input.
#' @param output Shiny output
#' @param session Shiny session
server = function(input, output, session){
# DON'T REMOVE the line below, it's mandatory!
super\$server(input,output,session)
# Module server logic starts here ...


}
)
)
snippet tm.port.define
self\$definePort({
${0}
})
snippet tm.port.in
self\$addInputPort(
name = "${1:port_name}",
description = "A clear description for this input port${0}",
sample = data.frame(x = rnorm(10), y = rnorm(10))
)
snippet tm.port.out
self\$addOutputPort(
name = "${1:port_name}",
description = "A clear description for this output port${0}",
sample = data.frame(x = rnorm(10), y = rnorm(10))
)
snippet tm.port.assign
self\$assignPort({
${0}
})
snippet tm.port.edges
defineEdges({
${0}
})

2 changes: 1 addition & 1 deletion man/TidyModule.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/add_tm_snippets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/defineEdges.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.