Skip to content

Commit

Permalink
Merge pull request #12 from Sage-Bionetworks/synapser
Browse files Browse the repository at this point in the history
Use synapser
  • Loading branch information
Kenneth Daily authored Feb 6, 2018
2 parents be57904 + 5362218 commit cb161b6
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 87 deletions.
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
Package: knit2synapse
Type: Package
Title: Knit RMarkdown to a Synapse Wiki page.
Version: 0.0.0.9000
Version: 0.1
Date: 2015-04-27
Authors@R: c(
person("Brian", "Bot", email = "[email protected]", role = "cre"),
person("Kenneth", "Daily", email = "[email protected]", role = "aut"))
Maintainer: Kenneth Daily <[email protected]>
person("Kenneth", "Daily", email = "[email protected]", role = "aut"),
person("Thanneer", "Perumal", email = "[email protected]", role = "aut"))
Maintainer: Kenneth Daily <[email protected]>
Description: Knit RMarkdown to Synapse Wiki page.
License: CC0
Imports: knitr,
RCurl,
stringr,
tools,
synapseClient
synapser
RoxygenNote: 6.0.1
6 changes: 5 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
exportPattern("^[[:alpha:]]+")
# Generated by roxygen2: do not edit by hand

export(createAndKnitToFileEntity)
export(createAndKnitToFolderEntity)
export(knitfile2synapse)
83 changes: 31 additions & 52 deletions R/knitfile2synapse.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ knitfile2synapse <- function(file, owner, parentWikiId=NULL, wikiName=NULL, over

## IF OWNER IS CHARACTER, TRY TO GET FROM SYNAPSE
if( is.character(owner) & length(owner) == 1 ){
owner <- synapseClient::synGet(owner, downloadFile=FALSE)
owner <- synapser::synGet(owner, downloadFile=FALSE)
}

#####
Expand Down Expand Up @@ -66,42 +66,32 @@ knitfile2synapse <- function(file, owner, parentWikiId=NULL, wikiName=NULL, over
} else {
stop(sprintf("markdown file %s does not exist at this location: %s", basename(mdName), mdName))
}
att <- list.files(knitPlotDir, full.names=TRUE)

att <- as.list(list.files(knitPlotDir, full.names=TRUE))

## New wiki page

newWiki <- synapser::Wiki(owner=owner,
title=wikiName,
markdownFile=mdFile,
attachments=att,
parentWikiId=as.character(parentWikiId))
## Create/retrieve and store Wiki markdown to Synapse
w <- try(synapseClient::synGetWiki(owner),silent=T)
w <- try(synapser::synGetWiki(owner),silent=T)

## create new wiki if doesn't exist
if (class(w) == 'try-error') {
w <- synapseClient::WikiPage(owner=owner,
title=wikiName,
markdown=readChar(mdFile, file.info(mdFile)$size))
# delete existing wiki along with history
if (class(w)[1] == 'try-error') {
w <- newWiki
# delete existing wiki along with history
} else if (overwrite) {
w <- synapseClient::synGetWiki(owner)
w <- synapseClient::synDelete(w)
w <- synapseClient::WikiPage(owner=owner,
title=wikiName,
markdown=readChar(mdFile, file.info(mdFile)$size))
w <- synapser::synDelete(w)
w <- newWiki
# update existing wiki
} else {
w <- synapseClient::synGetWiki(owner)
w@properties$title <- wikiName
w@properties$markdown <- readChar(mdFile, file.info(mdFile)$size)
}

## Add the attachments
if (length(att) > 0 ){
w@attachments <- as.list(att)
}

## Set the parent wiki id, if one provided
if(!is.null(parentWikiId)){
w@properties$parentWikiId <- parentWikiId
w <- newWiki
}

## Store to Synapse
w <- synapseClient::synStore(w)
w <- synapser::synStore(w)

# Undo changes to options
knitr::opts_chunk$restore(old_knitr_opts)
Expand All @@ -111,56 +101,45 @@ knitfile2synapse <- function(file, owner, parentWikiId=NULL, wikiName=NULL, over
return(w)
}

#' storeAndKnitToFileEntity
#' createAndKnitToFileEntity
#'
#' Store a local RMarkdown file to Synapse and then knit it to that file's WikiPage.
#'
#' @export
#' @param file path to a local .Rmd file which to knit
#' @param parentId A synapseClient::Project or synapseClient::Folder entity (or Synapse ID of an entity) where the File will be created
#' @param fileName Name of the synapseClient::File to create
#' @param owner A Synapse entity (or Synapse ID of an entity) which will own the resulting WikiPage (usually a Project, Folder, or File)
#' @param parentWikiId If the resulting WikiPage is to be a subpage of another WikiPage, this is the ID for the parent WikiPage (NOTE: owner is still required)
#' @param wikiName A title for the resulting WikiPage - will default to the file name without the .Rmd extension
#' @param overwrite Only if owner specified and parentWikiId is NULL - flag for whether or not to overwrite the previous root WikiPage (if it exists). This will remove the history of changes for this Wiki page.
#' @param knitmd Flag for whether or not to knit; if FALSE and file already exists, don't knit it again
#' @return a synapseClient::WikiPage object
storeAndKnitToFileEntity <- function(file, parentId, fileName=NULL, owner=NULL, parentWikiId=NULL,
wikiName=NULL, overwrite=FALSE, knitmd=TRUE, ...) {
createAndKnitToFileEntity <- function(file, parentId, fileName=NULL, wikiName=NULL, overwrite=FALSE, knitmd=TRUE, ...) {

if (is.null(owner)) {
entity <- synapseClient::File(file, parentId=parentId, name=fileName)
entity <- synapseClient::synStore(entity, ...)
owner <- entity
}

knitfile2synapse(file=file, owner=owner, parentWikiId=parentWikiId, wikiName=wikiName,
entity <- synapser::File(file, parentId=parentId, name=fileName)
entity <- synapser::synStore(entity, ...)

knitfile2synapse(file=file, owner=entity, wikiName=wikiName,
overwrite=overwrite, knitmd=knitmd)
}

#' knitToFolderEntity
#' createAndKnitToFolderEntity
#'
#' Create a Synapse Folder entity and knit a local RMarkdown file to it's WikiPage.
#'
#' @export
#' @param file path to a local .Rmd file which to knit
#' @param parentId A synapseClient::Project or synapseClient::Folder entity (or Synapse ID of an entity) where the Folder will be created
#' @param folderName Name of the synapseClient::Folder to create
#' @param owner A Synapse entity (or Synapse ID of an entity) which will own the resulting WikiPage (usually a synapseClient::Project, synapseClient::Folder, or synapseClient::File)
#' @param parentWikiId If the resulting WikiPage is to be a subpage of another WikiPage, this is the ID for the parent WikiPage (NOTE: owner is still required)
#' @param wikiName A title for the resulting WikiPage - will default to the file name without the .Rmd extension
#' @param overwrite Only if owner specified and parentWikiId is NULL - flag for whether or not to overwrite the previous root WikiPage (if it exists). This will remove the history of changes for this Wiki page.
#' @param knitmd Flag for whether or not to knit; if FALSE and file already exists, don't knit it again
#' @return a synapseClient::WikiPage entity object
knitToFolderEntity <- function(file, parentId, folderName, owner=NULL, parentWikiId=NULL,
wikiName=NULL, overwrite=FALSE, knitmd=TRUE, ...) {
createAndKnitToFolderEntity <- function(file, parentId, folderName,
wikiName=NULL, overwrite=FALSE, knitmd=TRUE, ...) {

if (is.null(owner)) {
entity <- synapseClient::Folder(parentId=parentId, name=folderName)
entity <- synapseClient::synStore(entity, ...)
owner <- entity
}

knitfile2synapse(file=file, owner=owner, parentWikiId=parentWikiId, wikiName=wikiName,
entity <- synapser::Folder(parentId=parentId, name=folderName)
entity <- synapser::synStore(entity, ...)

knitfile2synapse(file=file, owner=entity, wikiName=wikiName,
overwrite=overwrite, knitmd=knitmd)
}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
## knit2synapse: Knit RMarkdown files to Synapse wikis.

> This version has [synapser](https://github.com/Sage-Bionetworks/synapser) as a dependency. The last version of this package to use the [rSynapseClient](https://github.com/Sage-Bionetworks/rSynapseClient) is `v0.1`.
### Installing

Using `devtools`:
First, install the `synapser` package:

```
install.packages("synapser", repos=c("https://sage-bionetworks.github.io/staging-ran", "http://cran.fhcrc.org"))
```

The, using `devtools`:

```
devtools::install_github("Sage-Bionetworks/knit2synapse")
Expand Down
2 changes: 1 addition & 1 deletion knit2synapse.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ LaTeX: pdfLaTeX
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd
PackageRoxygenize: rd,collate,namespace,vignette

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

18 changes: 6 additions & 12 deletions man/knitToFolderEntity.Rd → man/createAndKnitToFolderEntity.Rd

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

3 changes: 1 addition & 2 deletions man/knit2synapse.Rd

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

3 changes: 1 addition & 2 deletions man/knitfile2synapse.Rd

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

0 comments on commit cb161b6

Please sign in to comment.