【发布时间】:2019-12-15 08:15:22
【问题描述】:
在 R 中使用模块时,我遇到了反应性问题。如果我更新一个模块,然后尝试用这些更新的值更新另一个模块,我会在更新之前获取这些值。 >
我已经编写了一些基本代码来说明我的意思。在这里,我有一个应用程序更新 rHandsontableOutput 放置在名为 my_module 的模块中,然后在按下按钮时将此更新后的 rHandsontableOutput 复制到名为 module_to_update 的第二个模块中。
我发现my_module 中的第一个表将更新,但module_to_update 中的表不会更新。相反,module_to_update 表将收到my_module 的初始表的副本更新之前。如果我再次按下更新按钮,一切都会按预期进行。
我猜这是我如何处理会话或一般反应值的问题,但我没有想法。
问题:如何设置响应式值和模块,以便我可以在同一个函数调用中对更新的模块数据运行操作? (例如,请参阅下面的 observeEvent(input$update_btn, ...) 调用示例)
application.R
library(shiny)
library(rhandsontable)
source('my_modules.R')
active_tab = ""
ui <- navbarPage("Module Test Tool",
tabsetPanel(id = 'mainTabset',
tabPanel("My Tab",
#This is the HoT that works as expected, updating when called upon
h4("Table 1"),
myModuleUI('my_module'),
#This is the HoT that does NOT work as expected. This HoT fails to use the updated values from 'my_module' HoT
h4("Table to be updated"),
myModuleUI('module_to_update'),
br(),
br(),
fluidRow(
#this button updates tables to new values
actionButton("update_btn", "Update and Add Tables"),
br(),
br(),
textOutput('table1_sum'),
textOutput('table2_sum'),
br(),
br()
)
)
)
)
server <- function(input, output, session) {
#Link logic for tab module
callModule(myModule, 'my_module')
#This button sums up all the rHandsonTable data frames
observeEvent(input$update_btn, {
#Update values in table and integer drop down list before doing basic operations on them
#New values should be all 5s
five_col = rep(5,3)
callModule(updateModule, 'my_module', 5, data.frame(col1 = five_col,
col2 = five_col,
col3 = five_col))
#Grabs updated module table and does operations on it
module_data = callModule(getMyModuleData, 'my_module')
module_int= module_data$module_int
module_df = module_data$module_df
output$table1_sum = renderText({
paste0("Sum of Table 1 is: ", sum(module_df())," | The selected integer is: ", module_int())
})
#------------------------------------------------------
#------------------ERROR BELOW-------------------------
#------------------------------------------------------
#THIS IS THE CODE THAT FAILS. This updates a 2nd module that should mirror the updated values. However, this results in old values.
callModule(updateModule, 'module_to_update', module_int(), module_df())
#Tries to call on new, updated table
updated_module_data = callModule(getMyModuleData, 'module_to_update')
updated_module_int= updated_module_data$module_int
updated_module_df = updated_module_data$module_df
#Display results of basic operations on new table
output$table2_sum = renderText({
paste0("Sum of Updated Table is: ", sum(updated_module_df())," | The selected integer is: ", updated_module_int())
})
})
}
## Create Shiny app ----
shinyApp(ui, server)
my_modules.R
#Simple module containing one rHandsontable and a drop down list of integers
myModuleUI <- function(id,tab_name){
ns <- NS(id)
fluidRow(
rHandsontableOutput(ns("module_hot")),
selectInput(ns('module_int_list'),"Integers:",c(1:5), selected = 1)
)
}
#Initializes myModuleUI rHandsonTable with some values
myModule <- function(input, output, session) {
one_col = rep.int('VALUE AT INITIALIZATION',3)
df = data.frame(col1 = one_col,
col2 = one_col,
col3 = one_col)
output$module_hot <- renderRHandsontable({
rhandsontable(df, stretchH = "none", rowHeaders = NULL)
})
}
#Returns myModule data for use outside of the module
getMyModuleData <- function(input,output,session){
return (
list(
module_df = reactive({hot_to_r(input$module_hot)}),
module_int = reactive({input$module_int_list})
)
)
}
updateModule<- function(input,output,session, new_integer, new_dataframe){
if(!is.null(new_dataframe))
{
output$module_hot <- renderRHandsontable({
rhandsontable(new_dataframe, stretchH = "none", rowHeaders = NULL)
})
}
outputOptions(output, "module_hot", suspendWhenHidden = FALSE)
updateSelectInput(session, "module_int_list",selected = new_integer)
}
【问题讨论】:
-
为什么将 rhandsontable 渲染和数据评估分成多个模块?似乎把事情复杂化了很多。你能提供一个最小的例子来指出你的问题吗?很难遵循您的代码结构
-
您好 Thomas,感谢您的反馈。我可能会进一步剥离它。不过,为了给你上下文,这是我实际问题的一个非常简化的版本。我的工具具有模块化的选项卡面板,其中包含 rhandsontables。我正在执行一个更新一个模块中的 rhandsontable 的操作,然后在一个单独的模块中使用更新的值。这是我试图用简单的术语重现问题。
标签: r shiny reactive rhandsontable