This question is a follow-up to this one I asked a couple of days ago. Following the suggestion from that post, I created a custom data type, and also overloaded the base.show() function. But the output is not what I expected. So I wonder if I misunderstood something.
type Output
testname::String
output::Float64
end
function show(io::IO,object::Output)
println(io,"\tOutput Statistic for $(object.testname)")
println(io,"$(object.output)")
end
I use the following function to illustrate
function MEDIAN(x::Array)
Output("Median: ", median(x))
end
julia> x=randn(10)
julia> MEDIAN(x)
Output("Median: ",0.2267306855631679)
I want the output to be the following instead:
Median: 0.2267306855631679
To answer the question, you probably forgot to import Base.show
Related
The result variable is a json-type string, which is very long. What is the option in Julia REPL that allows only a limited output when the variable is this long? DataFrame is originally only partially output. I hope that the general variables will also be output like that.
You can overwrite the display method for AbstractStrings:
import Main.display
display(x::AbstractString) =
show(length(x)<=50 ? x : SubString(x,1,50)*"…")
Let us test it:
julia> str = join(rand('a':'z', 200))
"wcbifwzglgqyenrcdgdxagohlwdoxrrumoaltklkjauptwzrmi…"
Suppose I want to create a method for a class I've created, but I don't have access to the code of the original function - I just want to build on top of it. Just to give a simple example that doesn't actually do anything:
x1<-1
class(x1)<-c("myclass",class(x1))
print.myclass<-function(x) {
x<-paste0(x,"foobar")
print(x)
}
print(x1)
If I try to run the last line, it throws the function into a loop and R eventually crashes. The solution I found was to add a line to the function that strips the new class name from x before passing it to the original function:
print.myclass<-function(x) {x<-paste0(x,"foobar"); class(x)<-class(x)[-1]; print(x)}
Is there a better/best practice way to do it?
I think your problem is that you create an infinite loop: print(print(...).
I don't know what you want to achieve but this might be what you are looking for:
x1 <- 1
class(x1) <- c("myclass",class(x1))
print.myclass <- function(x) print.default(x)
print(x1)
Perhaps you might want to look here
BTW: I don't think your solution really solves the problem. You just delete your new class entry which causes print not to use print.myclass.
For details see Hadley
I want to print every argument passed to function read.table. My idea was to write some decorator that is easy in Python. But for R, I don't know how to do it, what I learned was to use trace(). However, I don't know how to print variables inside trace.
Example:
trace(f)
a <- "123"
f(a)
untrace(f)
trace() will only output f(a), but I want to know the evaluation of a.
thanks for your guys' help, I find the answer.
Simply use the following code:
trace(f, tracer = quote(print(lapply(as.list(match.call()),eval))))
d<-1
f(d)
untrace(f)
I'm working on a non-linear optimization, with the constrOptim.nl from the alabama package. However, my problem is more related to passing arguments (and the dot-dot-dot (ellipis/"...") and maybe do.call)- so I give first a general example and later refer to the constrOptim.nl function.
Suppose, I have the following functions - from which I only can edit the second and third but not the first.
first<-function (abc, second, third, ...){
second(abc,...)
third(abc,...)
}
second<- function(abc, ttt='nothing special'){
print(abc)
print(ttt)
}
third<- function(abc, zzz="default"){
print(abc)
print(zzz)
}
The output I want is the same I would get when I just run
second("test", ttt='something special')
third("test", zzz="non-default")
This is
"test"
"something special"
"test"
"non-default"
However, the code below doesn't work to do this.
first("test",second=second, third=third, ttt='something special',zzz="non-default")
How can I change the call or the second and third function to make it work?
http://www.r-bloggers.com/r-three-dots-ellipsis/
here I found some advice that do.call could help me but at the moment I'm not capable of understanding how it should work.
I cannot change the first function since this is the constrOptim.nl in my particular problem - and it is designed to be capable of passing more arguments to different functions. However, I can change the second and third function - as they are the restrictions and the function that I want to minimize. Obviously I can also change the call of the function.
So to be more particular, here is my specific problem:
I perform a maximum likelihood estimation with non-linear restrictions:
minimize <- function(Param,VARresiduals){
#Blahblah
for (index in 1:nrow(VARreisduals)){
#Likelihood Blahbla
}
return(LogL)
}
heq<-function(Param,W){
B<-Param[1:16]
restriction[1]<-Lrestriction%*%(diag(4)%x%(solve(W))%*%as.vector(B))
restriction[2:6]<-#BlablaMoreRestrictions
return(restriction)
}
Now I call the constrOptim.nl...
constrOptim.nl(par=rnorm(20), fn=minimize,hin=NULL heq=heq,VARresiduals,W)
...but get the same error, as I receive when I call the first function above - something like: "Error in second(abc, ...) : unused argument (zzz = "non-default")".
How can I change minimize and heq or the call? :) Thanks in Advance
Update after the post got marked as a duplicate:
The answer to the related post changes the first function in my example - as it implements a do.call there, that calls the other functions. However, I cannot change the first function in my example as I want to keep the constrOptim.nl working a variety of different functions. Is there another way?
The solution I came up with is not very elegant but it works.
second_2<- function(abc, extras){
a<-extras[[1]]
print(abc)
print(a)
}
third_2<- function(abc, extras){
a<-extras[[2]]
print(abc)
print(a)
}
extras<-list()
extras[[1]]<-'something special'
extras[[2]]<-"non-default"
extras
first("test",second=second_2, third=third_2, extras)
surprisingly also the following code works, but with a slightly different outcome
first("test",second=second, third=third, extras)
after all, setting default values is now a little clumsy but not infeasible.
I'm trying to package some code I use for data analysis so that other workers can use it. Currently, I'm stuck trying to write a simple function that imports data from a specific file type generated by a datalogger and trims it for use by other functions. Here's the code:
import<-function(filename,type="campbell",nprobes){
if (filename==TRUE){
if (type=="campbell"){
message("File import type is from Campbell CR1000")
flux.data<<-read.table(filename,sep=",",header=T,skip=1)
flux.data<<-flux.data[,-c(1,2)];flux.data<<-flux.data[-c(1,2),]
if (nprobes=="missing"){
nprobes<-32
}
flux.data<<-flux.data[,c(1:nprobes)]
flux.data.names<<-colnames(flux.data) #Saves column names
}
}
}
Ideally, the result would be a dataframe/matrix flux.data and a concomittant vector/list of the preserved column headers flux.data.names. The code runs and the function executes without errors, but the outputs aren't preserved. I usually use <<- to get around the function enclosure but its not working in this case - any suggestions?
I think the real problem is that I don't quite understand how enclosures work, despite a lot of reading... should I be using environment to assign environments within the function?
User joran answered my question in the comments above:
The critical issue was just in how the function was written: the conditional at the start (if filename==TRUE) was intended to see if filename was specified, and instead was checking to see if it literally equaled TRUE. The result was the conditional never being met, and no function output. Here's what fixed it:
import<-function(filename,type="campbell",nprobes){
if (exists(filename){
if (type=="campbell"){
#etc....
Another cool thing he pointed out was that I didn't need the <<- operator to utilize the function output and instead could write return(flux.data). This is a much more flexible approach, and helped me understand function enclosures a lot better.