使用来自被动输入的值直接输入到自定义函数中 [英] using values from a reactive input to directly input into a custom function
问题描述
get_num< - function(input $ num){ans< -input $ num
return(ans)}
由于R被 $
符号。
虽然这是一个玩具的例子,但我想写一个函数直接获取被动输入值(在这种情况下是一个数字)并对它们做一些有意义的事情,而不必通过
num < - input $ num
get_num < - function(num) {ans <-num
return(ans)}
这甚至可能吗?
这里有三点:$ b
$ b
- 在处理被动值时,在脚本中使用
reactive()
而不是function()
。
以下是示例:
num_square = reactive({input $ num ^ 2})
observe(print(num_square()))
第一行基于输入$ num
定义了一个新的反应值,第二行只要它发生变化就会打印出来。请注意,被动值与函数相同,您应该在它们前面使用()
来调用它们。
- 当您要将值保存到外部环境(其他内部使用函数或反应函数)时,您应该使用
<< -
而不是=
或< -
表示法。
以下是一个例子:
reactive1 < - reactive({num_square < < - input $ num ^ 2
print(num_square)})
上面的行只要在代码中运行 reactive1()
某处,就会更改 num_square
的值。请注意,如果不运行 reactive1()
,那么 num_square
的值不会改变。这是 reactive()
(懒惰评估)和 observe()
(急切评估)之间的巨大差异。
-
observe()
是另一种使用反应值的方法在一个函数中。 在我看来,您正在寻找这件作品。 就是一个例子。在您的程序中更改输入$ num时,get_num
的值将会更改observe({get_num<< - input $ num
print(get_tmp)})
请注意,上面的脚本应该位于
shinyServer(function(input,output){...})
中间。
reactive()和 observe()
:[请参阅: http://shiny.rstudio.com/ reference / shiny / latest / observe.html ]
观察者就像一个反应式表达式,它可以读取反应式
值并调用反应表达式,并且当这些相关性发生变化时将自动重新执行
。但与反应式
表达式不同,它不会产生结果,也不能用作其他反应式表达式的输入
。因此,观察者仅对
的副作用有用(例如,执行I / O)。
被动表达式和观察者之间的另一个对比是它们的
执行策略。反应式表达式使用惰性评估;也就是
,当它们的依赖关系发生变化时,它们不会立即重新执行,而是等待它们被其他人调用。事实上,如果他们是
没有被叫,那么他们将永远不会重新执行。相比之下,观察员使用
渴望评估;只要它们的依赖关系发生变化,它们就会自行安排
重新执行。
The following function fails:
get_num <- function(input$num){ans <-input$num return(ans)}
since R is confused by the
$
symbol.Although this is a toy example, I would like to write a function that directly takes reactive input values (in this case, a number) and does something meaningful with them, without having to preempt the situation with
num <- input$num get_num <- function(num){ans <-num return(ans)}
Is this even possible?
解决方案There are three points here:
- when you are dealing with reactive values, you use
reactive()
instead offunction()
in your script.
Here is example:
num_square = reactive({input$num^2}) observe(print(num_square()))
The first line defines a new reactive values base on
input$num
and second lines print it as soon as it changes. Note that reactive values are same as function, and you should call them with()
in front of them.- when you want to save a value to outside environment (other that internal use of function or reactive) you should use
<<-
instead of=
or<-
notation.
Here is an example:
reactive1 <- reactive({num_square <<- input$num^2 print(num_square) })
The above line changes the value of
num_square
as soon as you runreactive1()
some place in your code. note that without runningreactive1()
the value ofnum_square
wont change. This is the BIG DIFFERENCE betweenreactive()
(lazy evaluation) andobserve()
(eager evaluation).observe()
is another method to use reactive values in a function. It seems to me that you are looking for this one.
Here is an example. The value of
get_num
will change as soon as you change input$num in your program.observe({get_num <<- input$num print(get_tmp)})
Note that above script should be in middle of
shinyServer(function(input, output) { ... })
.
Difference between
reactive()
andobserve()
: [refer to: http://shiny.rstudio.com/reference/shiny/latest/observe.html ]An observer is like a reactive expression in that it can read reactive values and call reactive expressions, and will automatically re-execute when those dependencies change. But unlike reactive expressions, it doesn't yield a result and can't be used as an input to other reactive expressions. Thus, observers are only useful for their side effects (for example, performing I/O).
Another contrast between reactive expressions and observers is their execution strategy. Reactive expressions use lazy evaluation; that is, when their dependencies change, they don't re-execute right away but rather wait until they are called by someone else. Indeed, if they are not called then they will never re-execute. In contrast, observers use eager evaluation; as soon as their dependencies change, they schedule themselves to re-execute.
这篇关于使用来自被动输入的值直接输入到自定义函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- when you are dealing with reactive values, you use