Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rshiny: Refactoring code

Tags:

r

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")
like image 318
XerXes Avatar asked Jun 27 '26 20:06

XerXes


1 Answers

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")
like image 68
amwill04 Avatar answered Jun 30 '26 19:06

amwill04



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!