【发布时间】:2019-05-15 13:50:04
【问题描述】:
我有一个闪亮的模块,它使用反应值来存储其内部状态。在下面的示例中,这仅用于输出输入更改的数字,但我的真实use-cases 更复杂。
我现在想创建一个函数,该函数可用于将这些模块之一设置为另一个模块的状态,包括内部状态 - 或更一般地说:我想创建一个函数 updateModule,它也可以更新内部状态。
所以我的问题是:如何从外部访问和更改模块内部的 reactiveValues?
另一个与我的特殊目的相关的问题是:如何在更新 Input 时防止内部 reactiveValue 更新 - 或者如何重置它(回到主要问题?
目前,我知道两种可能的解决方法:
- Store internal state in hidden Input
- 使用 data.table A,B 的按引用逻辑调用(见下文)。有other ways of implementing call-by-reference,还没用过。
但是我很想知道是否有更直接的解决方案,也因为我要更新的内部是更复杂的列表。
示例代码
#Problem: How to change reactiveValues from the outside?
library(shiny)
moduleUI <- function(id, label=id,min = 0,max = 100,value = 30){
ns <- NS(id)
fluidRow(
column(width=9,
sliderInput(ns("sl"), label=label, min=min, max=max, value=value)
),
column(width=2,
textOutput(ns("changesCount") )
)
)
}
synchModule<-function(session, targetModule, oldModule){
ns<-NS(targetModule)
updateSliderInput(session,ns("sl"),value=oldModule() )
##Accessing and changing internal Value of targetModule??
}
module<- function(input, output, session){
rv<-reactiveValues(changesCount=0)
observeEvent(input$sl,rv$changesCount<-rv$changesCount+1)
output$changesCount=renderText(rv$changesCount)
return(reactive({
ret <- input$sl
attr(ret,"changesCount")<-rv$changesCount
ret
}))
}
ui=fluidPage(
moduleUI("module1"),
moduleUI("module2"),
actionButton("synchButton", "Set Module 2 to state of Module 1."),
textOutput("module1state"),
textOutput("module2state")
)
server= function(input, output, session) {
module1<-callModule(module,"module1")
module2<-callModule(module,"module2")
observeEvent(input$synchButton, synchModule(session,"module2",module1)
)
output$module1state=renderPrint(module1() )
output$module2state=renderPrint(module2() )
}
shinyApp(ui, server)
解决方法 1:使用隐藏的 NumericInput
#Problem: How to change reactiveValues from the outside?
##Workaround using hidden input
library(shiny)
library(shinyjs)
moduleUI <- function(id, label=id,min = 0,max = 100,value = 30){
ns <- NS(id)
fluidRow(
column(width=9,
sliderInput(ns("sl"), label=label, min=min, max=max, value=value)
),
column(width=2,
textOutput(ns("changesCount") ),
hidden(numericInput(
ns("changesCountNumeric"), "If you can see this, you forgot useShinyjs()", 0)
)
)
)
}
synchModule<-function(session, targetModule, oldModule){
ns<-NS(targetModule)
updateSliderInput(session,ns("sl"),value=oldModule() )
updateNumericInput(session,ns("changesCountNumeric"),
value=attr(oldModule(),"changesCount")-1) #-1 to account for updating slider itself,
}
module<- function(input, output, session){
observeEvent(input$sl,
updateNumericInput(session,"changesCountNumeric",
value=input$changesCountNumeric+1)
)
output$changesCount=renderText(input$changesCountNumeric)
return(reactive({
ret <- input$sl
attr(ret,"changesCount")<-input$changesCountNumeric
ret
}))
}
ui=fluidPage(
useShinyjs(),
moduleUI("module1"),
moduleUI("module2"),
actionButton("synchButton", "Set Module 2 to state of Module 1."),
textOutput("module1state"),
textOutput("module2state")
)
server= function(input, output, session) {
module1<-callModule(module,"module1")
module2<-callModule(module,"module2")
observeEvent(input$synchButton, synchModule(session,"module2",module1)
)
output$module1state=renderPrint(module1() )
output$module2state=renderPrint(module2() )
}
shinyApp(ui, server)
P.s:我不确定是否将我的解决方法作为解决方案。
【问题讨论】: