2015-10-15 130 views
2

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

shinyUI(

     something, 
     something, 
     something 
) 

而且我想这样做:

shinyUI(

     somethingFunction() 
) 

和函数存储在不同的数据:

somethingFunction() <- function() 

    something, 
    something, 
    something 

的用户界面仍然有效,我仍然得到相同的用户界面。但是服务器的功能不再工作。看来,如果我有一个像只是一个基本的服务器:

shinyServer(function(input, output, session) { 

}) 

我,一旦我分析出该服务器无法与UI正常通信了功能的感觉。有谁能够帮助我?

编辑

这里谈到的代码的相关片段。 ImportDataTab() - 文件包含重构的代码。

服务器文件:

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-文件

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() - 文件:

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

回答

2

如果明白你正在考虑以下后什么文件结构:

--ShinyApp 
    --ui.R 
    --server.R 
    --myFunctions.R 

其中myFunctions.R包含您的所有功能。要提供myFunctions.R中定义的所有功能,只需在server文件顶部的source文件。例如:

source("/Volumes/full/directory/path/myFunctions.R") 
+0

我想,它一见钟情,但我使用了错误的代码。对困惑感到抱歉。 – XerXes

+0

好像我在UI文件中调用一个函数的那一刻,服务器就停止工作。即使打印(“你好”)也不会工作了。 – XerXes

+0

你不应该在UI中调用函数。如果您发布您的shinyApp,我们可以看看 – amwill04