简体   繁体   English

在Shiny中,如何直接将源代码替换为R函数调用或语句?

[英]In Shiny, how do I directly substitute source code into R function calls or statements?

Right now, I'm coding in Shiny, the web-app creation framework written in R, and I use the app.R format for my Shiny app, which mean I have two variables that I pass to Shiny::runapp(ui, server) . 现在,我正在使用Shiny(用R编写的Web应用程序创建框架)进行编码,并且我为Shiny应用程序使用app.R格式,这意味着我有两个变量要传递给Shiny::runapp(ui, server) The code for the server looks like this: 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)
}

These source files contain code like this: 这些源文件包含如下代码:

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)"))
    }
  })
})

...which is pretty standard--I output my ggplot2 graphs to variables that are "features" of the general output variable that Shiny processes and sends to the output. ...这很标准-我将ggplot2图输出到变量,这些变量是Shiny处理ggplot2送到output的常规output变量的“特征”。

The other half of a Shiny app is the UI: UI的另一半是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
)

Now, I want to compartmentalize each part of the UI that corresponds to a certain module in this app, but I can't simply do the same thing like I did with server and source("filename.R", local=TRUE) . 现在,我想分隔与该应用程序中某个模块相对应的UI的每个部分,但是我不能像在server和source("filename.R", local=TRUE)那样简单地做同样的事情。 It simply doesn't work. 它根本不起作用。

Is there another option I can use for directly substituting source code from a file directly into a R function/statement? 我是否可以使用另一个选项将源代码直接从文件直接替换为R函数/语句?

Since @DavidRobinson didn't provide an answer, I will summarize his solution: 由于@DavidRobinson没有提供答案,因此我将总结他的解决方案:

The best way to accomplish this is to simply assign the output of the UI component functions as variables, and then to directly include these variables in the larger UI function itself. 实现此目的的最佳方法是简单地将UI组件函数的输出分配为变量,然后将这些变量直接包含在较大的UI函数本身中。

ui_input <- 
tabPanel("Data Input",
 ##ROW 1
 fluidRow(...and so on...


ui <- navbarPage("Shiny App",
  ui_input
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM