从另一个选项卡面板调用时,rHandsonTable模块返回NULL
我有一个应用程序,它在一个使用从另一个选项卡面板调用时,rHandsonTable模块返回NULL,r,module,shiny,rhandsontable,R,Module,Shiny,Rhandsontable,我有一个应用程序,它在一个使用选项卡面板的模块中使用rHandsonTableOutput(HoT)。虽然我可以从该模块获取其他数据(例如,在selectInput中进行的选择),但由于某种原因,我在尝试获取HoT数据时会得到一个空值。我制作了一个非常简单的代码版本来说明我的意思 我发现,如果将模块放在同一UI元素中,则可以访问热数据。出于某种原因,如果我将它放在选项卡面板的另一个选项卡中,它将无法返回数据。我的示例代码应该在这里显示这一点 更新-我发现只要我先点击并加载有问题的HoT选项卡,模
选项卡面板的模块中使用rHandsonTableOutput
(HoT)。虽然我可以从该模块获取其他数据(例如,在selectInput
中进行的选择),但由于某种原因,我在尝试获取HoT
数据时会得到一个空值。我制作了一个非常简单的代码版本来说明我的意思
我发现,如果将模块放在同一UI元素中,则可以访问热数据。出于某种原因,如果我将它放在选项卡面板
的另一个选项卡中,它将无法返回数据。我的示例代码应该在这里显示这一点
更新-我发现只要我先点击并加载有问题的HoT
选项卡,模块就会工作!首先“查看”选项卡似乎可以解决此问题。我很想找到一种不需要这样做的方法。这是否与rshiny“sessions”有关
下面是应该重现错误的代码。您将发现一个具有两个HoT
s的简单应用程序。主选项卡有一个通过模块myModuleUI
输入的选项卡。还有第二个下拉选项卡,其中包含通过第二个模块myModuleTabUI
生成的第二个HoT
。它们完全相同,只是myModuleTabUI
将所有内容都放在选项卡面板中。如果您按下“添加表格按钮”,它只是将表格中的数字相加,但在尝试对ID为first的HoT
选项卡(即在另一个选项卡中找到的HoT
进行相加时,将失败
这包含主应用程序
应用程序.R
library(shiny)
library(rhandsontable)
source('my_modules.R')
ui <- navbarPage("Module Test Tool",
tabsetPanel(id = 'mainTabset',
tabPanel("My Tab",
#This is the HoT that is accessible from a module
h4("Table 1"),
myModuleUI('my_module'),
br(),
br(),
fluidRow(
actionButton("sum_btn", "Add table data"),
br(),
br(),
textOutput('table1_sum'),
textOutput('table2_sum'),
br(),
br()
)
),
navbarMenu("My Module Tabs",
#This is the HoT that is inaccessible from a module
myModuleTabUI('first_tab', 'First')
)
)
)
server <- function(input, output, session) {
#Link logic for tab module
callModule(myModule, 'my_module')
callModule(myModuleTab, 'first_tab')
one_col = rep.int(1,3)
df = data.frame(col1 = one_col,
col2 = one_col,
col3 = one_col)
output$hot <- renderRHandsontable({
rhandsontable(df, stretchH = "none", rowHeaders = NULL)
})
#This button sums up all the rHandsonTable data frames
observeEvent(input$sum_btn, {
#Works just fine when not pulling from the same panel's module
module_data = callModule(getMyModuleData, 'my_module')
module_int= module_data$module_int
module_df = module_data$module_hot
output$table1_sum = renderText({
paste0("Sum of Table 1 is: ", sum(module_df)," | Integer one is: ", module_int)
})
#Fails when pulling a hands on table from another tab
module_tab_data = callModule(getMyModuleTabData, 'first_tab') #<---THIS LINE FAILS
module_tab_int= module_tab_data$module_tab_int
module_tab_df = module_tab_data$module_tab_hot
output$table2_sum = renderText({
paste0("Sum of the table in the 'First' tab is: ", sum(module_tab_df)," | Integer in 'First' tab is: ", module_tab_int)
})
})
}
## Create Shiny app ----
shinyApp(ui, server)
#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) {
two_col = rep.int(2,3)
df = data.frame(col1 = two_col,
col2 = two_col,
col3 = two_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_hot = hot_to_r(input$module_hot),
module_int = input$module_int_list
)
)
}
#Simple module that adds the same as MyModuleUI, except in a tabPanel
myModuleTabUI <- function(id,tab_name){
ns <- NS(id)
tabPanel(tab_name,
fluidRow(
rHandsontableOutput(ns("module_tab_hot")),
selectInput(ns('module_tab_int_list'),"Integers:",c(1:5), selected = 1)
)
)
}
#Initializes myModuleTabUI rHandsonTable with some values
myModuleTab <- function(input, output, session){
three_col = rep.int(3,3)
df = data.frame(col1 = three_col,
col2 = three_col,
col3 = three_col)
output$module_tab_hot <- renderRHandsontable({
rhandsontable(df, stretchH = "none", rowHeaders = NULL)
})
}
#Returns MyModuleTab data for use outside of the module
getMyModuleTabData <- function(input,output,session){
return (
list(
module_tab_hot = hot_to_r(input$module_tab_hot), #<---THIS LINE FAILS
module_tab_int = input$module_tab_int_list
)
)
}
库(闪亮)
图书馆(rhandsontable)
源('my_modules.R')
ui请编辑以下行:
#Fails when pulling a hands on table from another tab
module_tab_dat = callModule(getMyModuleTabData, 'first_tab') #<---THIS LINE FAILS
module_tab_int= module_tab_dat$module_tab_int
module_tab_df = module_tab_dat$module_tab_hot
但是您将模块命名为module\u tab\u dat
。复制粘贴错误,我想
更新
关于您的更新,请在模块代码中添加此行:
output$module_tab_hot <- renderRHandsontable({
rhandsontable(df, stretchH = "none", rowHeaders = NULL)
})
# Added line
outputOptions(output, "module_tab_hot", suspendWhenHidden = FALSE)
output$module\u tab\u热好捕获!是的,这是一个复制粘贴错误。谢谢你看!但要明确的是,这不是错误的根源!错误仍然存在。您已将其编辑为模块\u选项卡\u数据。它实际上是module_tab_dat.Eli,如果您不先单击选项卡就运行应用程序,它是否有效?如果我先单击“第一个”选项卡,然后单击“添加表数据”按钮,这对我是有效的。如果我没有,我就会收到错误。好像我必须在“第一”选项卡中“查看”或加载有问题的rHandsonTable才能成功。我刚刚看到了您的更新。我现在要调查那件事。我似乎误解了你的问题。
output$module_tab_hot <- renderRHandsontable({
rhandsontable(df, stretchH = "none", rowHeaders = NULL)
})
# Added line
outputOptions(output, "module_tab_hot", suspendWhenHidden = FALSE)