3

For my Rshiny app, I have a ui and a server file. Both the ui and the server file got really huge, now I would like to refactor some parts of the ui/server to other functions and then call those functions. For example I have this UI-code:

shinyUI(

       something,
       something,
       something
)

And I would like to do this:

shinyUI(

       somethingFunction()
)

And the function is stored in a different data as:

somethingFunction() <- function()

   something,
   something,
   something

The UI still works and I still get the same UI. But the functionalities from the server don't work anymore. It seems as if I had just a basic server like:

shinyServer(function(input, output, session) {

})

I have the feeling that once I factor out the functions the server can't communicate properly with the UI anymore. Can anybody help me?

EDIT

Here comes a relevant snippet of the code. ImportDataTab()-File contains the refactored code.

The server 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

  })



})

The UI-File

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


  )
)

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

1 Answer 1

2

If have understood what you are after consider the following file structure:

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

Where myFunctions.R contains all your functions. To make available all the functions defined in myFunctions.R simply source that file at the top of your server file. For example:

source("/Volumes/full/directory/path/myFunctions.R")
Sign up to request clarification or add additional context in comments.

3 Comments

I thought, it worked at first sight, but I used the wrong code. Sorry for the confusion.
It seems like the moment I call a function within the UI-file, the server stops working. Even print("Hello") won't work anymore.
You shouldnt really be calling functions into the UI. If you post your shinyApp we can have a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.