Gaurav Chawla
Gaurav Chawla

Reputation: 1643

How can I read an input file into shiny?

My shiny app is based on a single .csv file for its data. So I need to input the data on startup. That way if someone opens the app on their system, the result is is shown by the app right way. How can I do this?

Upvotes: 3

Views: 3744

Answers (1)

John Paul
John Paul

Reputation: 12664

You have a few options here. I assume you know how to read the file into R using read.csv or something similar.

You can put the input the read.csv in one of three places:

1) Globlal.r: If you have a global.r file you can use read.csv there and the data will be directly available to both the ui and the server functions. Typically you do not need to do this but it is an option.

For the next two options the data will be directly available to the server side, but must be passed to the ui side through one of the render functions.

2)Server.r but NOT in shinyServer: In this case the read.csv is located in server.r file but outside of the shinyServer() function. The file will be read in one time per session and will not change. This is a common place to read in data.

3) Server.r and in shinyServer: In this case the read.csv is part of theshinySever() function. This is a good place to read the data if you want some degree of reactivity to be involved. For example, if the user chooses which data to input or if the data file is constantly updating (stock prices maybe) and you want to check the data file periodically for updates while the user is working.

Note: You also need to consider where the data is stored. You can put it in a subdirectory of your app directory and then read it is using a relative (not absolute) path. This is helpful if you are testing your app on your desktop, but are going to deploy it elsewhere and don't wish to rewrite the code to take the new directory structure into account.

Upvotes: 9

Related Questions