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

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
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.

Related

deploy shiny's publish button from R function-script

I have some code that reorganizes some spreadsheets and then creates a shiny from the spreadsheet. All of this code sits in a master script within functions. This way, when I need to run a particular function I just source the master script and run the function.
Is it possible to publish the output onto shinyapps.io without physically pushing the publish button? I just want to be able to run the function and have it publish automatically.
edit 1: is there was a way I could make "appDir" = "some function in my master script"?
edit 2: Fixed the problem to edit 1. Don't put shiny code within a function.
edit 3: Is there a way to call the app.R file and my .csv file in rsconnect if they are in different locations?

R shiny: read csv a file in UI

Context
I built a shiny app in R that i can run as i want. It means i can insert read.csv before UI and then use the file loaded in R in the UI. Now i need to create a shortcut in desktop so i need to insert the read.csv in the UI. But i can't insert a read.csv in the UI. So i need to know how can i load a file and use it in UI so i can run "C:\Program Files\R\R-3.3.2\bin\R.exe" -e shiny::runApp('C:/Users/Desktop/Cal_pro')
In the Cal_pro folder i put my ui.R and server.R
Does anyone has an idea ?

Web application which runs R

I have written my R script with some functions to do some calculations. On the output of my calculations, I have created plots in shiny(4 tab panels in that as of now, which is using the data specified in my global.R). Now I am trying to automate the process and deliver it to non technical users. So something like, users feed in 2 csv files in web UI and click a button in the web UI, which will then call my r scripts to do the calculations, then my server.R script should build plots on the output of it. Any similar examples? I tried different links, but nothing seems to be relevant.
If I add my R script using source(**.R) and use the functions mentioned in there. Or Do I still have to save output in data folder and specify something in global.R or just use the output of the function in my plot construction?
If I have the fileInput option in first tab in my tabpanels, how can I make stop the user from viewing the other tabs(tabs with graphs) without loading the input csv files and clicking the "Go" button?
Can I do this whole thing in Shiny ? Or better to go for rook or some other framework which calls my r script just for calculations?

R shiny updateSelectInput in observeEvent not working

In my Shiny app my users can upload a file from the server to view the results. I have the user select the file using a selectInput, and then they click an actionButton to load the file. I also want users to be able to delete files and add new files, and I've set that up successfully using separate actionButtons. When the user deletes or adds a file I want to update the selectInput so the deleted files aren't there anymore, and the added files are. I tried this using updateSelectInput in my observeEvent code but its not working. Here's my observeEvent for the delete file portion:
#Delete parameter files
observeEvent(input$delete,{
file.remove(input$input_file) #deletes the file selected in the input_file widget
choices <- list.files("C:/Users/shiny/testapp/", pattern="*.Rdata")
updateSelectInput(session,"input_file",choices) #supposed to update the input_file widget
})
When I run the app with this code in it, two things happen. In the App window, I get this text right above the input_file widget: [object Object
And in my R console I get:
Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future
version of jsonlite, this option will not be supported, and named
vectors will be translated into arrays instead of objects. If you want
JSON object output, please use a named list instead. See ?toJSON.
I have included the session parameter in my shinyServer call:
shinyServer(function(input, output, session)
Any advice would be appreciated.
I found a corner case that also causes this bug. Upon putting tabPanel() in a renderUI(), an updateSelectInput in an unrelated area of my code just stopped working.
To resolve, leave the tabPanel in ui.R, and use renderUI() to adjust within the function instead of bringing it all into server.R, like here: updateSelectInput order of operations/race condition

Embedding Shiny app in knitr document

I have a shiny app which has a ui.R, server.R and global.R. The app directory (name = dash) contains the folder 'data' in which the dataset resides. Also, this app folder is inside the project's working directory.
In global.R I read the data as:
dash <- read.table("data/ntraj1acc.txt", sep=",", header=T)
This app works fine. Now, I am trying to embed it in a ioslides presentation which otherwise works good. The example in External Applications section on rmarkdown website also works perfect in my presentation. But when I replace the path in system.file to my app, I get the error:
No Shiny application exists at the path ""
Here is how I replaced the path:
shinyAppDir(
system.file("dash", package="shiny"),
options=list(
width="100%", height=700
)
)
After the error, I tried following:
shinyAppDir(
"C:/Users/durraniu/Documents/Trajectory-one/dash",
options=list(
width="100%", height=700
)
)
But then I got a new error:
object 'dash' not found
Which means that it is not parsing global.R.
How can I fix this problem?
I successfully embeded an application within an interactive shiny document with the following rmarkdown chunk:
```
shinyAppDir("D:/Documents/OneDrive/Notes/R-Explore/shiny/01-ages/",
options=list(
width="100%", height=550
)
)
```
All 3 files: the ui.R, server.R, and test.Rmd are at the above absolute path
system.file("dash", package="shiny") is definitely not going to work, that's looking for a folder named "dash" inside the shiny package itself, which obviously doesn't exist.
Also we have a limitation in RMarkdown that it doesn't call global.R for embedded Shiny apps, sorry that this fact appears not to have made it into the documentation. https://github.com/rstudio/rmarkdown/issues/211
Finally, unlike in a regular Shiny app, in an RMarkdown document's embedded Shiny apps you can't assume that relative paths from server.R will resolve correctly; that's because many apps can be running in the single session, including the RMarkdown document itself (which is also a Shiny app), so there's no way to make all of them happy.
For now, I recommend you load the data directly in the RMarkdown document and from the sub-apps just assume the data is already loaded--I believe this works, please correct me if I'm mistaken.
2018 update to this question:
Thanks to the bookdown package, knitr is now able to include HTML widgets, Shiny apps, and arbitrary websites using
knitr::included_url()
knitr::include_app()
Check out the document in the bookdown book for more examples:
https://bookdown.org/yihui/bookdown/web-pages-and-shiny-apps.html

Resources