Newbie question - loading excel file data into r shiny as a DataTable




I am working through a datacamp tutorial that uses an underlying url link for the DataTables, but I cannot find the lines of code in order for Shiny to read data from an underlying excel or csv file stored on my own laptop?

Please help?

I have found code to upload a dataframe into the mainPanel but not referencing an underlying file, which I want to do calculations on like mean, standard deviation, correlation etc.

Many thanks!

Datacamp example:



This command:


loads data into R's memory. Specifically, after running this command you'll have movies dataset in memory. You can use it to do all of the things you've talked about. For example, try the following:

movies$runtime %>% mean(na.rm = TRUE) # this will calculate mean for `runtime` variable


Sorry @mishabalyasin , I think you have interpreted my question incorrectly.

I am not looking to manipulate the existing link (i can do this already) but to create my own DataTable in excel, load into shiny and be able to do calculations on it like mean etc...

Hope that clarifies??


I think, you are mixing whole lot of things together and it is difficult for me to understand what is the end goal.

For instance, do you actually need Excel or are you just trying to have a matrix with columns and rows? And what is the connection between DataTable and Excel? Do you want to have a DataTable widget that you want to interact with?
Why do you need Shiny? Are you planning to run analysis interactively? Is that just for yourself (so you can run it locally) or are you planning to give it someone else to interact with (so you need to upload your Shiny app somewhere else)?

In any case, saving and loading data in R can be done with, for example, saveRDS and readRDS. Then you can use resulting file and put it anywhere you like, including S3 as it was done with Datacamp example.


Yes I do and I want to run it locally on my laptop as a proof of concept!



Ok, so you can prepare the dataset in whatever way you want, save it with saveRDS and then load it inside of Shiny app similar to how it was done in your original post. Just keep in mind that you'll need to save it to a variable, so:

data <- readRDS("/local/path/to/file/")

Then you can use data in any way you need. For example, you can render it as a DataTable. There is an example of how it can be done.


Thanks, so sorry for being a newbie but can you show me some example code for saveRDS?


Sure, but keep in mind that it'll depend on many factors (your OS, whether you are using projects or not (you should) and many other things). But in general, you would save it in a following way:

saveRDS(mtcars, "name_of_the_object.rds")

You can then read it in memory like this:

data <- readRDS("name_of_the_object.rds")

And use it as data in your code.


I assume you put the internal directory location and file name after (mtcars, "C:/admin/DataSource.rds")??

thanks so much


Yes, you can put the full path there.

By default, it'll save file to current working directory. As I've said, if you are working in a project then it'll a directory inside of the project. This way you'll have all the files connected to a project in one place.


Hi @mishabalyasin

I can use the instruction below to create a dataframe but I cant incoporate the file contents to memory in the code. What I am doing wrong please?

# loads csv file okay
saveRDS(movies, "movies.rds")
movies <- readRDS("movies.rds")

# Main Code
saveRDS(movies, "movies.rds")
movies <- readRDS("movies.rds")

# > runApp('R/datacampbasic/load csv of movies.R')
# Error in load(movies) : bad 'file' argument


I'm pretty sure load(movies) is redundant as you've already loaded the data with the readRDS("movies.rds"), if you run class(movies) I reckon that will already be a data frame.


Thank you so much @ciaranevans

Does this look okay?


# loads csv file from working directory
saveRDS(movies, "movies.rds")
movies <- readRDS("movies.rds")


The class() call is purely there for us to see what readRDS() was returning, it returns the data type of the passed in variable.

Can you use a breakpoint or the Environment tab in R Studio to see the contents of movies?

If you run your code line by line, you can see the variables you assign appear on the right:

Here I'm looking at the earthquakes variable by double clicking on it in the Environment tab. Maybe this will help!

Edit: If I run class(earthquakes) in the console I get:

> class(earthquakes)
[1] "data.frame"