eventReactive()的初始状态(Initial state of eventReactive())
我在我的闪亮app中使用了一个被动事件,被动输出是renderDataTable的一部分,我想改变该函数的初始结果。
我从纪录片中读到:
"eventReactive() returns NULL until the action button is clicked. As a result, the graph does not appear until the user asks for it by clicking “Go”."
但这不适用于我的代码。生殖实例:
library(shiny) library(DT) shinyApp( ui = fluidPage( actionButton("go","Go"), DT::dataTableOutput('tbl')), server = function(input, output) { dat <- eventReactive(input$go, head(iris,10) ) output$tbl <- renderDataTable({ if(is.null(dat())){ data.frame("a"=NA,"b"=NA,"c"=NA,"d"=NA,"e"=NA) }else(data.frame(dat())) }) } )
这应该在单击按钮之前返回带有5个变量的空data.frame,而不是。
由于函数在单击按钮后作出反应,我认为函数的状态之前为NULL,因为它是if语句的一部分。
I'm using an reactive event in my shiny app, the reactive output is part of a renderDataTable and I want to change the initial outcome of that function.
From the documentary I read:
"eventReactive() returns NULL until the action button is clicked. As a result, the graph does not appear until the user asks for it by clicking “Go”."
But this does not work with my code.Reproductive Example:
library(shiny) library(DT) shinyApp( ui = fluidPage( actionButton("go","Go"), DT::dataTableOutput('tbl')), server = function(input, output) { dat <- eventReactive(input$go, head(iris,10) ) output$tbl <- renderDataTable({ if(is.null(dat())){ data.frame("a"=NA,"b"=NA,"c"=NA,"d"=NA,"e"=NA) }else(data.frame(dat())) }) } )
This should return an empty data.frame with 5 variables before the button is clicked, which it is not.
As the function reacts after the button is clicked, I think the state of the function was NULL before, as it is part of the if statement.
原文:https://stackoverflow.com/questions/35792052
最满意答案
Typescript使用类似ActionScript3的getter / setter语法。
class foo { private _bar:boolean = false; get bar():boolean { return this._bar; } set bar(theBar:boolean) { this._bar = theBar; } }
这将产生这个Javascript,使用Ecmascript 5 Object.defineProperty()功能。
var foo = (function () { function foo() { this._bar = false; } Object.defineProperty(foo.prototype, "bar", { get: function () { return this._bar; }, set: function (theBar) { this._bar = theBar; }, enumerable: true, configurable: true }); return foo; })();
所以要用,
var myFoo = new foo(); if(myFoo.bar) { // calls the getter myFoo.bar = false; // calls the setter and passes false }
但是,为了完全使用它,您必须确保TypeScript编译器指向ECMAScript5。 如果您正在运行命令行编译器,请使用--target标志像这样;
tsc - 目标ES5
如果您使用的是Visual Studio,则必须编辑项目文件以将该标志添加到TypeScriptCompile构建工具的配置中。 你可以在这里看到:
Typescript uses getter/setter syntax that is like ActionScript3.
class foo { private _bar:boolean = false; get bar():boolean { return this._bar; } set bar(theBar:boolean) { this._bar = theBar; } }
That will produce this Javascript, using the Ecmascript 5 Object.defineProperty() feature.
var foo = (function () { function foo() { this._bar = false; } Object.defineProperty(foo.prototype, "bar", { get: function () { return this._bar; }, set: function (theBar) { this._bar = theBar; }, enumerable: true, configurable: true }); return foo; })();
So to use it,
var myFoo = new foo(); if(myFoo.bar) { // calls the getter myFoo.bar = false; // calls the setter and passes false }
However, in order to use it at all, you must make sure the TypeScript compiler targets ECMAScript5. If you are running the command line compiler, use --target flag like this;
tsc --target ES5
If you are using Visual Studio, you must edit your project file to add the flag to the configuration for the TypeScriptCompile build tool. You can see that here:
As @DanFromGermany suggests below, if your are simply reading and writing a local property like foo.bar = true, then having a setter and getter pair is overkill. You can always add them later if you need to do something, like logging, whenever the property is read or written.
相关问答
更多-
constructor(options: any = {}) { options.tagName = 'li'; super(options); } 如果我没有弄错,你可以在super()调用之前做一些事情,只要它不影响this constructor(options: any = {}) { options.tagName = 'li'; super(options); } If I'm not mistaken, you can do things before th ...
-
在TypeScript中获取并设置(get and set in TypeScript)[2023-02-09]
Typescript使用类似ActionScript3的getter / setter语法。 class foo { private _bar:boolean = false; get bar():boolean { return this._bar; } set bar(theBar:boolean) { this._bar = theBar; } } 这将产生这个Javascript,使用Ecmascript 5 Object.d ... -
在Typescript中, enums被转换为JS数组(参见TypeScript中的枚举:JavaScript代码在做什么? )。 因此,如果要动态加载枚举,只需加载数组即可。 但是如果你的目标是创建一个select这个枚举,你应该definely反序列化你的json并将其传递给一个嵌入select的组件,使用*ngFor从你的对象创建option 。 最简单的方法是以这种方式格式化JSON: { "selectData":[ { ...
-
如何在TypeScript中的`window`上显式设置一个新属性?(How do you explicitly set a new property on `window` in TypeScript?)[2022-11-22]
为了保持动态,只需使用: (window).MyNamespace Just found the answer to this in another StackOverflow question's answer. declare global { interface Window { MyNamespace: any; } } window.MyNamespace = window.MyNamespace || {}; Basically you need to extend th ... -
TypeScript中的Hashset(Hashset in TypeScript)[2022-03-13]
它存在! mySet: Set= new Set (); https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set It exists! mySet: Set = new Set (); https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_ ... -
Getter和Setter有几个用途,比如 如果不指定setter,则可以使私有变量只读 class example { private _a: any; get a(): any{ return this._a; } } 您可以使用它们在变量发生变化时执行自定义逻辑,对事件发射器进行排序 class example { private _a: any; set a(value: any){ this._a = value; ...
-
Set是一个可迭代的对象,初始数组是可迭代的值,这就是为什么你可以通过for..of访问它们的原因。 当你说pets["species"]= "mamals"你正在创造一个属性“物种”,其值为“mamals”,但这不是可迭代值的一部分,这就是为什么你可以在使用for..in时看到它。 for..of将显示iterable的值,而for..in将显示对象的属性。 我希望这有帮助 Set is an iterable object the initial array sets are the iterable ...
-
由于Nitzan的评论解决了这个问题。 这是代码: declare private getUrl(){ chrome.tabs.query({}, function(tabs) { console.log(tabs[0]); }); } ngOnInit(){ this.getUrl(); } Solved it thanks to Nitzan's comment. Here is the code : declare private getUrl(){ ...
-
Polyfill在TypeScript中设置,但“Set is not defined”(Polyfill Set in TypeScript but “Set is not defined”)[2023-03-29]
似乎cdn.polyfill.io不能识别这些设备的用户代理。 根据文档 - 如果用户代理未知且请求参数未知,则可以强制返回默认的填充。 Seems cdn.polyfill.io doesn't recognize User-Agent of these devices. According to docs - ... -
你可以使用一个seperat对象或数组( Playground ): enum RoleTypes { None, Admin, SuperAdmin } let RoleTypesDisplay: { [index: number]: string } = {}; RoleTypesDisplay[RoleTypes.None] = "None"; RoleTypesDisplay[RoleTypes.Admin] = "Administrator"; RoleTypesDisp ...