带有条件面板的闪亮模块中的命名空间错误 [英] Namespace error in shiny module with conditional panel
问题描述
我编写了一个小应用程序,您可以在其中看到一个单选按钮,您可以使用该按钮在绘图图表和渲染表格之间切换.有用.之后,我阅读了关于模块的 Shiny 文档并最终得到了这个应用程序:
I programmed a little app where you see a radio button which you could use to switch between a plotly chart and a rendered table. It works. After that I read Shiny documentation on modules and ended up with this app:
我的应用程序.R
library(shiny)
ui <- fluidPage(
fluidRow(
column(6,
chartTableSwitchUI("firstUniqueID")
)
)
)
server <- function(input, output) {
callModule(chartTableSwitch, "firstUniqueID")
}
shinyApp(ui = ui, server = server)
然后我编写了一个看起来像这样的 globar.R:
and I coded a globar.R that looks like this:
library(shiny)
library(plotly)
#define a simple dataframe for module example
X <- c("a", "b", "c")
Y <- c(1,2,3)
df <- data.frame(X,Y)
#UI function for first module
chartTableSwitchUI <- function(id){
ns <- NS(id)
tagList(
radioButtons("rb1", "View", choices = c(ns("Chart"), ns("Table")),
selected = "Chart", inline = TRUE),
conditionalPanel(
condition = "input.rb1 == 'Chart'", ns=ns,
plotlyOutput(ns("chart"))),
conditionalPanel(
condition = "input.rb1 == 'Table'", ns=ns,
tableOutput(ns("chartTable")))
)
}
#Server logic for first module
chartTableSwitch <- function(input, output, session){
output$chart <- renderPlotly(
plot_ly(df, x = ~X, y = ~Y)
)
output$chartTable <- renderTable(df)
}
如果我运行应用程序,单选按钮在那里,但没有绘图或图表.只是单选按钮.
If I run the app the radio buttons are there but no plot or chart. Just the radio buttons.
StackExchange 上的一些研究给了我提示,这可能是由于命名空间错误,但我不知道问题到底是什么.
Some research here on StackExchange gave me the hint that this is probably due to namespacing error but I do not know what exactly the problem is.
我的错误在哪里?
推荐答案
1) ns
函数应该在 radioButtons
名称(在您的情况下为rb1")和条件检查应该适应这种情况.
1) ns
function should be called on radioButtons
name ("rb1" in your case) and conditional checking should be adapted to that.
2) 无需对您选择的名称调用 ns
.
2) There is no need to call ns
on you choice names.
将您的模块 UI 功能更改为:
Change your module UI function to:
#UI function for first module
chartTableSwitchUI <- function(id){
ns <- NS(id)
tagList(
radioButtons(ns("rb1"), "View", choices = c("Chart", "Table"),
selected = "Chart", inline = TRUE),
conditionalPanel(
condition = paste0('input[\'', ns('rb1'), "\'] == \'Chart\'"),
plotlyOutput(ns("chart"))),
conditionalPanel(
condition = paste0('input[\'', ns('rb1'), "\'] == \'Table\'"),
tableOutput(ns("chartTable")))
)
}
另见这个问题的解释.
这篇关于带有条件面板的闪亮模块中的命名空间错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!