Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split Shiny app code over multiple files in RStudio? [closed]

Tags:

r

rstudio

shiny

I tried to split code for a shiny application over different files, but can't get this to work decently in Shiny. My attempt can be found in this demo

How can I split up my code over different files, but still keep the 'Run App button ' and have 'Code Completion' back in RStudio?

if not ! can i integrate shiny with Visual Studio ?

like image 867
Magdy Avatar asked Mar 24 '17 15:03

Magdy


People also ask

How do you run a Shiny app with two files?

To create a two-file app, create a new directory (for example, newdir/ ) and place two files, called ui. R and server. R , in the directory. To run it, call runApp("newdir") .

How do you organize a Shiny app?

If you are creating a large or long-term Shiny app, I highly recommend that you organise your app in the same way as an R package. This means that you: Put all R code in the R/ directory. Write a function that starts your app (i.e. calls shinyApp() with your UI and server).

What are the main 2 parts of an R Shiny app?

I'll start by showing you the minimum boilerplate needed for a Shiny app, and then you'll learn how to start and stop it. Next you'll learn the two key components of every Shiny app: the UI (short for user interface) which defines how your app looks, and the server function which defines how your app works.

How do you stop a Shiny app in R?

Automatically stop a Shiny app when closing the browser tab By adding a single line to the server code session$onSessionEnded(stopApp) , a Shiny app will automatically stop running whenever the browser tab (or any session) is closed.


Video Answer


2 Answers

Yes, you can achieve that very easily in the same way you do that with every other project in RStudio: by using the R mechanisms provided to achieve that:

  • define functions and/or objects in separate files.
  • use source() in your main file to load their definitions

Code completion for shiny functions only occurs in RStudio when the shiny package is loaded using library(shiny). The 'Run App' button will be visible for the main file in the application. In the example below, that would be the app.R file. So if you want to run your app from within RStudio, you always have to go back to the main file.

standard example

An example :

In a file app.R you put:

library(shiny)
source('myUI.R', local = TRUE)
source('myServer.R')


shinyApp(
  ui = myUI,
  server = myserver
)

This code does nothing else but initiate the objects myUI and myserver and call the app.

The file myUI.R contains

source('Tabs.R')
myUI <- shinyUI({
  fluidPage(
    tabsetPanel(
      Tab1,
      Tab2
    )
  )
})

This file defines the UI object used in app.R. The function tabsetPanel takes a number of tabPanels as arguments. These tabPanels are created in the following file (Tabs.R), so that one has to be sourced before the UI is constructed.

The file Tabs.R contains:

Tab1 <- tabPanel("First Tab",
                 selectInput("select",
                             "Choose one",
                             choices = letters[1:3],
                             selected = 'a'))

Tab2 <- tabPanel("Second Tab",
                 textOutput('mychoice'))

This file creates the tabPanel objects to be added to the tabsetPanel. In my own code, I store every tabPanel definition in a separate file.

The file myServer.R contains:

myserver <- function(input,output,session){
  output$mychoice <- renderText(
    input$select
  )
}

And if you want, you can again create separate files with functions that can be used inside the server function. But you always have to follow the classic R logic: assign things to an object and refer to that object at the position you want to insert it.

You can also source code directly inside the server() function. In that case you should source locally using source(..., local = TRUE), so the created objects are contained inside the server function. See also : https://shiny.rstudio.com/articles/scoping.html

Using modules

If you want to take it up a notch and reuse a certain logic and layout (eg a plot option control panel that should be attached to some plots), you should go to modules. (see also http://shiny.rstudio.com/articles/modules.html )

Modules can again be stored in a separate file, and that file sourced in the app.R file you have.

like image 180
Joris Meys Avatar answered Oct 04 '22 15:10

Joris Meys


@Joris Meys 's answer covered the topic of splitting shiny code into files. Though one part of the question is to use the run app button, which may not be available even if the organization make a valid shiny app.

If you search this question you can find this issue, then following to the commit made in that issue you can find what's the rule to have a run app button, and this function about isShinyAppDir, this function of shinyfiletype:

Basically any folder have ui.R, server.R, app.R, global.R, www folder will be considered as shiny folder(the detailed conditions are more complex, see source code), then the above 4 files will have run app button.

One thing I noticed is that usually you can keep the app running, make some changes then reload app to see the changes, but if you sourced other file, reload app button will not reload changes in that sourced file.

like image 37
dracodoc Avatar answered Oct 04 '22 14:10

dracodoc