简体   繁体   English

R闪亮修改反应数据框

[英]R shiny modify reactive data frame

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. 我正在尝试构建一个应用程序,在其中可以选择数据文件(input $ dataset),然后添加新的datetime列,以格式化先前的日期和时间来使用ggplot2进行绘图。

I use 'within' that previously worked in batch scripts and in Rstudio. 我使用以前在批处理脚本和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? cbind? ddply? 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): 关键是我想以这种方式使用ggplot(适应旧的R脚本):

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? 那么,我应该如何在datos中添加日期时间? Maybe creating datos2 as a new reactive source merging datos and datetime? 也许创建datos2作为合并datos和datetime的新反应源?

EDIT 2 Added full code to github https://github.com/pacomet/git 编辑2将完整的代码添加到github https://github.com/pacomet/git

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): 您有2个选择(我知道):

1) Make a new object that holds the reformatted date: 1)制作一个包含重新格式化日期的新对象:

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

then use NewDate() as your variable for graphing. 然后将NewDate()用作绘制图形的变量。

2) Change the date format within the function that makes the graph. 2)在制作图表的函数中更改日期格式。 eg 例如

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 这是一个类似的问题: 格式化反应性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: 我对ggplot不太了解,但是如果问题是将所有内容整合到一个data.frame ,那么您可能想要执行以下操作:

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. 然后尝试在ggplot使用datos2() -我认为这应该ggplot您的需求。

I think this question can be closed thanks to the answer for @dieter-menne to another question about subsetting reactive data frames. 我想这个问题可以解决,这要归功于@ dieter-menne对另一个关于设置反应数据框架的问题的回答。 The point is to create a new local variable, similar to @john-paul suggestion. 关键是要创建一个新的局部变量,类似于@ john-paul建议。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM