2

I'm trying to build an app in which I can select the data file (input$dataset), then add a new datetime column formatting date and time previous columns to make plots with ggplot2.

I use 'within' that previously worked in batch scripts and in Rstudio. But now I get this error message:

no applicable method for 'within' applied to an object of class "reactive"

How can I apply this method to a reactive object? Should I use another command? cbind? ddply?

  datos=reactive({
    read.csv(input$dataset,header=T,sep=";",na.strings="-99.900")
    within(datos, datetime <- as.POSIXct(paste(FECHA,H_SOLAR),format = "%y/%m/%d %H:%M:%S"))        
  })

Thanks in advance

EDIT:

Following the answer below I understand a reactive source can't be modified, say add a column to the data frame. The point is I want to use ggplot in this way (adapting an old R script):

p=ggplot(datos(),aes_string(x="datetime", y=input$var,colour="as.character(stat_id)")) +
      geom_line()  
  }

So, how should I add datetime to datos? Maybe creating datos2 as a new reactive source merging datos and datetime?

EDIT 2 Added full code to github https://github.com/pacomet/git

pacomet
  • 5,011
  • 12
  • 59
  • 111
  • Have you tried without the `within`? So just `datetime <- as.POSIXct(paste(datos$FECHA,datos$H_SOLAR),format = "%y/%m/%d %H:%M:%S"))`? – tcash21 Dec 05 '13 at 14:51
  • Hi, I used just the commands from an older script where I also wanted to save the new date with the dataframe. I'll give it a try. Thanks. – pacomet Dec 06 '13 at 13:18

2 Answers2

2

You cannot change the datafile directly - it is a reactive source that cannot be changed except by a user input (in this case the choice of data file).

You have 2 choices (that I know of):

1) Make a new object that holds the reformatted date:

NewDate<-reactive({ as.POSIXct(paste(FECHA,H_SOLAR),  
            format = "%y/%m/%d %H:%M:%S")})

then use NewDate() as your variable for graphing.

2) Change the date format within the function that makes the graph. e.g.

plot(x~as.POSIXct(paste(FECHA,H_SOLAR),format="%y/%m/%d %H:%M:%S"),   
      data=datos())

Here is a somewhat similar issue:Formatting reactive data.frame

EDIT In response to the edited question - here is an updated answer.

I don't know much about ggplot but if the issue is to get this all into one data.frame, then you might want to do something like this:

datos=reactive({read.csv(input$dataset,header=T,sep=";",na.strings="-99.900"))} 
NewDate<-reactive <- ({as.POSIXct(paste(FECHA,H_SOLAR),  
   format = "%y/%m/%d %H:%M:%S" )})  
datos2<-reactive({ data.frame(datos(),NewDate() })

Then try using datos2() in ggplot - I think that should give you what you need.

Community
  • 1
  • 1
John Paul
  • 12,196
  • 6
  • 55
  • 75
  • Thanks, I'll try the first option. – pacomet Dec 06 '13 at 13:19
  • Hi, this does not work for me as I'm using ggplot for graphing. I need to add a column to datos and not to create new object. Or find the way to use two different objects in ggplot. I'm gonna add ggplot command to the original question. Thanks for your help. – pacomet Dec 10 '13 at 11:19
  • Hi @john-paul Tried your code but can't get it to work. I had also tried something similar before. It complains an error that is dumped to Javascript console. How can I check that error? – pacomet Dec 12 '13 at 15:16
  • In the meantime I used an R script to preprocess data and added the datetime column before starting the shiny app. There has to be a shiny solution. I'll try to post data and code to github so you can check the full app. Thanks – pacomet Dec 12 '13 at 15:22
1

I think this question can be closed thanks to the answer for @dieter-menne to another question about subsetting reactive data frames. The point is to create a new local variable, similar to @john-paul suggestion.

Please take a look at https://stackoverflow.com/a/20569512/709777

Community
  • 1
  • 1
pacomet
  • 5,011
  • 12
  • 59
  • 111