Title: | Make Methods for R6 Classes |
---|---|
Description: | Generate boilerplate code for R6 classes. Given R6 class create getters and/or setters for selected class fields or use RStudio addins to insert methods straight into class definition. |
Authors: | Jakub Sobolewski [aut, cre] |
Maintainer: | Jakub Sobolewski <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2025-01-23 02:53:37 UTC |
Source: | https://github.com/jakubsob/r6methods |
Extract R6 Class
extract_class(content, start_pos = 1)
extract_class(content, start_pos = 1)
content |
Character, content of a file or a string |
start_pos |
Integer, row position of cursor. Serves as starting point to search for class definition |
A list with fields
class_contentCharacter, extracted class definition
startInteger, start position of class definition within 'content'
endInteger, end position of class definition within 'content'
Find position of closing character to first encountered opening character
find_closing(text, opening = "\\(", closing = "\\)")
find_closing(text, opening = "\\(", closing = "\\)")
text |
Character, text to search |
opening |
Opening character |
closing |
Closing character |
Integer, position of closing character
Get Cursor Position From Active Document
get_cursor_pos(context)
get_cursor_pos(context)
context |
Active document context |
Integer, position of cursor in text
Insert Methods
insert_methods( content, start_pos = 1, field = c("all", "public", "private"), method = c("both", "get", "set"), add_roxygen = TRUE )
insert_methods( content, start_pos = 1, field = c("all", "public", "private"), method = c("both", "get", "set"), add_roxygen = TRUE )
content |
Character, content of the file or a string |
start_pos |
Integer, position of cursor within 'content'. Number of characters before the cursor. |
field |
Character, fields for which to create method. May be "all", "public", "private" or name of class field. Multiple values allowed. |
method |
Character, methods to create. One of "both", "get", "set" |
add_roxygen |
Logical, whether to add roxygen description of method |
Character, modified content
with injected methods
An addin for inserting methods straigth into the source file
insert_methods_addin()
insert_methods_addin()
No return value, called for side effects
Insert methods addin gadget
insert_methods_addin_gadget()
insert_methods_addin_gadget()
No return value, called for side effects
Create gadget for generating R6 methods. Action after clicking 'Done' button is defined by 'done_fun'.
make_gadget(title, title_bar, done_fun)
make_gadget(title, title_bar, done_fun)
title |
Character, title of gadget window |
title_bar |
Character, gadget title bar |
done_fun |
Function to be used after clicking 'Done' button |
Function creating and running a Shiny gadget
Make method string
make_getter_method_str(field, is_public = TRUE, add_roxygen = TRUE) make_setter_method_str(field, is_public = TRUE, add_roxygen = TRUE)
make_getter_method_str(field, is_public = TRUE, add_roxygen = TRUE) make_setter_method_str(field, is_public = TRUE, add_roxygen = TRUE)
field |
Character name of class field |
is_public |
Logical, whether the field is in public list |
add_roxygen |
Logical, whether to add roxygen description of method |
Character containing method definition
Make methods
make_methods( r6, field = c("all", "public", "private", names(r6$public_fields), names(r6$private_fields)), method = c("both", "get", "set"), add_roxygen = TRUE )
make_methods( r6, field = c("all", "public", "private", names(r6$public_fields), names(r6$private_fields)), method = c("both", "get", "set"), add_roxygen = TRUE )
r6 |
R6 class for which to create methods |
field |
Character, fields for which to create method. May be "all", "public", "private" or name of class field. Multiple values allowed. |
method |
Character, methods to create. One of "both", "get", "set" |
add_roxygen |
Logical, whether to add roxygen description of method |
Character containing generated methods to put into class definition
Example <- R6::R6Class("Example", list(public_field = NULL), list(private_field = NULL)) make_methods(Example) make_methods(Example, "private", "get") make_methods(Example, "private_field", c("get", "set")) make_methods(Example, "public_field", c("both"))
Example <- R6::R6Class("Example", list(public_field = NULL), list(private_field = NULL)) make_methods(Example) make_methods(Example, "private", "get") make_methods(Example, "private_field", c("get", "set")) make_methods(Example, "public_field", c("both"))
Make Methods Addin
make_methods_addin()
make_methods_addin()
No return value, called for side effects
Make methods addin gadget
make_methods_addin_gadget()
make_methods_addin_gadget()
No return value, called for side effects
This class allows you to make your R6 class reactive by inheriting from ReactiveR6
By calling private$invalidate()
in a method you can invalidate the class in a controlled way, i.e.
only when specific methods are called. See example how to use it.
Inspired by https://community.rstudio.com/t/good-way-to-create-a-reactive-aware-r6-class/84890
reactive()
Call this method to make object instance reactive
ReactiveR6$reactive()
clone()
The objects of this class are cloneable with this method.
ReactiveR6$clone(deep = FALSE)
deep
Whether to make a deep clone.
## Not run: if (interactive()) { library(shiny) library(r6methods) Counter <- R6::R6Class( "Counter", inherit = ReactiveR6, public = list( increment = function() { private$counter <- private$counter + 1 private$invalidate() }, decrement = function() { private$counter <- private$counter - 1 private$invalidate() }, silent_increment = function() { private$counter <- private$counter + 1 }, get_counter = function() { private$counter } ), private = list( counter = 0 ) ) counter <- Counter$new()$reactive() shinyApp( fluidPage( actionButton("increment", "Increment"), actionButton("decrement", "Decrement"), actionButton("silent_increment", "Silent increment"), textOutput("value") ), function(input, output, session) { observeEvent(input$increment, { counter()$increment() }) observeEvent(input$decrement, { counter()$decrement() }) observeEvent(input$silent_increment, { counter()$silent_increment() }) output$value <- renderText({ counter()$get_counter() }) } ) } ## End(Not run)
## Not run: if (interactive()) { library(shiny) library(r6methods) Counter <- R6::R6Class( "Counter", inherit = ReactiveR6, public = list( increment = function() { private$counter <- private$counter + 1 private$invalidate() }, decrement = function() { private$counter <- private$counter - 1 private$invalidate() }, silent_increment = function() { private$counter <- private$counter + 1 }, get_counter = function() { private$counter } ), private = list( counter = 0 ) ) counter <- Counter$new()$reactive() shinyApp( fluidPage( actionButton("increment", "Increment"), actionButton("decrement", "Decrement"), actionButton("silent_increment", "Silent increment"), textOutput("value") ), function(input, output, session) { observeEvent(input$increment, { counter()$increment() }) observeEvent(input$decrement, { counter()$decrement() }) observeEvent(input$silent_increment, { counter()$silent_increment() }) output$value <- renderText({ counter()$get_counter() }) } ) } ## End(Not run)
Sources R6 class from text, prepends namespace to 'R6Class' in order to not require 'R6' to be loaded.
source_class(txt)
source_class(txt)
txt |
Character, text containing class definition |
R6 class