nississippi
nississippi

Reputation: 327

R - Shiny App: Read in csv and convert it to a dataframe / ts object

I want to create a Forecast Shiny App. The user should be able to upload a csv or xls file. With the data in the file, the forecast should be done.

Since the file could be another one each time, I created a reactive read in:

data <- reactive ({
    file1 <- input$file
    if(is.null(file1)){return()}
    read.table(file = file1$datapath, sep = input$sep, header = input$header)
         })

To be able to further work with the data and create a simple plot, I created a dataframe:

output$plot <- renderPlot ({

#create dataframe
df <- as.data.frame(data())
plot(df)

)}

My input file is a simple csv which only contains one column with pageimpressions. The title of the column also is pageimpressions. Now the df <- as.data.frame(t(data()))part creates a df which looks like this:

enter image description here

Does anybody know how to create a "normal" dataframe which looks like this:

enter image description here

AND/OR: Does anybody know how to directly create a ts object of the upper dataframe?

Many thanks in advance!

(Used packages: library(shiny))

Upvotes: 0

Views: 1740

Answers (1)

Eli Berkow
Eli Berkow

Reputation: 2725

At least for the "normal" dataframe I'm not sure why you're running this line:

df <- as.data.frame(t(data()))

data() is already the "normal" dataframe once it gets read into R using read.table()/read.csv(). If you run class(data()) you should get [1] "data.frame"

the t() creates the transpose of the dataframe but it doesn't seem you're after that?

You could try plot.ts() or ts.plot().

For ts the xts package is quite useful although you need some sort of time corresponding to the data points which I don't see in the dataset above.

Upvotes: 0

Related Questions