在 Shiny 中,如何直接将源代码替换为 R 函数调用或语句?
In Shiny, how do I directly substitute source code into R function calls or statements?
现在,我正在使用用 R 编写的网络应用程序创建框架 Shiny 进行编码,我对我的 Shiny 应用程序使用 app.R
格式,这意味着我有两个变量传递给Shiny::runapp(ui, server)
。 server
的代码如下所示:
#SERVER CODE: INCLUDE YOUR SOURCE FILES BELOW
server <- function(input, output, session) {
source("mod_input.R", local=TRUE)
source("mod_normalization.R", local=TRUE)
source("mod_outlier.R", local=TRUE)
}
这些源文件包含如下代码:
output$outlier_PCA <- renderPlot({
withProgress(message='Creating PCA plot...', {
if(!is.null(rval$outlier_rw)) {
rwl <<- logTrans(rval$outlier_rw)
print(mdl_pca(rwl) + ggtitle("Principal Component Analysis Graph (Retained Data)"))
}
})
})
...这是非常标准的——我将我的 ggplot2
图表输出到变量,这些变量是 Shiny 处理并发送到输出的一般 output
变量的 "features"。
Shiny 应用程序的另一半是 UI:
ui <- navbarPage("test app",
###INPUT MODULE
tabPanel("Data Input",
##ROW 1
fluidRow(
column(12,
#FILE UPLOAD
tabsetPanel("Upload files",
tabPanel("Upload Raw Data",
fileInput("rw_file",label = "Raw Sample Counts (CSV)"),
fileInput("srr_file", label= "Experiment-Sample Metadata (CSV) \n[Optional]"),
fileInput("srx_file", label= "Experiment Summary (CSV) \n[Optional]"),
actionButton("submit_raw_data", label='Submit data')
),
#DATA PREVIEW
tabPanel("Preview Raw Data",
DT::dataTableOutput("rw_preview", width = "auto", height="auto"),
uiOutput("rw_preview_range"),
uiOutput("grab_rw_preview_range")
),
#NORMALIZED DATA PREVIEW
tabPanel("Preview Normalized Data",
DT::dataTableOutput("norm_preview", width = "auto", height="auto"),
uiOutput("norm_preview_range"),
uiOutput("grab_norm_preview_range")
)
)
)
)
##ROW 2
),
###END INPUT MODULE
###OUTLIER REMOVAL MODULE
tabPanel("Outlier Removal",
column(4,
wellPanel(
tabsetPanel("Settings",
tabPanel("Filter data",
sliderInput(inputId = "outlier_zscore", "Resistant Z-score Sample Threshold",
min=0, max=10.0,
value=2.0, step=0.01),
actionButton(inputId = "grab_outlier_zscore", "Filter data")
),
tabPanel("Sample Boxplot",
uiOutput("outlier_preview_range") ,
actionButton(inputId = "grab_outlier_preview_range", "View samples")
)
)
),
plotOutput("outlier_PCA")
),
column(8,
plotOutput("outlier_boxplot"),
plotOutput("outlier_density")
)
)
###END OUTLIER REMOVAL MODULE
)
现在,我想将 UI 的每个部分与此应用程序中的某个模块相对应,但我不能像对服务器和 source("filename.R", local=TRUE)
.它根本不起作用。
我可以使用另一种方法将文件中的源代码直接替换为 R function/statement吗?
由于@DavidRobinson 没有给出答案,我总结一下他的解决方案:
实现此目的的最佳方法是简单地将 UI 组件函数的输出分配为变量,然后直接将这些变量包含在更大的 UI 函数本身中。
ui_input <-
tabPanel("Data Input",
##ROW 1
fluidRow(...and so on...
ui <- navbarPage("Shiny App",
ui_input
)
现在,我正在使用用 R 编写的网络应用程序创建框架 Shiny 进行编码,我对我的 Shiny 应用程序使用 app.R
格式,这意味着我有两个变量传递给Shiny::runapp(ui, server)
。 server
的代码如下所示:
#SERVER CODE: INCLUDE YOUR SOURCE FILES BELOW
server <- function(input, output, session) {
source("mod_input.R", local=TRUE)
source("mod_normalization.R", local=TRUE)
source("mod_outlier.R", local=TRUE)
}
这些源文件包含如下代码:
output$outlier_PCA <- renderPlot({
withProgress(message='Creating PCA plot...', {
if(!is.null(rval$outlier_rw)) {
rwl <<- logTrans(rval$outlier_rw)
print(mdl_pca(rwl) + ggtitle("Principal Component Analysis Graph (Retained Data)"))
}
})
})
...这是非常标准的——我将我的 ggplot2
图表输出到变量,这些变量是 Shiny 处理并发送到输出的一般 output
变量的 "features"。
Shiny 应用程序的另一半是 UI:
ui <- navbarPage("test app",
###INPUT MODULE
tabPanel("Data Input",
##ROW 1
fluidRow(
column(12,
#FILE UPLOAD
tabsetPanel("Upload files",
tabPanel("Upload Raw Data",
fileInput("rw_file",label = "Raw Sample Counts (CSV)"),
fileInput("srr_file", label= "Experiment-Sample Metadata (CSV) \n[Optional]"),
fileInput("srx_file", label= "Experiment Summary (CSV) \n[Optional]"),
actionButton("submit_raw_data", label='Submit data')
),
#DATA PREVIEW
tabPanel("Preview Raw Data",
DT::dataTableOutput("rw_preview", width = "auto", height="auto"),
uiOutput("rw_preview_range"),
uiOutput("grab_rw_preview_range")
),
#NORMALIZED DATA PREVIEW
tabPanel("Preview Normalized Data",
DT::dataTableOutput("norm_preview", width = "auto", height="auto"),
uiOutput("norm_preview_range"),
uiOutput("grab_norm_preview_range")
)
)
)
)
##ROW 2
),
###END INPUT MODULE
###OUTLIER REMOVAL MODULE
tabPanel("Outlier Removal",
column(4,
wellPanel(
tabsetPanel("Settings",
tabPanel("Filter data",
sliderInput(inputId = "outlier_zscore", "Resistant Z-score Sample Threshold",
min=0, max=10.0,
value=2.0, step=0.01),
actionButton(inputId = "grab_outlier_zscore", "Filter data")
),
tabPanel("Sample Boxplot",
uiOutput("outlier_preview_range") ,
actionButton(inputId = "grab_outlier_preview_range", "View samples")
)
)
),
plotOutput("outlier_PCA")
),
column(8,
plotOutput("outlier_boxplot"),
plotOutput("outlier_density")
)
)
###END OUTLIER REMOVAL MODULE
)
现在,我想将 UI 的每个部分与此应用程序中的某个模块相对应,但我不能像对服务器和 source("filename.R", local=TRUE)
.它根本不起作用。
我可以使用另一种方法将文件中的源代码直接替换为 R function/statement吗?
由于@DavidRobinson 没有给出答案,我总结一下他的解决方案:
实现此目的的最佳方法是简单地将 UI 组件函数的输出分配为变量,然后直接将这些变量包含在更大的 UI 函数本身中。
ui_input <-
tabPanel("Data Input",
##ROW 1
fluidRow(...and so on...
ui <- navbarPage("Shiny App",
ui_input
)