对于我的Rshiny应用程序,我有一个ui和一个服务器文件。ui和服务器文件都变得非常庞大,现在我想将ui/服务器的某些部分重构为其他函数,然后调用这些函数。例如,我有一个UI代码:
shinyUI(
something,
something,
something
)我想这样做:
shinyUI(
somethingFunction()
)该函数存储在不同的数据中,如下所示:
somethingFunction() <- function()
something,
something,
somethingUI仍然工作,而我仍然得到相同的UI。但是服务器的功能不再起作用了。似乎我只有一个基本的服务器,比如:
shinyServer(function(input, output, session) {
})我有一种感觉,一旦我分析出这些功能,服务器就无法与UI进行适当的通信了。有人能帮我吗?
编辑
下面是代码的相关片段。ImportDataTab()-File包含重构的代码。
服务器文件:
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()-File:
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")发布于 2015-10-15 12:23:32
如果您已经了解了之后的内容,请考虑以下文件结构:
--ShinyApp
--ui.R
--server.R
--myFunctions.R其中myFunctions.R包含所有函数。要使myFunctions.R中定义的所有函数可用,只需将该文件放在server文件的顶部。例如:
source("/Volumes/full/directory/path/myFunctions.R")https://stackoverflow.com/questions/33148336
复制相似问题