Download and open Csv in r shiny - first shiny app



Hi Folks,

I would like to download a csv file from a bank website and plot each of the 400 expenditure columns in graphs in r shiny. The user should be able to select which of the 400 columns they wish to see on a plot and these plots should have the Date (the first column in the dataframe) on the x axis and the expenditure data on the y axis.

I have an r code to download the csv to a directory, unzip this file, open in r, and put the data into a dataframe.

I have the basic code for an r shiny app, obtained from here :

My question is where can I put this r code I have to dowload the file into a dataframe, into the r shiny code?

does it go into the ui<-fluidpage section? or into the server<-function(input, output) section?


If the download does not need to be called dynamically (i.e., it just needs to be done once when the app is loaded) then you can just put the code to download it at the top of your script outside of both your ui and server elements (if you are building the app from a single file, if you are using separate files, ui.R and server.R, then I would put it in my global.R file). This code will be run when the app is loaded and then you can just reference the dataframe from inside your server function with whatever name you assigned it.


Alternatively, if you want to be able to redownload to file mid session, then you can add an action button in your ui code and then use an eventReactive() call with the action button as the trigger and your code to download the data inside it.


This is perfect, thank you so much @tbradley!

I have one more question, I hope this is okay:

There are over 400 columns in my dataframe. The aim of the r shiny app is for the user to select from a drop down which currency they’d like to see on the plot.

In the server section, inside a renderplot{()} function must I create a plot for each of the 400 columns?

Thank you in advance :smiley:


No. I would create a vector of the column names with something like:

col_names <- colnames(data)

and then I would render a ui in my server code using renderUI with a selectInput() call inside it, where the choices argument for your selectInput() is set to col_names. From there you filter your dataframe with the selected choice of that input. I would save that as a new reactive dataframe and then use that reactive value inside of your renderPlot() call.

If you are not familiar with renderUI(), it is the same as other render functions in that you need to place a corresponding output function in your ui code. In this case, the output function is uiOutput()

There are a few ways you could go about selecting the columns to include based on the selectInput value selected. The easiest way if you are not familiar with tidyeval would be like this:

data[ , input$selected_cols]

by default, the value returned by a select input is a vector so even if you allow the user to select multiple choices, that will properly subset your data. (NOTE: input$select_cols is my simulated inputId for your selectInput() call.


Thank you again for your help, @tbradley :smiley:


One other thing, if you choose the option for loading your data outside of your server function, then you can create the col_names vector outside of it as well. If you do it that way then you can create the selectInput() directly in your ui and skip the renderUI() step. If you load your data with the eventReactive() method, then you will need to use renderUI