Adding csv to shiny

Hello everyone,

I am new to R and I would like to know how to add a csv file to shiny?
should it be added to ui or server or both? and what is the correct code.

I have tried


but it didn't work

I want to create an interactive chart using the data in my csv file.



There are a few possible reasons why this does not work. One reason could be that the filepath is incorrectly specified. You should firstly check what the working directory is and or have the file within the same folder as the project / scripts. However, you shouldn't really use setwd() or absolute paths particularly when the deploying app to a server. Another reason is that you are calling the file in the wrong place. The ui file is just for the user interface so anything within this function should not contain anything other than user interface functions. Generally, you may wish to add it to a server file but prior to the server function being defined. You can also define it within a reactive data statement if the files need to be changed using a widget which can make it more scalable if you wish to add extra data files.

# read the data first
mydata  <- read.csv("myfile.csv")

server <- function(input, output){

      output$plot <- renderplot({
              plot(mydata$x, mydata$y)



I usually use a third Rscript which contains the data, extra functions and the alike then source this file in the ui and server components. That way, the ui and server files are not being filled with things that are not relevant to the ui or server operations. I believe you could probably go the other way around and source the ui and server files in this initialisation script but I haven't tried to deploy such an app yet.

Actually it also does depend on whether you play on using information within that csv for widgets. If you are then you may need to read this file within ui file prior to defining your ui function.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.