```{r setup,echo = false}库(searchbiociondumon)stopifnot(Biocinstaller :: biocversion()==“3.0”)``````:: Markdown()```#组织函数,文件和包中的代码Martin Morgan
2014年10月29日##编程_R_是一种编程语言 - 您自己的功能```{r fun} fun < - function(n){x < - rnorm(n)y < - x + rnorm(n,sd =。5)LM(Y〜x)}``` - 查看_rstudio_函数向导!- 迭代```{r reateration,eval = false} ##'副作用'(I在1:10)消息(i)##后续计算的结果sapply(1:10,function(i){sum(rnorm(1000))})``` - 其他迭代范例:`lapply()`,`apply()`,`mapply()/ map()`。- 条件执行```{R条件,eval = false} Sapply(1:10,功能(i)){if(sum(rnorm(1000))> 0){“hi!”}否则{“低!”从脚本到包脚本的``##包含数据和转换的组合。通常,转换通常是特殊的,并且严重依赖各种包提供的功能。有时,脚本包含一个有用的代码块,可以在不同的地方重用。我们在本课程中遇到的例子可能包括DNA序列的GC含量(或者已经有一个功能?查看`R Biocpkg(“BioStrings”)“!)并从一种类型创建一个简单的”地图“向另一个注释。创建一个包很容易和有益。 - Only need to think carefully about how to implement the function once. - Code in the package is reused -- if you correct an error, then all your scripts automatically benefit. - Share with your lab mates / colleagues for consistent results, and with the wider community for fame and glory. What is an _R_ package? - Simple directory structure of required files. Minimal: MyPackage |-- DESCRIPTION (title, author, version, etc.) |-- NAMESPACE (imports / exports) |-- R (R function definitions) |-- gc.R |-- annoHelper.R |-- man (help pages) |-- MyPackage.Rd |-- gc.Rd |-- annoHelper.Rd Check out the _Rstudio_ package wizard! ## Lab ### GC-content Write a function that takes a `DNAStringSet` and returns the GC content. Modify the function using a conditional statement to work whether provided a `DNAString` or a `DNAStringSet`. Test the function. Save the function in a file on your AMI. ### Annotation-helper Write a function that takes as its argument Ensembl gene identifiers (like the `rownames()` of the _SummarizedExperiment_ object in the RNASeq vignette yesterday) and uses the `select()` method and annotation package `r Biocannopkg("org.Hs.eg.db")` to return a named character vector, where the names of the vector are the Ensembl identifiers and the values are the corresponding gene SYMBOLs. Adopt some simple-to-implement policy for handling Ensembl identifiers that map to more than one gene symbol. Save this function to another file ### A package Use the _RStudio_ wizard to create a package from the files containing your GC-content and annotation-helper functions.