R shiny modify reactive data frame

放肆的年华 提交于 2019-12-11 03:23:39

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/20401932/r-shiny-modify-reactive-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!