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 ?
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.
@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.