2017-08-01 36 views
9

ř包內的方法我正在與一個R6類及其方法的文檔掙扎。我的目標是在RStudio中爲這些方法獲得自動完成。目前,我只知道方法的名稱,但沒有我通常使用的幫助信息roxygen2記錄具有參數等功能。歸檔R6類和RStudio

任何人都可以幫助我嗎?

目前,這是我的課:

#' @importFrom R6 R6Class 
MQParameters <- R6::R6Class(
    'MQParameters', 
    public=list(
    initialize=function(file_path=NA) { 
     private$location <- file_path 
     mq_parameters <- read.delim(file_path, stringsAsFactors=FALSE) 
     mq_parameters <- 
     setNames(mq_parameters$Value, mq_parameters$Parameter) 
     private$mq_version <- unname(mq_parameters['Version']) 
     private$fasta_file <- 
     gsub('\\\\', '/', strsplit(mq_parameters['Fasta file'], ';')[[1]]) 
    }, 
    # this method returns the version 
    getVersion=function() { 
     private$mq_version 
    }, 
    # this methods returns the fastafile. 
    # @param new_param it is possible to rewrite the basedir. 
    getFastaFile=function(new_basedir=NA) { 
     if(is.na(new_basedir)) { 
     private$fasta_file 
     } else { 
     file.path(new_basedir, basename(private$fasta_file)) 
     } 
    } 
), 
    private=list(
    location=NULL, 
    mq_version=NULL, 
    fasta_file=NULL 
) 
) 

如果您有興趣測試這個類,這裏​​是一個小重複的例子:

df <- data.frame(Parameter=c('Version', 'Fasta file'), 
       Value=c('1.5.2.8','c:\\a\\b.fasta')) 
write.table(df, 'jnk.txt', sep='\t', row.names=F) 

p <- MQParameters$new('jnk.txt') 
p$getVersion() 
# [1] "1.5.2.8" 
p$getFastaFile() 
# [1] "c:/a/b.fasta" 
p$getFastaFile(new_basedir='.') 
# [1] "./b.fasta" 

我不知道該怎麼文檔參數,因爲這些參數實際上屬於創建者,但不屬於類。函數中的其他方法的參數怎麼樣?
什麼是使用它的方法來記錄類的首選方法?

我很想擺脫RStudio「正常」的功能,如打F1獲得直接的幫助頁面。

任何建議是非常值得歡迎的。

通過搜索互聯網,我已經看到了一些報道關於這個話題的github,但他們超過10歲,我希望有一些好消息,或者至少一些好的做法在那裏。

UPDATE:

感謝的mikeck答案我現在有類一個不錯的文件和它的方法。但是,我仍然缺乏的是讓函數/方法及其參數的提示想在這個屏幕截圖爲了一個共同的功能的可能性:

rStudio help on functions

,如果我能以某種方式手動註冊我的功能我想知道,但由於它沒有特定的名稱(它總是與用於對象OBJECTNAME$methodeCall()的變量對象名稱耦合),所以我不知道如何執行此操作。

回答

5

我的理解是,這是最簡單的記錄與同一@name爲您的班級NULL對象,因爲這提供了最大的靈活性。我在我的一個軟件包中使用R6類;您可以查看roxygen here。我已經在下面包含了一個小樣本:

#' Python Environment 
#' 
#' The Python Environment Class. Provides an interface to a Python process. 
#' 
#' 
#' @section Usage: 
#' \preformatted{py = PythonEnv$new(port, path) 
#' 
#' py$start() 
#' 
#' py$running 
#' 
#' py$exec(..., file = NULL) 
#' py$stop(force = FALSE) 
#' 
#' } 
#' 
#' @section Arguments: 
#' \code{port} The port to use for communication with Python. 
#' 
#' \code{path} The path to the Python executable. 
#' 
#' \code{...} Commands to run or named variables to set in the Python process. 
#' 
#' \code{file} File containing Python code to execute. 
#' 
#' \code{force} If \code{TRUE}, force the Python process to terminate 
#' using a sytem call. 
#' 
#' @section Methods: 
#' \code{$new()} Initialize a Python interface. The Python process is not 
#' started automatically. 
#' 
#' \code{$start()} Start the Python process. The Python process runs 
#' asynchronously. 
#' 
#' \code{$running} Check if the Python process is running. 
#' 
#' \code{$exec()} Execute the specified Python 
#' commands and invisibly return printed Python output (if any). 
#' Alternatively, the \code{file} argument can be used to specify 
#' a file containing Python code. Note that there will be no return 
#' value unless an explicit Python \code{print} statement is executed. 
#' 
#' \code{$stop()} Stop the Python process by sending a request to the 
#' Python process. If \code{force = TRUE}, the process will be 
#' terminated using a system call instead. 
#' 
#' @name PythonEnv 
#' @examples 
#' pypath = Sys.which('python') 
#' if(nchar(pypath) > 0) { 
#' py = PythonEnv$new(path = pypath, port = 6011) 
#' py$start() 
#' py$running 
#' py$stop(force = TRUE) 
#' } else 
#' message("No Python distribution found!") 
NULL 

