I am new to R and here. Sorry If I am complicated. I created a function by trial name. There are some functions from my own function below.
write.table(pars, "C:\\Parameters\\pars_(variable name).txt")
cov12lag1_(variable name) <<- acvf(half, 1)
Let's say I have two different data. Data names are jan2005 and jan2006
When I write function trial(jan2005) I want to get pars_jan2005.txt and also in my environment I want to see cov12lag1_jan2005 automatically.
Also, again when I want to write trial(jan2006), again I want to see pars_jan2006.txt and cov12lag1_jan2006 automatically.
So these were only examples, my aim is when I write a variable in a function, I want all desired parts to be changed with variable name.
Related
I am using a summary() function from an R package. It prints out multiple results by using cat() and print() functions interweavingly (I checked its source code by getAnywhere()). It uses cat() to output descriptive messages, and uses print() to print out the dataframe and the matrix that I wanted to save. I am using R notebook, so I can see the cat() output and matrix output in the R Console tab, and the dataframe output by itself in another result tab. I wasn't able to save or retrieve the dataframe.
I tried sink() and capture.output() functions, but it only saved the descriptive messages and matrix together in a text file, and no dataframe saving. Does anyone know how to retrieve the dataframe object by itself? And I may also need to retrieve the matrix object by itself too.
The R package is called "lmmlasso". I guess it's not very well maintained. Here I am just giving an example code:
summary.foo= function(){
cat("random effects\n")
print(matrix(rnorm(9),3))
cat("fixed effects \n")
print(data.frame(X = c("A","B","C"), Estimate = rnorm(3)))
}
summary.foo()
I was not able to solve it directly, so I turned to the alternative way: I copied the original source code, return the dataframe and matrix objects at the end, and changed its function name. I got the dataframe by using the new summary function.
You should provide a reproducible code.
Anyway, try to assign it to a variable and then use the variable attributes, either with $ or #, to access the output you desire
Suppose that after some calculations I obtain a (floating) number, stored in the variable a, for example
a <- sqrt(2)
Now, I want to define a function that uses that parameter, for example:
myfunction <- function(x){x-a}
How can I save myfunction into an RDS file, in such a way that it can be loaded and used in a new R session, where the variable a is not defined?
Or from a different perspective: How to define the function, but substituting for a its actual numerical value in the function definition? That is, I'd like R to actually define the function
myfunction <- function(x){x - 2.1415.....}
where the actual value of a has been substituted in the definition.
Simply trying saveRDS(myfunction, 'myfunction.rds') does not work: if I start a new R session and do
myfunction <- readRDS('myfunction.rds')
myfunction(1)
then R complains that a is not defined.
Please note that I'm here giving a minimal working example of the problem. Obviously, in the case above I could just define myfunction <- function(x){x-sqrt(2)} and save that in an RDS file; it could be loaded in a new session and used without problems.
However, in my case I have many parameters like a, not just one, obtained from long calculations. I'm not interested in saving their values, I only want to save the function that uses them in its definition, and be able to use that function in a new R session.
An RDS file won't save the global environment, but if you create a closure, it will preserve the values in that environment. One such way to do what would be
myfunction <- {function(a) function(x){x-a}}(a)
And then you can call this function like a regular function
myfunction(1)
I have a repetitive region of R-script that manipulates matrices that I would like to simplify with a custom function. I can generate the custom function just fine, but I would like the output of the function to be a matrix in the global environment with a name based on the name of the input matrix.
So the code goes something like this:
CustomFunction <- function (input) {
input_mod <- input *2
}
(Obviously I have simplified the function here as the actual steps in the function don't matter for the question.)
But what I would like is for the output to be a matrix in the global environment with a name based on the input name. Then if I run the custom function three times on three different input matrices: input1, input2 and input3, the output from the custom function should be three matrices in the global environment named input1_mod, input2_mod and input3_mod.
My current abilities in R only lead me to be able to generate an output that always has the same name and I want to be able to run the function many times and store each of the output matrices for subsequent further manipulations.
Thanks for looking and hope you can help!
Have a look at the assign function, ex.
assign("Global.res", x^2, envir = .GlobalEnv)
I can't figure out why this version of plyr's rename function isn't working.
I have a dataframe where I have a single column that ends up being named seq(var_slcut_trucknumber_min, var_slcut_trucknumber_max) because I made it like this:
df_metbal_slcut <- as.data.frame(seq(var_slcut_trucknumber_min,var_slcut_trucknumber_max))
The terms var_slcut_trucknumber_min and var_slcut_trucknumber_max are defined as the min and max of another column.
However, when trying to rename it by the following code,
var_temp <- names(df_metbal_slcut)
df_metbal_slcut <- rename(df_metbal_slcut, c(var_temp="trucknumber"))
I get an error as follows:
The following `from` values were not present in `x`: var_temp
I don't understand why. I know that I can easily do this as colnames(df_metbal_slcut)[1] <- "trucknumber", but I'm an R n00b, and I was looking at a data manipulation tutorial that said that learning plyr was the way to go, so here I am stuck on this.
Try this instead:
df_metbal_slcut <- rename(df_metbal_slcut, setNames("trucknumber",var_temp))
The reason it wasn't working was that c(var_temp = "trucknumber") creates a named vector with the name var_temp, which is not what you were intending. When creating named objects using the tag = value syntax, R won't evaluate variables. It assumes that you literally want the name to be var_temp.
More broadly, it might make sense to name the column more sensibly when initially creating the data frame again using setNames.
I want to see the contents of a dataframe that is created inside a function after the dataframe gets created.
Let's say the dataframe is called df in the function. If I just include a line of code like this:
df
I don't get any display to the console. I would get the dump of the dataframe contents outside a function.
Thanks
In an interactive session, when you type the name of an object, like df, R implicitly prints() the object using an appropriate method. It is as if you'd typed print(df), without you actually having to type all those extra characters.
However, there are a few places that this automatic printing is not active
As you found out, this is not active within functions,
Within loops, such a for, while etc.,
When you source a script.
In such cases, you need to explicitly print() an object.
This often catches people out with lattice and ggplot2 plots, which need to be print()ed to get them drawn on the device. but in general use, the user never has to explicitly print() these plots to get something drawn.
You're expecting it to print out because when you type the name of an object into R's shell, you have the representation of the object helpfully echoed back to you. Inside a function, this doesn't happen unless you explicitly tell R to print the information.
If print(df) doesn't work, look for buffered output and be sure it's turned off.
When you type a variable name in the console and hit enter, R silently passes that variable to print. In order to get the same effect from inside a function, you need to explicitly print. In your case:
print(df)
df <- data.frame(x = rpois(100,80))
print_me <- function(x){
print(x)
}
print_me(df)