I want to create GUI using gWidget in R but I am getting all these kind of weird errors:
guitoolkit = new("guiWidgetsToolkittcltk")
Error in getClass(Class, where = topenv(parent.frame())) :
“guiWidgetsToolkittcltk” is not a defined class
or:
> setIs("guiWidget","guiWidgetORgWidgetTcltkORtcltk")
Error in .walkClassGraph(classDef, "subclasses", where) :
the 'subClass' list for class “guiWidgetORgWidgetTcltkORtcltk”, includes an undefined class “guiWidget”
Any idea what I am missing?
Related
I am using rpy2 to import a library from CRAN repository called "MatrixEQTL" to run in within Python using importr, here is my attempt:
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
robjects.r('install.packages("MatrixEQTL")')
mtrql = importr('MatrixEQTL')
I am trying to access both class fields as well as class methods but I failed here is what the class looks like when printing into the python shell:
# to view class SlicedData of MatrixEQTL R package
print(mtrql.SlicedData)
Generator for class "SlicedData":
Class fields:
Name: dataEnv nSlices1 rowNameSlices
Class: environment numeric list
Name: columnNames fileDelimiter fileSkipColumns
Class: character character numeric
Name: fileSkipRows fileSliceSize fileOmitCharacters
Class: numeric numeric character
Class Methods:
"Clear", "show#envRefClass", "nSlices", "getRefClass", "export",
"initialize", "CombineInOneSlice", "callSuper", "initFields", "nCols",
"getClass", "RowStandardizeCentered", "import", "SaveFile", "RowReorder",
"setSlice", "getSlice", "RowReorderSimple", "CreateFromMatrix", "nRows",
"ResliceCombined", "LoadFile", "ColumnSubsample", "SetNanRowMean",
"setSliceRaw", "getSliceRaw", "copy", "RowMatrixMultiply", "usingMethods",
"GetAllRowNames", "RowRemoveZeroEps", "field", ".objectParent",
"IsCombined", "untrace", "trace", "Clone", "GetNRowsInSlice",
".objectPackage", "show", "FindRow"
Reference Superclasses:
"envRefClass"
I want to access for instance class field say fileDelimiter and also class Methods say LoadFile but I couldn't, here is also my attempt to access class method LoadFile and the error message that generated:
# If I try to run this without "()"
data = mtrql.SlicedData
load_my_file = data.LoadFile(file)
data = data.LoadFile(file)
AttributeError: 'DocumentedSTFunction' object has no attribute 'LoadFile'
# And that's my attempt for when adding "()" to the class name
data = mtrql.SlicedData
load_my_file = data.LoadFile(file)
data = data.LoadFile(file)
AttributeError: 'RS4' object has no attribute 'LoadFile'
I tried to look for a solution to this issue but I wasn't successful, please help me understand and solve this issue.
Thank you so much in advance.
mtrql.SlicedData is a "Generator", that is a constructor. In R that means a function (that will return an instance of the class), which is why mtrql.SlicedData is an R function with rpy2.
Consider the following example from the R documentation (https://rdrr.io/r/methods/Introduction.html):
import rpy2.robjects as ro
Pos = ro.r("""
setClass("Pos", slots = c(latitude = "numeric", longitude = "numeric",
altitude = "numeric"))
""")
With rpy2, the object Pos is an R function:
>>> type(Pos)
rpy2.robjects.functions.SignatureTranslatedFunction
However that function object has an S3 class in R (there are several OOP systems in R, 2 of them coexisting in standard R unfortunately).
>>> tuple(Pos.rclass)
('classGeneratorFunction',)
A specific print function is implemented for this S3 class, and it will use the attributes package and className for the object (the R function-that-is-in-fact-a-Generator) to display all the info about the clas you are seeing.
Since this is a constructor, to create an instance you can do:
pos = Pos()
or
pos = Pos(latitude=0, longitude=0, altitude=-10)
The instance will have instance attributes (and methods)
>>> tuple(pos.list_attrs())
('latitude', 'longitude', 'altitude', 'class')
>>> tuple(pos.do_slot('altitude'))
(-10,)
With your specific example, you probably want something like:
slidata = mtrql.SlicedData()
slidata.do_slot('LoadFile')(file)
The documentation can provide more information about R S4 classes in rpy2, and how to make wrapper classes in Python that map attributes and methods.
https://rpy2.github.io/doc/v3.3.x/html/robjects_oop.html#s4-objects
Code looks like this:
library(rvest)
library(stringr)
library(dplyr)
library(ggplot2)
plik_html = read_html("https://en.wikipedia.org/wiki/List_of_countries_by_vehicles_per_capita")
tabela_html = html_node(plik_html, "#mw-content-text > div > table.wikitable.sortable.jquery-tablesorter")
tabela_r = html_table(tabela_html, fill = TRUE)
and I get this error
Error in UseMethod("html_table") : no applicable method for 'html_table' applied to an object of class "xml_missing" >
and I don't know how to fix this. Please help. I'm trying to read first table from website.
I am currently creating a new S4 class which uses a S3 zoo object. I can create a class
setOldClass("zoo")
setClass("rollingSD", slot = c(rollPeriod = "numeric", tsOutput = "zoo"))
This code works fine. Now if I want to create an object as
riskSD <- new("rollingSD")
This also works fine. However, the following generates an error
riskSD <- new("rollingSD", rollPeriod = 12)
Error in validObject(.Object) :
invalid class “rollingSD” object: invalid object for slot "tsOutput" in class
"rollingSD": got class "S4", should be or extend class "zoo"
This not clear for me why a default object of the zoo class is not inititated. I also do not know how to fix this.
The problem is caused because R's class mechanism doesn't know how to make a new zoo object. You can fix this by specifying a "prototype":
setClass(
"rollingSD",
slot = c(rollPeriod = "numeric", tsOutput = "zoo"),
prototype=prototype(
tsOutput=some_zoo_object
)
)
where some_zoo_object is of class zoo. The default prototype for a numeric slot is numeric(), but because you defined the (S4) class zoo yourself, the default is new("zoo") and this isn't defined.
I'm tring to use the soilphysics package and the fitsoilwater funciton.
It works pretty well with simple data sets but fails when I try to use bigger data sets with thousands of records (which is not usual I agree).
this is the error I get, poorly understandable for a beginner
Error in structure(.External(.C_dotTclObjv, objv), class = "tclObj") :
[tcl] floating point value is Not a Number.
so this works well,
ref_h<-c(1,10,22,60,100,200,300,600)
ref_VWC<-c(0.40838,0.3823,0.36928,0.35342,0.3394,0.25122,0.21506,0.1787)
fitsoilwater(ref_VWC, ref_h)
the same principle with longer vectors doesn't work and gives the error I mentionned.
Any comment welcome
You may have an NA in ref_VWC:
> ref_h<-c(1,10,22,60,100,200,300,600)
> ref_VWC<-c(0.40838,0.3823,0.36928,0.35342,0.3394,0.25122,0.21506,0.1787)
> fitsoilwater(ref_VWC, ref_h)
works fine, but then:
> ref_VWC[4]=NA
> fitsoilwater(ref_VWC, ref_h)
Error in structure(.External(.C_dotTclObjv, objv), class = "tclObj") :
[tcl] floating point value is Not a Number.
I am pretty new using R in an advanced way...so apologize for futile questions!
I want to create a object of class S4, defined by 3 slots. Thing is that I can't manage to create these attributes as a list. Here is my code :
test<-setClass("dblist",representation(df.list="list", df.para="list",df.coups="list"))
new("dblist",representation(df.list="list", df.para="list",df.coups="list"))
and the error I get :
Error in initialize(value, ...) : cannot use object of class “list” in new(): class
“dblistpgn” does not extend that class
Could you please explain how creating an object with list plot?
Thx!
try this:
new("dblist", df.list = list(), df.para = list(), df.coups = list())