I am trying to create an app with 2 buttons to show either chart or table (not both) in the same box content. For example, if user click on chart button, chart appears. similarly clicking on table button, table appear in the same place and chart disappears.
minimal example
if (interactive()) {
library(shiny)
library(shinydashboard)
shinyApp(
ui = shinydashboard::dashboardPage(
header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody(
actionButton(inputId = 'input1', label = 'Chart'),
actionButton(inputId = 'input2', label = 'Table'),
box(
uiOutput('plot_table_output'))
),
title = "DashboardPage"
),
server = function(input, output) {
output$plot_table_output <- renderUI({
if(input$input1 >0) {
plotOutput('my_plot')
}
if(input$input2 >0) {
dataTableOutput('mytable')
}
})
output$my_plot <- renderPlot({
mydf <- data.frame(X=1:10, Y=1:10)
plot(mydf$X, mydf$Y, type = 'l')
})
output$mytable <- renderDataTable({
mydf <- data.frame(X=1:10, Y=1:10)
mydf
})
}
)
}
One way to do it is to use ObserveEvent()
. Try this
library(shiny)
library(shinydashboard)
shinyApp(
ui = shinydashboard::dashboardPage(
header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody(
actionButton(inputId = 'input1', label = 'Chart'),
actionButton(inputId = 'input2', label = 'Table'),
box(
uiOutput('plot_table_output'))
),
title = "DashboardPage"
),
server = function(input, output) {
observeEvent(input$input1, {
output$plot_table_output <- renderUI({
plotOutput('my_plot')
})
})
observeEvent(input$input2, {
output$plot_table_output <- renderUI({
dataTableOutput('mytable')
})
})
output$my_plot <- renderPlot({
mydf <- data.frame(X=1:10, Y=1:10)
plot(mydf$X, mydf$Y, type = 'l')
})
output$mytable <- renderDataTable({
mydf <- data.frame(X=1:10, Y=1:10)
mydf
})
}
)