Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...