你可以使用我为此设计的Ramd 包。首先devtools::install_github('robertzk/Ramd')
。现在您可以包含任意脚本(采用与 node.js 项目中相同的“异步模块化依赖”格式):
用户界面
library(Ramd)
define('public_data', function(public_data) {
shinyUI( navbarPage("Synodos Data Explorer", id="nav",
public_data, kinome_screens
))
})
public_data.r
tabPanel("Public Data",
titlePanel("Explore Public Datasets"),
source("code1.R")
)
kinome_screens.r
tabPanel("Kinome Screens",
titlePanel("Exploring Kinome Screening Data"),
source("code2.R")
)
解释
该define
函数允许您包含相对于当前执行文件所在目录的 R 脚本。例如,如果您想将代码结构化为目录格式,您可以执行以下操作:
define('dir/some_file', 'dir2/some_other_file', function(one, two) {
# You can use one and two, the "return value" (last expression) in the
# above files
})
请注意,正确使用它完全消除了全局变量的使用,并将解决您管理全局状态和代码组织问题。这背后的主要技巧是,当您获取文件时,如下所示:
# test.r
c(1,2,3) # This will be ignored, since it isn't at the end of the file
list(1, "hello!")
# otherfile.r
x <- source('test.r')$value
然后x
是现在list(1, "hello!")
(特别是,像这样的调用的“返回值”base::source
将始终给出文件中的最后一个表达式。您想要传递给包含该文件的其他文件的所有内容都应该在最后包装,可能在一个列表,如果你想返回多个东西。你可以使用它来将你复杂的 Shiny 项目分层嵌套在一个健全的组织结构中。
这是一个完整的工作示例,说明了如何使用 Ramd 在目录中构造一些代码:
### main.r
define('blah/foo', 'blah2/faa', function(one, two) {
print(one + two)
})
### blah/foo.r
x <- 1
y <- 2
x + y
### blah2/faa.r
z <- 1
w <- 5
w - z
### R console
library(Ramd); source('main.r')
# [1] 7