首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rshiny:重构代码

Rshiny:重构代码
EN

Stack Overflow用户
提问于 2015-10-15 12:16:46
回答 1查看 299关注 0票数 3

对于我的Rshiny应用程序,我有一个ui和一个服务器文件。ui和服务器文件都变得非常庞大,现在我想将ui/服务器的某些部分重构为其他函数,然后调用这些函数。例如,我有一个UI代码:

代码语言:javascript
复制
shinyUI(

       something,
       something,
       something
)

我想这样做:

代码语言:javascript
复制
shinyUI(

       somethingFunction()
)

该函数存储在不同的数据中,如下所示:

代码语言:javascript
复制
somethingFunction() <- function()

   something,
   something,
   something

UI仍然工作,而我仍然得到相同的UI。但是服务器的功能不再起作用了。似乎我只有一个基本的服务器,比如:

代码语言:javascript
复制
shinyServer(function(input, output, session) {

})

我有一种感觉,一旦我分析出这些功能,服务器就无法与UI进行适当的通信了。有人能帮我吗?

编辑

下面是代码的相关片段。ImportDataTab()-File包含重构的代码。

服务器文件:

代码语言:javascript
复制
    library(Korridorbudgetierung)
library(shinythemes)


source("ImportDataTab.R")


shinyServer(function(input, output, session) {


  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.

    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    file.data <-  as.tbl(read.csv(inFile$datapath, header = input$header,
                                  sep = input$sep, quote = input$quote, dec = ","))

    file.data

  })



})

UI文件

代码语言:javascript
复制
  library(dygraphs)
library(xtable)
library(htmltools)
library(shiny)
library(shinythemes)
library(d3heatmap)
library(datasets)
library(DBI)
library(RMySQL)

source("ImportDataTab.R")

shinyUI(


  navbarPage(title="App", 


             tabPanel("Home"),
             ImportDataTab(),
             tabPanel("New Tab")


  )
)

ImportDataTab()-File:

代码语言:javascript
复制
library(shiny)


ImportDataTab <- function()
  navbarMenu(
    "1. Import Data", ImportFile(), DatabaseFile(), WebsiteFile()
  )

#######################################################################
#FUNCTION
#######################################################################
ImportFile <- function()

  tabPanel("Data from Files",
           h4("Uploading data", align="center"),
           sidebarLayout(
             sidebarPanel(
               fileInput('file1', 'Choose file to upload',
                         accept = c(
                           'text/csv',
                           'text/comma-separated-values',
                           'text/tab-separated-values',
                           'text/plain',
                           '.csv',
                           '.tsv'
                         )
               ),
               tags$hr(),

               checkboxInput('header', 'Header', TRUE),
               radioButtons('sep', 'Separator',
                            c(Comma=',',
                              Semicolon=';',
                              Tab='\t'),
                            ','),
               radioButtons('quote', 'Quote',
                            c(None='',
                              'Double Quote'='"',
                              'Single Quote'="'"),
                            '"'),
               tags$hr(),
               headerPanel(
               h6("Powered by", align = "left",
                    style = "font-weight: 600;color: black")),

               br(),
               tags$img(src= 'pic.png', height=70, width=70)
             ),
             mainPanel(
               tableOutput('contents')
             )

           )
  )

#######################################################################
#FUNCTION
#######################################################################

DatabaseFile <- function()

tabPanel("Data from Database",
         h4("Uploading data", align="center"),
         sidebarLayout(
           sidebarPanel(
             selectInput("tables", "Select a table", c("Cali", "Florida"))
           ),

           mainPanel(
             tableOutput('contents')
           )
         )
)

#######################################################################
#FUNCTION
#######################################################################

WebsiteFile <- function()

  tabPanel("Data Extraction from Website")
EN

回答 1

Stack Overflow用户

发布于 2015-10-15 12:23:32

如果您已经了解了之后的内容,请考虑以下文件结构:

代码语言:javascript
复制
--ShinyApp
   --ui.R
   --server.R
   --myFunctions.R

其中myFunctions.R包含所有函数。要使myFunctions.R中定义的所有函数可用,只需将该文件放在server文件的顶部。例如:

代码语言:javascript
复制
source("/Volumes/full/directory/path/myFunctions.R")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33148336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档