Wednesday, November 28, 2018

r - Error calculating mean by input variable in Shiny

Using the mtcars dataset in R, I am tying to use the input variable (cyl, am etc...) to calculate the mean mpg by that variable.



My code in the ui.R is like:



verbatimTextOutput("Avg_Mileage")



My code in the shinServer function in server.R is like:



carsdata <- mtcars

output$Avg_Mileage <- renderPrint({aggregate(mpg~input$variable, carsdata,mean)})


I have tried to change the code in server.R in various ways. But I keep getting the message about conflicting variable lengths: "variable lengths differ (found for 'input$variable')"



I would appreciate any help in trying to see how can this mean mpg by input variable be implemented in Shiny

How to retrieve GET parameters from javascript?





http://domain.com/page.html?returnurl=%2Fadmin


For js within page.html,how can it retrieve GET parameters?



For the above simple example,func('returnurl') should be /admin



But it should also work for complex querystrngs...


Answer



With the window.location object. This code gives you GET without the question mark.




window.location.search.substr(1)


From your example it will return returnurl=%2Fadmin



EDIT: I took the liberty of changing Qwerty's answer, which is really good, and as he pointed I followed exactly what the OP asked:



function findGetParameter(parameterName) {
var result = null,

tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function (item) {
tmp = item.split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
});
return result;
}



I removed the duplicated function execution from his code, replacing it a variable ( tmp ) and also I've added decodeURIComponent, exactly as OP asked. I'm not sure if this may or may not be a security issue.



Or otherwise with plain for loop, which will work even in IE8:



function findGetParameter(parameterName) {
var result = null,
tmp = [];
var items = location.search.substr(1).split("&");

for (var index = 0; index < items.length; index++) {
tmp = items[index].split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
}
return result;
}

objective c - string has prefix PHP











How make an if-statement that if $mystring has the prefix "http://" then {do something}.



I've done this in objective-c like this:



if([mynsstring hasPrefix:@"http://"])

{
//Do something...
}


I don't know how to do this in PHP.
Thanks for your help in advance.


Answer



Simplest would be using substring to compare




if (substr($mystring, 0, 7) === 'http://') {
// do something
}


Remember of course to take the exact number of characters.


java - The Use of Multiple JFrames: Good or Bad Practice?




I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide whether or not to use a separate JFrame to add images to the database from the GUI.



I'm just wondering whether it is good practice to use multiple JFrame windows?


Answer





I'm just wondering whether it is good practice to use multiple JFrames?




Bad (bad, bad) practice.




  • User unfriendly: The user sees multiple icons in their task bar when expecting to see only one. Plus the side effects of the coding problems..

  • A nightmare to code and maintain:



    • A modal dialog offers the easy opportunity to focus attention on the content of that dialog - choose/fix/cancel this, then proceed. Multiple frames do not.

    • A dialog (or floating tool-bar) with a parent will come to front when the parent is clicked on - you'd have to implement that in frames if that was the desired behavior.







There are any number of ways of displaying many elements in one GUI, e.g.:





  • CardLayout (short demo.). Good for:


    1. Showing wizard like dialogs.

    2. Displaying list, tree etc. selections for items that have an associated component.

    3. Flipping between no component and visible component.


  • JInternalFrame/JDesktopPane typically used for an MDI.

  • JTabbedPane for groups of components.


  • JSplitPane A way to display two components of which the importance between one or the other (the size) varies according to what the user is doing.

  • JLayeredPane far many well ..layered components.

  • JToolBar typically contains groups of actions or controls. Can be dragged around the GUI, or off it entirely according to user need. As mentioned above, will minimize/restore according to the parent doing so.

  • As items in a JList (simple example below).

  • As nodes in a JTree.

  • Nested layouts.



But if those strategies do not work for a particular use-case, try the following. Establish a single main JFrame, then have JDialog or JOptionPane instances appear for the rest of the free-floating elements, using the frame as the parent for the dialogs.




Many images



