Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

r - Namespace error in shiny module with conditional panel

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:

my app.R

library(shiny)

ui <- fluidPage(  
  fluidRow(
    column(6,
           chartTableSwitchUI("firstUniqueID")
           )
    )
)

server <- function(input, output) {
  callModule(chartTableSwitch, "firstUniqueID")
}

shinyApp(ui = ui, server = server)

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.

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.

Where is my error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1) ns function should be called on radioButtons name ("rb1" in your case) and conditional checking should be adapted to that.

2) There is no need to call ns on you choice names.

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")))
  )
}

see also this question for explanation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...