Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #12 from stencila/develop
Browse files Browse the repository at this point in the history
v0.28.0
  • Loading branch information
Nokome Bentley authored Sep 7, 2017
2 parents 7496b50 + 53a51f2 commit ec1295d
Show file tree
Hide file tree
Showing 45 changed files with 1,818 additions and 198 deletions.
16 changes: 11 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,32 @@ Authors@R: c(person("Nokome", "Bentley", email = "nokome@stenci.la", role = c("a
URL: https://github.com/stencila/r#readme
BugReports: https://github.com/stencila/r/issues
License: Apache-2.0
Version: 0.27.0
Version: 0.28.0
LazyData: TRUE
Imports:
base64enc,
DBI,
evaluate,
httpuv,
jsonlite,
mime,
methods,
R6,
RSQLite,
stringr,
tidyverse,
tools
tools,
urltools
Suggests:
devtools,
testthat,
covr,
packagedocs
covr
RoxygenNote: 6.0.1
Collate:
Collate:
'addins.R'
'file-storer.R'
'host-http-server.R'
'sqlite-context.R'
'r-context.R'
'host.R'
'main.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(FileStorer)
export(RContext)
export(SqliteContext)
export(host)
export(pack)
export(type)
Expand Down
10 changes: 10 additions & 0 deletions R/addins.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#' Opens the file that is currently open in the source editor in Stencila
addin_open <- function() {
# Get the path of the file currently in the source editor
editor <- rstudioapi::getSourceEditorContext()
path <- editor$path
# If path is empty (as it is for a new document) we could revert to loading the
# editor$contents but currently error
if (nchar(path)==0) stop('Error when attempting to open in Stencila: no file currently active', call.=FALSE)
host$open(paste0('file://', path))
}
120 changes: 120 additions & 0 deletions R/file-storer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#' A file storer
#' @export
FileStorer <- R6::R6Class('FileStorer',

public = list(

#' @section new():
#'
#' Create a new \code{FileStorer}
#'
#' \describe{
#' \item{path}{Local file system path. Default \code{''}}
#' \item{version}{Version of storer. Currently ignored but required for compatability. Default \code{NULL}}
#' }
initialize = function (path = '/', version = NULL) {
path <- suppressWarnings(normalizePath(path))
if (file.exists(path)) {
isdir <- file.info(path)[1, 'isdir']
} else {
isdir <- !str_detect(path, '\\.([[:alnum:]]+)$')
}
private$.dir <- if (isdir) path else dirname(path)
private$.main <- if (isdir) NULL else basename(path)
},

#' @section getDirectory():
#'
#' Get the directory path of this storer
getDirectory = function () {
private$.dir
},

#' @section getMain():
#'
#' Get the main file, if specified
getMain = function () {
private$.main
},

#' @section getFiles():
#'
#' List files within this storer
getFiles = function () {
I(setdiff(list.files(private$.dir), list.dirs(private$.dir, recursive = FALSE, full.names = FALSE)))
},

#' @section getInfo():
#'
#' Get information about this storer:
#'
#' \describe{
#' \item{dir}{The absolute file sytem path of the storer's directory}
#' \item{main}{The main file, if specified}
#' \item{files}{A list of file in the storer}
#' }
getInfo = function () {
list(
dir=self$getDirectory(),
main=self$getMain(),
files=self$getFiles()
)
},

#' @section readFile():
#'
#' Read content from the file at \code{path}
#'
#' \describe{
#' \item{path}{Path within the storer}
#' }
readFile = function (path) {
path <- file.path(private$.dir, path)
connection <- file(path, 'rt')
content <- readChar(connection, file.info(path)$size)
close(connection)
content
},

#' @section writeFile():
#'
#' Write content to the file at \code{path}
#'
#' \describe{
#' \item{path}{Path within the storer}
#' \item{content}{Content to write}
#' }
writeFile = function (path, content) {
path <- file.path(private$.dir, path)
connection <- file(path, 'wt')
# It would seem to make sense to use `writeChar` here but that
# appears to write a binary encoded file. So using `cat`.
cat(content, file=connection)
close(connection)
},

#' @section deleteFile():
#'
#' Delete the file at \code{path}
#'
#' \describe{
#' \item{path}{Path within the storer}
#' }
deleteFile = function (path) {
path <- file.path(private$.dir, path)
file.remove(path)
}

),

private = list(
.dir = NULL,
.main = NULL
)
)

FileStorer$spec <- list(
name = 'FileStorer',
base = 'Storer',
aliases = c('file')
)
Loading

0 comments on commit ec1295d

Please sign in to comment.