Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

How to separate out tabs into their own script in a shiny app

$
0
0

I am using several libraries to build a dashboard in shiny.

In my ui section I define some tabs, something like this:

ui <- dashboardPage(dashboardHeader(title = "Ecommerce Dashboard"),
                    dashboardSidebar(
                        sidebarMenu(
                            menuItem("Channel Analysis", tabName = "channel",
                            menuItem("Funnel Analysis", tabName = "funnel")
                        )),
                        tabItems(
                            tabItem(tabName = "channel",
                                    # channel analysis tab code and items here
                                    ),
                                    tabItem(tabName = "funnel",
                                    # funnel analysis tab code and items here
                                    )
                        )
                    ))

Then in server I have:

server <- function(input, output) {

    # lots of code relating to channel analysis here

    # lots of code relating to funnel analysis here
}

It's in the ui part that the two analysis are separated into self contained parts within tabs but in the server part it's just lots of code assigned to various outputs which are housed in either channel analysis or funnel analysis tabs.

My server <- function(input, output) {} part of my script s getting pretty long and I wanted to create two separate scripts to hold the code belonging to each analysis in the two tabs.

I tried something like this:

server <- function(input, output) {

# pull in code relating to channel analysis tab
source('channel_analysis_tab.R')

# lots of code relating to funnel analysis here
source('funnel_analysis_tab.R')
}

Then each of the two new scripts contain regular shiny server section code related to their respective tabs, e.g. in channel_analysis_tab.R the first few lines are info box definitions:

## Info boxes
output$SessionsBox <- renderInfoBox({
  infoBox(
    "Sessions", format(sum(dat.trended$Sessions), big.mark = ","), icon = icon("bar-chart"), color = "teal"
  )
})

output$TransactionsBox <- renderInfoBox({
  infoBox(
    "Transactions", format(sum(dat.trended$Transactions), big.mark = ","), icon = icon("bar-chart"), color = "teal"
  )
})

output$RevenueBox <- renderInfoBox({
  infoBox(
    "Revenue", paste0("$",format(sum(dat.trended$Revenue), big.mark = ",")), icon = icon("dollar"), color = "teal"
  )
})

However, when I run this I get:

Listening on http://127.0.0.1:7346 Warning: Error in <-: object 'output' not found 53: eval [channel_analysis_tab.R#4] 52: eval 50: source 49: server [/home/rstudio/blah/projects/ecommerce/app.R#171] Error in output$SessionsBox <- renderInfoBox({ : object 'output' not found

If I want to simplify my Shiny app by breaking it into smaller scripts, how should I do this?


Viewing all articles
Browse latest Browse all 206278

Trending Articles