Source() from server.R - r

I have a R file containing some variables I use in my general app.
I was minding if it was possible to source this R file from my server.R and then output it into the ui.R
Do you know if it is possible or do you know some advices ?

Related

How to upload a CSV file into R/shiny and automatically populate selectors based on column headers

I want to upload a csv file from a \data folder inside the R project.
So far I've been reading questions, but most answers seemed a bit too fancy, with user interaction when selecting a data file.
I simply want R Shiny to read a data file (just the one) without any user interaction.
I have the standard files ui.R and server.R I place them in a working directory.
I have a csv file with data which I place in a subdirectory called 'data'
I might be missing something, but this should work right?
df = rio::import("./data/my_filename.csv")

Load a csv file in R and share in all sessions?

I have a shiny application that uses a csv file to generate different figures. I upload my application in my personal linux server using shiny-server.
I use this structure for my application
global.R
ui.R
server.R
Inside my global.R file I have this line, which help me to load and read my csv file
df <- read_csv("../Desktop/covid_2021-02-15.csv")
But my application is very slow, I read that objects in the global.R script, are read only one time and are share in all sessions.
Is there other way to load this data frame to have a more efficient application?
In addition to the comments from Gregor and HubertL:
To load large CSV files can slow down. I had the same issue and changed to r binary file (rds) with saveRDS() and readRDS(). As a first step you can try rds file and see if the issue is solved.
To check whether there is a performance difference you can use system.time(). Returns the time taken to evaluate any R expression.
In your case:
df <- read_csv("../Desktop/covid_2021-02-15.csv")
# Save an object to a file
saveRDS(df, file = "my_data.rds")
# Restore the object
readRDS(file = "my_data.rds")

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 ?

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

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.

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