2015-10-15 182 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