sueb
November 9, 2018, 10:44am
1
...I'm trying to add an action button to swap tabs in shiny but it doesn't seem to work
library(shiny)
library(shinydashboard)
library(DT)
library(tidyverse)
# Application title
header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention")
# Sidebar with a slider input for number of bins
sidebar <- dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard")),
menuItem("Customer Retention", tabName = "CustomerRetention", icon=icon("dashboard"))
))
body <- dashboardBody(
tabItems(
tabItem(tabName = "Marketing",
h3("Annual Marketing Costs") ,
actionButton("switchtab", "Look at Customer Retention")),
tabItem(tabName = "CustomerRetention",
h3("Annual Costs Spent on Existing Customers"))))
ui <- dashboardPage(skin="blue",
header,
sidebar,
body
)
server <- function(input, output) {
observeEvent(input$switchtab, {
newtab <- switch(input$tabs,
"CustomerRetention" = "Marketing",
"Marketing" = "CustomerRetention")
updateTabItems(session, "tabs", newtab)
})
}
shinyApp(ui = ui, server = server)
Hi, you could directly specify the menuItem text in updateTabItems. Try this:
library(shiny)
library(shinydashboard)
library(DT)
library(tidyverse)
# Application title
header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention")
# Sidebar with a slider input for number of bins
sidebar <- dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard")),
menuItem("Customer Retention", tabName = "CustomerRetention", icon=icon("dashboard"))
))
body <- dashboardBody(
tabItems(
tabItem(tabName = "Marketing",
h3("Annual Marketing Costs") ,
actionButton("switchtab", "Look at Customer Retention")),
tabItem(tabName = "CustomerRetention",
h3("Annual Costs Spent on Existing Customers"))))
ui <- dashboardPage(skin="blue",
header,
sidebar,
body
)
server <- function(input, output, session) {
observeEvent(input$switchtab, {
updateTabItems(session, "tabs", "CustomerRetention")
})
}
shinyApp(ui = ui, server = server)
2 Likes
sueb
November 9, 2018, 11:03am
3
arihanth:
library(shiny) library(shinydashboard) library(DT) library(tidyverse) # Application title header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention") # Sidebar with a slider input for number of bins sidebar <- dashboardSidebar( sidebarMenu(id = "tabs", menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard")), menuItem("Customer Retention", tabName = "CustomerRetention", icon=icon("dashboard")) )) body <- dashboardBody( tabItems( tabItem(tabName = "Marketing", h3("Annual Marketing Costs") , actionButton("switchtab", "Look at Customer Retention")), tabItem(tabName = "CustomerRetention", h3("Annual Costs Spent on Existing Customers")))) ui <- dashboardPage(skin="blue", header, sidebar, body ) server <- function(input, output, session) { observeEvent(input$switchtab, { updateTabItems(session, "tabs", "CustomerRetention") }) } shinyApp(ui = ui, server = server)
I've tried importing your code into my script but it doesn't change it so I must have done something wrong in the rest of the program which I can't see and it's long
sueb
November 9, 2018, 11:07am
4
I've imported it in here but not sure what I've done:
library(shiny)
library(shinydashboard)
library(DT)
library(tidyverse)
# Application title
header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention")
# Sidebar with a slider input for number of bins
sidebar <- dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard")),
menuItem("Customer Retention", tabName = "CustomerRetention", icon=icon("dashboard"))
))
body <- dashboardBody(
tabItems(
tabItem(tabName = "Marketing",
h3("Annual Marketing Costs") ,
actionButton("switchtab", "Look at Customer Retention")),
tabItem(tabName = "CustomerRetention",
h3("Annual Costs Spent on Existing Customers"))))
ui <- dashboardPage(skin="blue",
header,
sidebar,
body
)
server <- function(input, output) {
server <- function(input, output, session) {
observeEvent(input$switchtab, {
updateTabItems(session, "tabs", "CustomerRetention")
})
}
}
shinyApp(ui = ui, server = server)
you have duplicated the server function. Try after removing the outer server function which is not required.
1 Like
sueb
November 9, 2018, 11:12am
6
sorry silly error, i haven't done that in my original script, would you mind if I post it for you to have a quick look at?
1 Like
sueb
November 9, 2018, 11:23am
7
I've done it!!!
I added my script to your working code one bit at a time and the only thing that was different was that the observe event for the action button was first rather than in the middle of the server function.
Thank you for your help
1 Like
mara
November 12, 2018, 2:33pm
8
If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).
Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.
If your question has been answered, don't forget to mark the solution!
How do I mark a solution?
Find the reply you want to mark as the solution and look for the row of small gray icons at the bottom of that reply. Click the one that looks like a box with a checkmark in it:
[image]
Hovering over the mark solution button shows the label, "Select if this reply solves the problem". If you don't see the mark solution button, try clicking the three dots button ( ••• ) to expand the full set of options.
When a solution is chosen, the icon turns green and the hover label changes to: "Unselect if this reply no longer solves the problem". Success!
[solution_reply_author]
…
Thanks
1 Like