Macros don't seem to work?


#1

I’m new to Julia and trying to use some macros of Query.jl. I’m running Julia v0.6 in Juno on Linux Mint Cinnamon 18.1, 64bit. Macros don’t seem to work, I always get errors e.g.:

A=collect(1:10) '@where !=5 MethodError: no method matching @where(::Expr)

It makes no difference, which macro I’m using, they all throw this error. Did anybody ever encounter that before? I would guess, it’S something to do with my installation, but I don’t know, how to find that out. I already tried Pkg.update() and no packages need to be updated…
Thanks!


#2

I don’t believe @where is a macro. It’s not listed in https://github.com/JuliaLang/julia/blob/master/base/exports.jl Do you mean @which?

julia> @which 4!=5
!=(x, y) in Base at operators.jl:129

julia> @where 4!=5
ERROR: UndefVarError: @where not defined

julia-0.6.0 generic binary, running on opensuse tumbleweed.


#3

@where is defined in Query.jl. It is documented here: http://www.david-anthoff.com/Query.jl/stable/querycommands.html#Filtering-1. It never uses the syntax in your post. Also, I think you should apply it to sources like dataframes and not to a vector.

Could you say more clearly what you actually want to do?


#4

Thanks, but I#m not so sure, whether it’s a syntactic problem, because I get the same error, when trying @from (from the Query) package. I’m actually trying to do pretty much the same as in the example given there. The data I’m trying to use this on is also a DataFrame, however I only want to get certain values out if the first column, so it might be treated like a vector? I’m not so sure, how htis is in Julia. in R, I would use which().


#5

Can you give a minimal example, using a dataframe, the code that you execute on that dataframe, what result you expect to get, and the errors printed?


#6

Thanks! I tried @which and it works! So my guess is, the installation of the Query package didn’t work properly. I used Pkg.add() and then updated it with Pkg.update(). Is this the correct way or is there a difference between installing packages in the REPL and in Juno?


#7

Of course!

using Distributions
using DataFrames
using Query
Adult_h_ind=DataFrame(Float64, 100, 100)
Adult_h_ind[1:20,1]=rand(Normal(10, 2), 20)
NotNA= Query.@from i in Adult_h_ind[1] begin
Query.@where Adult_h_ind[1] != NA
end

edit to add the error message:

MethodError: no method matching @from(::Expr)
Closest candidates are:
@from(::Expr, !Matched::Expr) at /home/gast/.julia/v0.6/Query/src/Query.jl:47
include_string(::String, ::String) at loading.jl:515
eval(::Module, ::Any) at boot.jl:235
(::Atom.##61#64)() at eval.jl:102
withpath(::Atom.##61#64, ::Void) at utils.jl:30
withpath(::Function, ::Void) at eval.jl:38
macro expansion at eval.jl:101 [inlined]
(::Atom.##60#63{Dict{String,Any}})() at task.jl:80

I also tried shorter syntax, both in Juno and the REPL, with only one of the macros and I always got the method error… The other packages (like Distributions) work fine…


#8

Glad to hear. That’s the stantard incantation from the REPL. I don’t use Juno so can’t say with authority, but I’d be surprised if it were different there.

julia> Pkg.add("Query")
julia> Pkg.update()
julia> using Query
julia> @which 4!=3
!=(x, y) in Base at operators.jl:129

#9

Short question on the side: when using @which the result is something like !=(x,y) in Base at operators.jl:129. How do I get either the actual values or the indices of that parts of arrays that match the question?
e.g. A= collect(1:10), @which A != 5
and the answer I’m searching for is something like 1,2,3,4,6,7,8,9,10?
Thanks!


#10

I don’t think you want @which.

help?> @which
  @which

  Applied to a function or macro call, it evaluates the arguments to the
  specified call, and returns the Method object for the method that would be
  called for those arguments. Applied to a variable, it returns the module in
  which the variable was bound. It calls out to the which function.

For an array you can use find

julia> find(x -> x != 5, A)
9-element Array{Int64,1}:
  1
  2
  3
  4
  6
  7
  8
  9
 10

#11

Thanks! I’ll try to use find. I don’t know, what’s up with my Query package, but I think, with your help, i can work around it, hopefully…
Thanks a lot!


#12

Please quote your code.

From the doc @where is not a standalone macro so you must not use it like that.


#13

I showed my quote in the above answer. If I understand you correctly, @select and @collect are necessary to use the @where macro correctly? Or is it less than that? Where do I generally find information such as that, that there a requirements for macros, except for the examples? The error messages are not really that helpful in this… Thanks!


#14

Error messages on macro based API are typically quite unhelpful unless the author of them does careful syntax checking. You need to just read the docs for macros and look at examples.


#15

Thanks! I’ll keep that in mind!


#16

Quoting your code: Whenever you include code in your posts, put it between double or triple back single quotes so that it’s formatted nicely, e.g.

julia> @which 4+5
+(x::T, y::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at int.jl:32

About @where, reread @kristoffer.carlsson’s post. It looks like you want to use find, not @where


#17

I mean quote the code you quote with ```. It’s very hard to read if you compare to what other people post.

yes, according to the doc


#18

Thanks! I wasn’t sure how the formatting works here! Now I know! For my purposes find() will do fine! Thanks @kristoffer.carlsson and @John_Gibson! Everything else was a great learning opportunity for me, thanks again!