How to Reset All Options() Arguments to Their Default Values

How do I reset all options() arguments to their default values?

If you restart your R session, it will reset the options to the default values.
Options are saved in a list, and calling options() will show that list.

You can save the default options after restarting R:

backup_options <- options()

You can make any changes you need, and then to revert to the default options:

options(backup_options)

Reset the graphical parameters back to default values without use of dev.off()

See ?par. The idea is that you save them as they are when you found them, and then restore:

old.par <- par(mar = c(0, 0, 0, 0))
## do plotting stuff with new settings

Now restore as they were before we changed mar:

par(old.par)

Reset select value to default

You can make use of the defaultSelected property of an option element:

Contains the initial value of the selected HTML attribute, indicating whether the option is selected by default or not.

So, the DOM interface already keeps track which option was selected initially.

$("#reset").on("click", function () {
$('#my_select option').prop('selected', function() {
return this.defaultSelected;
});
});

DEMO

This would even work for multi-select elements.

If you don't want to iterate over all options, but "break" after you found the originally selected one, you can use .each instead:

$('#my_select option').each(function () {
if (this.defaultSelected) {
this.selected = true;
return false;
}
});

Without jQuery:

var options = document.querySelectorAll('#my_select option');
for (var i = 0, l = options.length; i < l; i++) {
options[i].selected = options[i].defaultSelected;
}

Reset par to the default values at startup

Every time a new device is opened par() will reset, so another option is simply do dev.off() and continue.

reset picocli Option field

Picocli automatically resets all @Option and @Parameter-annotated fields to their default value (which may be null) immediately before parsing command line arguments. (This is what allows CommandLine objects to be reused.) There is no need for a user-defined reset method.

I tried to use the defaultValue attribute of the @Option annotation, but it did not seem to reset the field value on each call.

Can you provide an example that reproduces this issue?

Alternatively, when you reproduce the issue in your application, can you run it with system property -Dpicocli.trace=DEBUG and post the output?

Note I'm still on picocli 3.9.6, but I do plan to upgrade to 4.x, so if that's part of the solution, that's fine.

Please do upgrade to 4.x, the latest version of picocli has many bug fixes and new features, and is overall a lot better. However, the "reset" behaviour has been part of picocli for a long time, certainly in the 3.x version, probably even earlier.

Default List Parameter doesn't Reset

No. It's a foible of Python that mutable object initializers do this. It's a better pattern to use None and set the initial value in the method body:

def f(a=None):
if a is None:
a = []
print(a)
a += [1]

If you use pylint, it provides a warning dangerous-default-value (W0102).

Setting default value for TypeScript object passed as argument

Actually, there appears to now be a simple way. The following code works in TypeScript 1.5:

function sayName({ first, last = 'Smith' }: {first: string; last?: string }): void {
const name = first + ' ' + last;
console.log(name);
}

sayName({ first: 'Bob' });

The trick is to first put in brackets what keys you want to pick from the argument object, with key=value for any defaults. Follow that with the : and a type declaration.

This is a little different than what you were trying to do, because instead of having an intact params object, you have instead have dereferenced variables.

If you want to make it optional to pass anything to the function, add a ? for all keys in the type, and add a default of ={} after the type declaration:

function sayName({first='Bob',last='Smith'}: {first?: string; last?: string}={}){
var name = first + " " + last;
alert(name);
}

sayName();

Set a default parameter value for a JavaScript function

From ES6/ES2015, default parameters are in the language specification.

function read_file(file, delete_after = false) {
// Code
}

just works.

Reference: Default Parameters - MDN

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

In ES6, you can simulate default named parameters via destructuring:

// the `= {}` below lets you call the function without any parameters
function myFor({ start = 5, end = 1, step = -1 } = {}) { // (A)
// Use the variables `start`, `end` and `step` here
···
}

// sample call using an object
myFor({ start: 3, end: 0 });

// also OK
myFor();
myFor({});

Pre ES2015,

There are a lot of ways, but this is my preferred method — it lets you pass in anything you want, including false or null. (typeof null == "object")

function foo(a, b) {
a = typeof a !== 'undefined' ? a : 42;
b = typeof b !== 'undefined' ? b : 'default_b';
...
}


Related Topics



Leave a reply



Submit