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
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.
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
Related
I want to load dataset from package ElemStatLearn in R studio.
But when I load the dataset, my Global Environment panel shows
library("ElemStatLearn")
data("nci")
However, when I execute
View("nci")
I can see the whole data but cannot export it to a dataframe.
How can I convert or export this dataset into a dataframe?
You can do
df <- data.frame(nci)
Another way to go around would be
df <- get(data("nci"))
If you had done anything with the name nci that required it's modification or evaluation, the R engine would have at that point pulled in the values and you would no longer have had a promise. Instead, you asked to look not at an R name but at an R literal character value. The value of "nci" is just "nci". The value of nci on the other hand has 6,830 entries when I try to look at it.
The data function can accept a character value for purposes of retrieving an externally stored object, but the View function expects a real (unquoted) R name. Or you could have used: View(as.name("nci") )
I have tried this. But its giving me error
Datecreated<-c('Created Time')
I will get this data from cloud using APIs. I need to define the format of this Created Time column as d-mon-yy
For example- 18-Nov-17
How can I achiEve this. I am new to R.
Any help woud be appreciated.
Your screenshot is showing you assign the string ‘created time’ to the variable Datecreated. I’m not sure where or how you’re getting your data but I’m assuming that is your issue. Once you have your data you can use akrun’s answer of as.Date(column, “%d-%b-%y”)
MyDates <- c(“17-Jan-16”, “6-Feb-17”)
FormattedDates <- as.Date(MyDates, “%d-%b-%y”)
I'm having an issue trying to format date in r... tried the following codes
rdate<-as.Date(dusted$time2,"%d/%m/%y") and also the recommendations on this stackoverflow question Changing date format in R but still couldn't get it to work.
geov<-dusted
geov$newdate <- strptime(as.character(geov$time2), "%d/%m/%Y")
all i'm getting is NA for the whole column for date. This are daily values, i would love if r can read them. Data available here https://www.dropbox.com/s/awstha04muoz66y/dusted.txt?dl=0
To convert to date, as long as you successfully imported the data already into a data frame such as dusted or geov, and have time2 holding dates as strings resembling 10-27-06, try:
geov$time2 = as.Date(geov$time2, "%m-%d-%y")
equal sign = used just to save on typing. It is equivalent to <-, so you can still use <- if you prefer
this stores the converted dates right back into geov$time2, overwriting it, instead of creating a new variable geov$newdate as you did in your original question. This is because a new variable is not required for conversion. But if for some reason you really need a new variable, feel free to use geov$newdate
similarly, you also didn't need to copy dusted to a new geov data frame just to convert. It does save time for testing purposes though, just in case the conversion doesn't work you can restart from copying dusted to geov instead of having to re-import data from a file into dusted
Additional resources
help(strptime) for looking up date code references such as %y. On Linux, man date can reveal the date codes
I using the Alteryx R Tool to sign an amazon http request. To do so, I need the hmac function that is included in the digest package.
I'm using a text input tool that includes the key and a datestamp.
Key= "foo"
datastamp= "20120215"
Here's the issue. When I run the following script:
the.data <- read.Alteryx("1", mode="data.frame")
write.Alteryx(base64encode(hmac(the.data$key,the.data$datestamp,algo="sha256",raw = TRUE)),1)
I get an incorrect result when compared to when I run the following:
write.Alteryx(base64encode(hmac("foo","20120215",algo="sha256",raw = TRUE)),1)
The difference being when I hardcode the values for the key and object I get the correct result. But if use the variables from the R data frame I get incorrect output.
Does the data frame alter the data in someway. Has anyone come across this when working with the R Tool in Alteryx.
Thanks for your input.
The issue appears to be that when creating the data frame, your character variables are converted to factors. The way to fix this with the data.frame constructor function is
the.data <- data.frame(Key="foo", datestamp="20120215", stringsAsFactors=FALSE)
I haven't used read.Alteryx but I assume it has a similar way of achieving this.
Alternatively, if your data frame has already been created, you can convert the factors back into character:
write.Alteryx(base64encode(hmac(
as.character(the.data$Key),
as.character(the.data$datestamp),
algo="sha256",raw = TRUE)),1)
I am creating a way to read in SPSS labels into R. Using library(sjPlot), view_spss(df, useViewer = FALSE) I can create a local html page such as http://localhost:11773/session/file1e0c67270a5.html that shows a nice table with columns for the variable names and the labels I am looking for.
Now I want to use rvest to scrape it but when I start with a command such as page <- rvest::html("http://localhost:11773/session/file1e0c67270a5.html") R just seems to get stuck.
I've tried searching for "connect with local host" but I can't seem to find any questions or answers related to the R package.
This doesn't really answer your specific question as I think the reason is that R spins up a non-persistent process to serve that HTML view of your data. But your approach seems quite round-a-bout to just get to variable labels. This is a general way that works quite well:
library(foreign)
d <- read.spss("your_data.sav", use.value.labels=TRUE, to.data.frame=FALSE)
var_labels <- attr(d, "variable.labels")
## To access the label of a variable named 'var_name':
var_labels[["var_name"]]
Where d results in a list of data, and var_labels is a named list of labels keyed by variable/column.
If you want to get variable and/or value label of SPSS-imported data, you can use get_val_labels and get_var_labels of the sjmisc-package.
See examples here. Both functions accept either a single variable (vector) or a data frame and return the associated variable and value labels. See also this blog post.
The sjmisc-Package supports data frames imported both with the haven- or foreign-package.