#' @export 
PythonEnv = R6::R6Class("PythonEnv", cloneable = FALSE, 
    # actual class definition... 

還有其他的替代(但相似)的實現; this example使用@docType class這可能會適合你最好:

#' Class providing object with methods for communication with lightning-viz server 
#' 
#' @docType class 
#' @importFrom R6 R6Class 
#' @importFrom RCurl postForm 
#' @importFrom RJSONIO fromJSON toJSON 
#' @importFrom httr POST 
#' @export 
#' @keywords data 
#' @return Object of \code{\link{R6Class}} with methods for communication with lightning-viz server. 
#' @format \code{\link{R6Class}} object. 
#' @examples 
#' Lightning$new("http://localhost:3000/") 
#' Lightning$new("http://your-lightning.herokuapp.com/") 
#' @field serveraddress Stores address of your lightning server. 
#' @field sessionid Stores id of your current session on the server. 
#' @field url Stores url of the last visualization created by this object. 
#' @field autoopen Checks if the server is automatically opening the visualizations. 
#' @field notebook Checks if the server is in the jupyter notebook mode. 
#' #' @section Methods: 
#' \describe{ 
#' \item{Documentation}{For full documentation of each method go to https://github.com/lightning-viz/lightining-r/} 
#' \item{\code{new(serveraddress)}}{This method is used to create object of this class with \code{serveraddress} as address of the server object is connecting to.} 
#' 
#' \item{\code{sethost(serveraddress)}}{This method changes server that you are contacting with to \code{serveraddress}.} 
#' \item{\code{createsession(sessionname = "")}}{This method creates new session on the server with optionally given name in \code{sessionname}.} 
#' \item{\code{usesession(sessionid)}}{This method changes currently used session on the server to the one with id given in \code{sessionid} parameter.} 
#' \item{\code{openviz(vizid = NA)}}{This method by default opens most recently created by this object visualization. If \code{vizid} parameter is given, it opens a visualization with given id instead.} 
#' \item{\code{enableautoopening()}}{This method enables auto opening of every visualisation that you create since that moment. Disabled by default.} 
#' \item{\code{disableautoopening()}}{This method disables auto opening of every visualisation that you create since that moment. Disabled by default.} 
#' \item{\code{line(series, index = NA, color = NA, label = NA, size = NA, xaxis = NA, yaxis = NA, logScaleX = "false", logScaleY = "false")}}{This method creates a line visualization for vector/matrix with each row representing a line, given in \code{series}.} 
#' \item{\code{scatter(x, y, color = NA, label = NA, size = NA, alpha = NA, xaxis = NA, yaxis = NA)}}{This method creates a scatterplot for points with coordinates given in vectors \code{x, y}.} 
#' \item{\code{linestacked(series, color = NA, label = NA, size = NA)}}{This method creates a plot of multiple lines given in matrix \code{series}, with an ability to hide and show every one of them.} 
#' \item{\code{force(matrix, color = NA, label = NA, size = NA)}}{This method creates a force plot for matrix given in \code{matrix}.} 
#' \item{\code{graph(x, y, matrix, color = NA, label = NA, size = NA)}}{This method creates a graph of points with coordinates given in \code{x, y} vectors, with connection given in \code{matrix} connectivity matrix.} 
#' \item{\code{map(regions, weights, colormap)}}{This method creates a world (or USA) map, marking regions given as a vector of abbreviations (3-char for countries, 2-char for states) in \code{regions} with weights given in \code{weights} vector and with \code{colormap} color (string from colorbrewer).} 
#' \item{\code{graphbundled(x, y, matrix, color = NA, label = NA, size = NA)}}{This method creates a bundled graph of points with coordinates given in \code{x, y} vectors, with connection given in \code{matrix} connectivity matrix. Lines on this graph are stacked a bit more than in the \code{graph} function.} 
#' \item{\code{matrix(matrix, colormap)}}{This method creates a visualization of matrix given in \code{matrix} parameter, with its contents used as weights for the colormap given in \code{colormap} (string from colorbrewer).} 
#' \item{\code{adjacency(matrix, label = NA)}}{This method creates a visualization for adjacency matrix given in \code{matrix} parameter.} 
#' \item{\code{scatterline(x, y, t, color = NA, label = NA, size = NA)}}{This method creates a scatterplot for coordinates in vectors \code{x, y} and assignes a line plot to every point on that plot. Each line is given as a row in \code{t} matrix.} 
#' \item{\code{scatter3(x, y, z, color = NA, label = NA, size = NA, alpha = NA)}}{This method creates a 3D scatterplot for coordinates given in vectors \code{x, y, z}.} 
#' \item{\code{image(imgpath)}}{This method uploads image from file \code{imgpath} to the server and creates a visualisation of it.} 
#' \item{\code{gallery(imgpathvector)}}{This method uploads images from vector of file paths \code{imgpathvector} to the server and creates a gallery of these images.}} 


Lightning <- R6Class("Lightning", 
... 
) 

編輯

如果你正在尋找一種方式來獲得RStudio提示試圖用一個類的方法時,展現出來......不幸的是我不要以爲您會找到一種解決方案,不需要以消除R6類的便利性和功能性的方式編寫類。

@ f-privé提供了一個答案,可以做你想做的事 - 只需將該邏輯擴展到所有方法即可。例如,myclass$my_method改爲由

my_method = function(r6obj) { 
    r6obj$my_method() 
} 
obj$my_method() 
my_method(obj)  # equivalent 

訪問換句話說,你需要爲每個方法的包裝。這顯然不方便編程,而僅僅使用obj$my_method(),並且可能會首先破壞使用R6類的有用性。

這裏的問題實際上是RStudio。 IDE沒有通過分析代碼來識別R6類的好方法,也不能區分定義的類的方法和列表或環境的元素。此外,RStudio不能提供任意函數的幫助,如:

na.omit()   # tooltip shows up when cursor is within the parentheses 
foo = na.omit 
foo()    # no tooltip 

這是相當類似於呼叫特定R6對象的方法。

+0

謝謝,這給了我一個很好的幫助文檔!我仍在尋找的是獲得rStudio的幫助。我喜歡的是自動完成和黃色的小窗口顯示函數調用的參數。現在我得到完整的函數名稱,但沒有提示參數。我必須通過'?MQParameters'進行搜索以獲得幫助,並且必須通讀方法部分。 – drmariod

+0

@drmariod我編輯了我的答案來討論工具提示問題,但不幸的是我不認爲你會發現當前版本的RStudio令人滿意的解決方案。 – mikeck

+0

@mikeck我不記得我是如何以這種方式結束的,但你可以檢查text2vec中的例子 - https://github.com/dselivanov/text2vec/blob/master/R/model_LSA.R。 我只記得,我也遇到了找到最好的方式來記錄R6的問題。 –

2

我認爲R人不想使用$new(...)來獲得新類的實例。他們更喜歡使用與該類相同名稱的函數來構造它的一個實例。

所以,你可以做的是重新命名您的R6ClassGenerator MQParameters_R6Class並創建另一個功能

MQParameters <- function(file_path = NA) { 
    MQParameters_R6Class$new(file_path) 
} 

然後,文件這一功能與任何其他功能,您將獲得「小黃窗口顯示函數調用其參數「來自RStudio。和快樂的R用戶。

+0

我喜歡這個主意,但我仍然不滿意!我認爲這是更好的編程方式! :-)讓我們看看是否有更好的建議,但謝謝我可能會考慮它。 – drmariod