I have a list of girafe objects, created using girafe() from the package ggiraph. These are essentially interactive ggplot objects.
I would like to loop over the list of girafe objects and print these plots in an rmarkdown file.
Using results = "asis" in the code chunk, my attempts have been along the lines of:
if ("chart.ls" %in% names(current)) {
for (i in seq_along(current$chart.ls)){
knitr::knit_print(current$chart.ls[[i]])
}
}
Further, the below code will print the plot as expected
if ("chart.ls" %in% names(current)) {
knitr::knit_print(current$chart.ls[[2]])
}
I understand there are alternative ways to print graphs from inside a loop, but these methods don't seem to work for interactive graphs.
Is there a way to print girafe objects from inside a loop, without using external packages? (e.g. knitrContainer)
I have a workaround, which is to use child Rmds. Where I can loop over the object, then reference a child.rmd which then prints it. But hoping for a real solution.
Related
I have a 2D list of structs that must be sequentially passed into a series of loops. Each loop will change the function in a way that affects the output from the following loop. All elements must have completed the first loop before the second loop can be started.
Currently, I have a system that looks like this:
self.list.iter().flatten().for_each(|e| { e.funcOne() });
self.list.iter().flatten().for_each(|e| { e.funcTwo() });
.....
While this does work, it is not elegant - it also requires the 2d list to be repeatedly flattened. One solution could be to store the self.list.iter().flatten() into its own variable, however, this would require the cloning of said variable each time I wanted to loop through it.
My Ideal solution would include something syntactically similar to this:
self.list.iter().flatten().for_each(|e| e.funcOne()).for_each(|e| e.funcTwo()) ....... ;
Performance-wise flattening is fine as #Chayim Friedman mentioned.
If you take a look at the flatten() source, it is not really converting the 2D-array into 1D-array. What it does is it lazily iterates over one row via a row iterator, and when the row exhausts, it switches to iterate over the next row iterator, and so on until there's no next row.
If you want a functional (one line) solution, consider this:
let functions = [
ElemType::funcOne,
ElemType::funcTwo,
];
functions.iter().for_each(|f| self.list.iter().flatten().for_each(f));
Possibly very stupid question I cannot seem to find an answer for (I am beginning with code)
I want to create a simple loop which appends myArray with three objects, which are members of a custom class MyClass. The objects have the following names: "object1", "object2", "object3".
When I write the following code, there is no issue:
myArray.append(object1)
But I want to write a loop to add all three. Again, very dumb, but I can't figure out how to insert the number in the name of the object as a variable. E.g., here was something I tried
let x = 3
for i in 1...x {
myArray.append(object[i])
}
This gives an error. The reason I want to do it using a loop, and not simply write in the three objects manually, is that I won't always loop three times. Sometimes I'll just want the first two objects, sometimes just the first.
I assume there's some easy way to do this, but when I search it tends to turn up more complex questions
I have a 3d array with lots of pages (or whatever you call 3rd dimension). I am trying to stick that array into a loop, and loop through the pages to fill in values.
In my real dataset, I'm manipulating values with a handful of custom functions, so the way it works best to minimize the amount of retyping I would need to do when changing variables is to create a new array inside the loop, make changes to that new array, then replace the new page in its spot in the original array. However, I'm having a hard time plugging the new page back into the original array. Example follows.
code="A" # I'll be running this loop with various name codes
assign(paste0("testarray",code),array(dim=c(5,5,20)))
# creates array "testarrayA" . I'm using assign(paste()) because I will
# do this with many different array codes, and I want to only change
# the code value each time for ease
set.seed(5)
testarrayA[,,1] <- runif(25,3,20) #fill first page
Then I want to run this array through a loop to change the values on each page. In the real code, the changes are made with somewhat complex custom functions, so again, for ease, I start by making a smaller array, changing that, then trying to plug back into the array from above. See following.
# this loop doesn't work at the moment, so just run lines
# inside the loop individually to attempt. set "page" object as 1
for(page in 1:dim(get(paste0("testarray",code)))[3] ){
# for pages 1-end of third dimension (20)
temparray <- get(paste0("testarray",code))[,,page:(page+1)]
# create smaller array of only current and (blank) next page of original array
temparray[,,2] <- temparray[,,1]*2
# do functions to change the values on the next page of temporary array.
# (just multiplying values by 2 for this example)
# try to plug in temporary array[2] as original array[page+1].
# both of these codes give me errors at the moment, but these are
# the options I have tried so far.
# trial 1:
get(paste0("testarray",code))[,,page+1] <- temparray[,,2]
# trial 2:
assign(paste0("testarray",code)[,,page+1],temparray[,,2])
}
I believe the problem comes from not being able to use paste() in the receiving end of an assign command. The first trial listed above returns error "target of assignment expands to non-language object," which I'm not quite sure how to interpret, and the second, "incorrect number of dimensions," which shouldn't be the case. When I highlight and run each object individually, they have the same dimensions, so I don't know what I'm doing wrong.
Basically, I need to know how to use paste() on the receiving end of an assign function such as <- or assign(), or if there's a reasonable workaround for the errors I'm getting. I know I could just omit the creation of the smaller temporary array, but in my actual code, that would make a lot more work by requiring me to change names a bunch of times in each command inside the loop when I run on a new dataset. I'd rather just change the "code" if possible. Thanks!
I have looked Pytorch source code of MNIST dataset but it seems to read numpy array directly from binaries.
How can I just create train_data and train_labels like it? I have already prepared images and txt with labels.
I have learned how to read image and label and write get_item and len, what really confused me is how to make train_data and train_labels, which is torch.Tensor. I tried to arrange them into python lists and convert to torch.Tensor but failed:
for index in range(0,len(self.files)):
fn, label = self.files[index]
img = self.loader(fn)
if self.transform is not None:
img = self.transform(img)
train_data.append(img)
self.train_data = torch.tensor(train_data)
ValueError: only one element tensors can be converted to Python scalars
There are two ways to go. First, the manual. Torchvision.datasets states the following:
datasets are subclasses of torch.utils.data.Dataset i.e, they have __getitem__ and __len__ methods implemented. Hence, they can all be passed to a torch.utils.data.DataLoader which can load multiple samples parallelly using torch.multiprocessing workers.
So you can just implement your own class which scans for all the images and labels, keeps a list of their paths (so that you don't have to keep them in RAM) and has the __getitem__ method which given index i reads the i-th file, its label and returns them. This minimal interface is enough to work with the parallel dataloader in torch.utils.data.
Secondly, if your data directory can be rearranged into either structure, you can use DatasetFolder and ImageFolder pre-built loaders. This will save you some coding and automatically provide support for data augumentation routines from torchvision.transforms.
Problem statement: We need a way to pass a two dimensional list (or array) from one groovy script to other scripts ( to assert values from multiple DB2 tables in other scripts].
Some Background:
Step1: Based on our input xml payload we are capturing the list of nodes (and child elements) in a two dimensional list [][]. [Done]
Step2: Now we want to use the values from each of this list to assert with respect to values in DB2 tables [Also done, however keeping both step1 and step2 in same groovy script].
What we want is to to be able to pass the 2dimensional list from step1 in step2. Specially important since we have multiple tables and we dont want to either add all table steps in one big groovy script Or to duplicate step1 code in each Db2 validataion script.
We read about setting each element value from list at test case level and then reconstructing the array back but we are hesitating to use that method due to (varying &) huge size of list elements (in thousands). Question is: Are there any clean ways to achieve this?
Thanks!
As you are aware of the limitation of the earlier solution, which would only work (sharing of object between the groovy scripts) if the test case is run and does not work if individual steps are run.
Here I want to provide an approach which over comes that by using groovy's meta programming.
In script 1, have the below code:
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase
WsdlTestCase.metaClass.myList = [1,2,3,4,5]
In script 2, have the below code:
log.info "From script 2: ${context.testCase.myList}"
assert [1,2,3,4,5] == context.testCase.myList
The above even works if individual steps are run.
Hope this is helpful.
EDIT: come to understand that user required to update the list repeatedly and with metaClass user couldn't update the list. Here is the alternative:
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase
WsdlTestCase.metaClass.myObject = new Expando(myList: [1,2,3,4,5])
log.info "list initialized: ${context.testCase.myObject.myList}"
WsdlTestCase.metaClass.myObject = new Expando(myList: [1,2,3,4,5,6,7])
log.info "list updated: ${context.testCase.myObject.myList}"
You can use context
That's real working Groovy Script steps.
step1:
def array = ['Kyiv', 'Boryspil', 'Kharkiv', "L'Viv", "Odesa"]
context.setProperty('cities', array)
log.info( 'script1: '+array )
step2:
def array = context.getProperty('cities')
log.info( 'script2: '+array )
assert array.size()>0
Note:
If you run just one step, then there will be absolutely
independent context.
But if you run the whole testcase then there will be context shared
for the whole testcase.
You can use Run from here context menu in your test case window to run from exact step.