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)
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
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.
I have a readline() function in a for loop
For Simplicity let's say I have this code:
x <- character()
for (i in 1:500) x[i] <- readline('Enter Value')
How can I automatically enter input instead of manually entering it at the console 500 times?
readline() isn't intended for automatic input. From ?readline:
Description
readline reads a line from the terminal (in interactive use).
and:
Details
The prompt string will be truncated to a maximum allowed length,
normally 256 chars (but can be changed in the source code).
This can only be used in an interactive session.
Looking at ?interactive we can read the following:
An interactive R session is one in which it is assumed that there is a
human operator to interact with, so for example R can prompt for
corrections to incorrect input or ask what to do next or if it is OK
to move to the next plot.
So, basically you are trying to use readline for something it isn't intended for.
You can vectorize this command, assuming you're looking to fill the column with the same value. If that's not what you're trying to do, I'm confused as to what you want. Run my example below and say whether that's the kind of thing you're looking for
working <- iris
head(working)
working$like <- readline("Do you like this flower? ")
head(working)
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 a series of tables and graphs that are produced from a list in R. I would like to create a pdf for each iteration of the list. I have tried simply using pdf() within the function but I get the error that too many graphic devices are open. How can I do this (and name the files according to the list element name?
So far I have tried:
ReportPDF<-function(x){
pdf(paste(name(x),"~\\Myfile.pdf")
tb<-table(x$acolumn)
print(fb)
}
lapply(mylist,ReportPDF)
I cant quite work out how to attach the name of the list element to the filename and I'm not even sure this is the best way to create a pdf from lapply
Can you clean some of this up?
Please give a more specific example of the object you're passing to ReportPDF(), I would expect a plot object, rather than what appears to be a data frame that you are selecting a column from.
The function example has some errors too, did you mean this?
ReportPDF<-function(x){
pdf(paste(names(x),"Myfile.pdf"))
tb<-table(x$acolumn)
print(tb)
dev.off()
}
lapply(mylist,ReportPDF)
I believe I've done something similar before and can update this answer when I get the other information.
Here's an update making some assumptions about your objects. It uses a for loop as lmo suggests, but I think a more elegant method must exist. I'm using the for loop because lapply passes the object within each element of the list, with no reference to name of the element in the list -- which is what you need to name the files individually. Note the difference between calling mylist[i] and mylist[[i]], which is part of what's breaking the code in your example. In your code, names(x) will get the names of the columns within x, rather than the name of x as it is inside of mylist, which is what you want.
x <- data.frame(acolumn = rnorm(10))
q<- data.frame(acolumn = rnorm(10))
mylist <- list(a = x,b = q)
for(i in seq_along(mylist) ){
filename <- paste(names(mylist[i]),'-myFile.pdf', sep = "")
pdf(filename)
plot(myList[[i]]$acolumn)
dev.off()
}