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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…