R 当输入或输出IF时,分配给封装在观察表达式中的对象的值会给我不同的值
我刚刚构建了一个表达式,为同一对象提供了两个不同的值,一个R 当输入或输出IF时,分配给封装在观察表达式中的对象的值会给我不同的值,r,shiny,R,Shiny,我刚刚构建了一个表达式,为同一对象提供了两个不同的值,一个if的内部和外部。小例子: library(shiny) ui <- fluidPage() server <- function(input, output, session) { a <- 1 b <- 2 observe({ if(a != b) { print(paste0("inside ", a)) a <- b } print
if
的内部和外部。小例子:
library(shiny)
ui <- fluidPage()
server <- function(input, output, session) {
a <- 1
b <- 2
observe({
if(a != b) {
print(paste0("inside ", a))
a <- b
}
print(paste0("outside ", a))
invalidateLater(1000)
})
}
shinyApp(ui, server)
库(闪亮)
ui如果要生成反应值,请使用reactiveVal
。有关更多详细信息,请参见?reactiveVal
。试试这个:
server <- function(input, output, session) {
#Here we build the reactive values
#Both a and b are functions.
#Calling them with no argument retrieves the value
#You set the value if you call them with an argument
a <- reactiveVal(1)
b <- reactiveVal(2)
observe({
if(a() != b()) {
print(paste0("inside ", a()))
#here we set the value of a() the same as b()
a(b())
}
print(paste0("outside ", a()))
invalidateLater(1000)
})
}
server如果要生成反应值,请使用reactiveVal
。有关更多详细信息,请参见?reactiveVal
。试试这个:
server <- function(input, output, session) {
#Here we build the reactive values
#Both a and b are functions.
#Calling them with no argument retrieves the value
#You set the value if you call them with an argument
a <- reactiveVal(1)
b <- reactiveVal(2)
observe({
if(a() != b()) {
print(paste0("inside ", a()))
#here we set the value of a() the same as b()
a(b())
}
print(paste0("outside ", a()))
invalidateLater(1000)
})
}
服务器我不明白您想在哪里显示什么。第一次打印时a
的值为1。然后将该值更改为b
的值,即2<代码>如果
语句没有创建自己的作用域,那么您正在更改a
的观察块级别值,而不是a
的服务器级别值。如果需要具有副作用的更新,则应使用reactiveValues()
而不是简单的函数级变量。但我仍然不清楚你到底想做什么。但是如果与
相关,就不应该是
。我不明白你想在哪里展示什么。第一次打印时a
的值为1。然后将该值更改为b
的值,即2<代码>如果语句没有创建自己的作用域,那么您正在更改a
的观察块级别值,而不是a
的服务器级别值。如果需要具有副作用的更新,则应使用reactiveValues()
而不是简单的函数级变量。但我仍然不清楚你到底想做什么。但是如果相关的话,就不应该是。谢谢,我想现在我明白了。我完全误解了反应性的概念,但我想我现在明白了…谢谢,现在我明白了,我想。我完全误解了反应性的概念,但我想我现在明白了。。