Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

There are a few methods to get metadata about a table:
***
EXEC sp_help tablename
***
Will return several result sets, describing the table, it's columns and constraints.

The INFORMATION_SCHEMA views will give you the information you want, though unfortunately you have to query the views and join them manually.
4 years ago
I would use dplyr for this, makes it super simple. It does require an id variable in your data set, which is a good idea anyway, not only for creating sets but also for traceability during your project. Add it if doesn't contain already.
***
mtcars$id <- 1:nrow(mtcars)
train <- mtcars %>% dplyr::sample_frac(.75)
test <- dplyr::anti_join(mtcars, train, by = 'id')
***
4 years ago
sqldf provides a nice solution
***
a1 <- data.frame(a = 1:5, b=letters[1:5])
a2 <- data.frame(a = 1:3, b=letters[1:3])
***
require(sqldf)
***
a1NotIna2 <- sqldf('SELECT * FROM a1 EXCEPT SELECT * FROM a2')
***
And the rows which are in both data frames:
***
a1Ina2 <- sqldf('SELECT * FROM a1 INTERSECT SELECT * FROM a2')
***
The new version of dplyr has a function, anti_join, for exactly these kinds of comparisons
***
require(dplyr)
anti_join(a1,a2)
***
And semi_join to filter rows in a1 that are also in a2
4 years ago
While the results vary in this case because the column names are numbers, another way I've used is data.frame(rbind(mytable)). Using the example from @X.X:
***
> freq_t = table(cyl = mtcars$cyl, gear = mtcars$gear)

> freq_t
gear
cyl 3 4 5
4 1 8 2
6 2 4 1
8 12 0 2

> data.frame(rbind(freq_t))
X3 X4 X5
4 1 8 2
6 2 4 1
8 12 0 2
If the column names do not start with numbers, the X won't get added to the front of them.
***
4 years ago
Just for the sake of completeness, we can use the operators [ and [[:
***
set.seed(1)
df <- data.frame(v1 = runif(10), v2 = letters[1:10])
Several options

df[df[1] < 0.5 | df[2] == "g", ]
df[df[[1]] < 0.5 | df[[2]] == "g", ]
df[df["v1"] < 0.5 | df["v2"] == "g", ]
df$name is equivalent to df[["name", exact = FALSE]]
***
Using dplyr:
***
library(dplyr)
filter(df, v1 < 0.5 | v2 == "g")
***
Using sqldf:
***
library(sqldf)
sqldf('SELECT *
FROM df
WHERE v1 < 0.5 OR v2 = "g"')
***
4 years ago
Easy Way To Delete Item From state array in react:

when any data delete from database and update list without API calling that time you pass deleted id to this function and this function remove deleted recored from list
***
export default class PostList extends Component {
this.state = {
postList: [
{
id: 1,
name: 'All Items',
}, {
id: 2,
name: 'In Stock Items',
}
],
}


remove_post_on_list = (deletePostId) => {
this.setState({
postList: this.state.postList.filter(item => item.post_id != deletePostId)
})
}

}
***
4 years ago
React hooks are a new way (still being developed) to access the core features of react such as state without having to use classes, in your example if you want to increment a counter directly in the handler function without specifying it directly in the onClick prop, you could do something like:

***
const [count, setCounter] = useState(0);
const [moreStuff, setMoreStuff] = useState(...);


const setCount = () => {
setCounter(count + 1);
setMoreStuff(...);
...
};
***
and onClick:
***

***
4 years ago
You're almost correct, just misplaced a few quotes. Wrapping the whole thing in regular quotes will literally give you the string #demo + {this.state.id} - you need to indicate which are variables and which are string literals. Since anything inside {} is an inline JSX expression, you can do:
***
href={"#demo" + this.state.id}
***
This will use the string literal #demo and concatenate it to the value of this.state.id. This can then be applied to all strings. Consider this:
***
var text = "world";
***
And this:
***
{"Hello " + text + " Andrew"}
***
This will yield:

Hello world Andrew
You can also use ES6 string interpolation/template literals with ` (backticks) and ${expr} (interpolated expression), which is closer to what you seem to be trying to do:
***
href={`#demo${this.state.id}`}
***
This will basically substitute the value of this.state.id, concatenating it to #demo. It is equivalent to doing: "#demo" + this.state.id.
4 years ago
The change event is triggered on the





{this.state.value}



);
}
});
***
React.render(, document.body);
Also note that

elements don't have a value attribute. React/JSX simply replicates the well-known HTML syntax, it doesn't introduce custom attributes (with the exception of key and ref). If you want the selected value to be the content of the

element then simply put inside of it, like you would do with any static content.

4 years ago
Starting with Safari 5/Firefox 4, it’s easiest to use the FormData class:
***
var data = new FormData();
jQuery.each(jQuery('#file')[0].files, function(i, file) {
data.append('file-'+i, file);
});
***
So now you have a FormData object, ready to be sent along with the XMLHttpRequest.
***
jQuery.ajax({
url: 'php/upload.php',
data: data,
cache: false,
contentType: false,
processData: false,
method: 'POST',
type: 'POST', // For jQuery < 1.9
success: function(data){
alert(data);
}
});
***
It’s imperative that you set the contentType option to false, forcing jQuery not to add a Content-Type header for you, otherwise, the boundary string will be missing from it. Also, you must leave the processData flag set to false, otherwise, jQuery will try to convert your FormData into a string, which will fail.

You may now retrieve the file in PHP using:
***
$_FILES['file-0']
***
(There is only one file, file-0, unless you specified the multiple attribute on your file input, in which case, the numbers will increment with each file.)

Using the FormData emulation for older browsers
***
var opts = {
url: 'php/upload.php',
data: data,
cache: false,
contentType: false,
processData: false,
method: 'POST',
type: 'POST', // For jQuery < 1.9
success: function(data){
alert(data);
}
};
if(data.fake) {
// Make sure no text encoding stuff is done by xhr
opts.xhr = function() { var xhr = jQuery.ajaxSettings.xhr(); xhr.send = xhr.sendAsBinary; return xhr; }
opts.contentType = "multipart/form-data; boundary="+data.boundary;
opts.data = data.toString();
}
jQuery.ajax(opts);
***
Create FormData from an existing form

Instead of manually iterating the files, the FormData object can also be created with the contents of an existing form object:
***
var data = new FormData(jQuery('form')[0]);
***
Use a PHP native array instead of a counter

Just name your file elements the same and end the name in brackets:
***
jQuery.each(jQuery('#file')[0].files, function(i, file) {
data.append('file[]', file);
});
***
$_FILES['file'] will then be an array containing the file upload fields for every file uploaded. I actually recommend this over my initial solution as it’s simpler to iterate over.
4 years ago
Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.
***
var myEl = document.getElementById('myelement');

myEl.addEventListener('click', function() {
alert('Hello world');
}, false);

myEl.addEventListener('click', function() {
alert('Hello world again!!!');
}, false);
***
4 years ago
Note: keyCode is becoming deprecated, use key instead.
***
function keyPress (e) {
if(e.key === "Escape") {
// write your logic here.
}
}
***
4 years ago