In this case where the multiple elements are images, it would be better to use either of the following instead:




  1. A single JLabel (centered in a scroll pane) to display whichever image the user is interested in at that moment. As seen in ImageViewer.

  2. A single row JList. As seen in this answer. The 'single row' part of that only works if they are all the same dimensions. Alternately, if you are prepared to scale the images on the fly, and they are all the same aspect ratio (e.g. 4:3 or 16:9).





python - How can I round up the number of periodic digits?




I have two numbers, one I get by calculating it and the other one I bring it from the database.



calculated = 2.183333333333333
database = 2.18333333333333



But when I compare them to know if they are the same, I return False when it should be True.



There is some way to limit the number of periodic numbers, but not to affect decimals that are not periodic, for example:



2.1748888888888 -> 2.1748
1.23333333 -> 1.23

Answer




You could use the math.isclose method:



>>> from math import isclose
>>> calculated = 2.183333333333333
>>> database = 2.18333333333333
>>> isclose(calculated, database)
True


This allows for setting the relative tolerance and minimum absolute tolerance as well refer to the docs for more explanation.



excel - copy data from each cell in range loop and paste it on another sheet




I have below vba code
Sub Macro1()



    Dim FLrange As Range
Set FLrange = Range("C3:E3")

For Each cell In FLrange
If cell.Value = 0 Then cell.FormulaR1C1 = "=SUMIFS(raw!C4,raw!C1,R[0]C1,raw!C3,R2C[0])"


Next cell

End Sub


Now, before executing the formula in above code, i would like to add another function on this. I need to copy the current cell value to another sheet on exactly same cell. ie, value from sheet1 cell C3 has to be pasted sheet2 cell C3



I have tried with



Sub Macro1()


Dim FLrange As Range
Set FLrange = Range("C3:E3")

For Each cell In FLrange
Selection.Copy
Sheets("sheet2").Paste
If cell.Value = 0 Then cell.FormulaR1C1 = "=SUMIFS(raw!C4,raw!C1,R[0]C1,raw!C3,R2C[0])"

Next cell


End Sub


But this one not pasting the value corresponding cell in sheet2, but see it is pasting formula to random cell.



How can I paste the value from each cell in range in current sheet (sheet1), to corresponding cell in another sheet shet2


Answer



First off, you're using Selection in your code, but you're never changing your ActiveCell. Regardless, using ActiveCell is bad practice, you should always use object references to copy and not your selection unless it's completely necessary.




Secondly you never give it an actual range to paste to, you just give it the sheet, so I'm surprised this isn't throwing an error of some sort when you try to run it.



What my code does it sets the range you want to iterate on (to use Me you need to place this in the Sheet Module of Sheet1, otherwise you need to explicitly say it's on Sheet1), iterates over it and then copies it to the same Col/Row index on a second explicitly defined sheet.



When it comes to best practices you should always explicitly define things, especially when you're talking about VBA.



Option Explicit
Sub Test()

Dim rng As Range

Dim c As Range
Dim dest As Worksheet


Set rng = Me.Range("A1:A10")
Set dest = ThisWorkbook.Worksheets("Sheet2") '' rename this to whatever sheet you want this to copy to

For Each c In rng
c.Copy dest.Cells(c.Row, c.Column)


If c.Value = 0 Then
c.FormulaR1C1 = "=SUMIFS(raw!C4,raw!C1,R[0]C1,raw!C3,R2C[0])"
End If
Next c

End Sub

Tuesday, November 27, 2018

javascript, wait for something to be true then run action



Well the title kindof says what I need. Because in Javascript timeouts asynchronous I need to know when something becomes true. I don't want busyloop.




Came up with:



function do_when(predicate, action, timeout_step) {
if (predicate()) {
action();
} else {
setTimeout(do_when, timeout_step, predicate, action, timeout_step);
}
}



Is it good Javascript or can I make better?


Answer



It's decent enough, if it's easy enough to read and it works just fine then it's generally good javascript.



Performance-wise, it's generally better to call the function whenever whatever is set to true happens. So in whatever function that executes to make predicate() return true, you could just call action() at the end. But I'm sure that's what you would have done if you could, right?



You could also look at using a callback, where you register a javascript function to a particular variable or function argument and when the function is run it executes whatever function was set to the callback variable.


plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...