Home:ALL Converter>share some ui between tabs with different output on shiny

share some ui between tabs with different output on shiny

Ask Time:2017-11-10T23:28:09         Author:student

Json Formatter

I would like to have a ui element for example a radio button on one tab of a shiny app be "the same" or linked as the ui on a different tab. There potentially be other tabs that do not share that ui element.

For example, I want when I select distance in the plot tab to have it also selected when I go to the summary tab in the code below.

library(markdown)
library(shiny)
ui <- navbarPage("Navbar!",
           tabPanel("Plot", sidebarLayout(sidebarPanel(
              radioButtons("yaxis1", "y-axis", c("speed"="speed", "dist"="dist"),
                                     selected = "speed"
                        )),
                      mainPanel( plotOutput("plot") ))),

           tabPanel("Summary", sidebarLayout(sidebarPanel(
              radioButtons("yaxis2", "y-axis", c("speed"="speed", "dist"="dist")
                        )),
                      mainPanel(verbatimTextOutput("summary"))))
)
# Server ------------------------------------------
server <- function(input, output, session) {
  output$plot <- renderPlot({ plot(cars[['speed']], cars[[input$yaxis1]]) })
  output$summary <- renderPrint({ summary(cars[[input$yaxis2]]) })
}
shinyApp(ui, server)

You may run the app with:

shiny::runGist('c8d5131c4e903817316e23e98cf088f9')

The following question links the tabs, but with hyperlinks:

R shiny build links between tabs

It's also kind of the opposite of the following:

How to use the same output binding on different tab panels in shiny

Author:student,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47226158/share-some-ui-between-tabs-with-different-output-on-shiny
Nate :

You can use the function observe({}) to link your radioButtons. It will create javascript that watches for changes to the input$variables you reference and then performs an action, like updating the other button choice to match.\n\nlibrary(shiny)\n# UI ----------------------------------------------------------\nui <- navbarPage(\"Navbar!\",\n tabPanel(\"Plot\", sidebarLayout(sidebarPanel(\n radioButtons(\"yaxis1\", \"y-axis\", c(\"speed\"=\"speed\", \"dist\"=\"dist\"),\n selected = \"speed\"\n )),\n mainPanel( plotOutput(\"plot\"),\n textOutput(\"test2\")))), # for input checking\n\n tabPanel(\"Summary\", sidebarLayout(sidebarPanel(\n radioButtons(\"yaxis2\", \"grouping-var\", c(\"speed\"=\"speed\", \"dist\"=\"dist\")\n )),\n mainPanel(\n verbatimTextOutput(\"summary\"),\n textOutput(\"test1\")\n )))\n)\n# Server ------------------------------------------\nserver <- function(input, output, session) {\n\n observe({\n x <- input$yaxis1\n updateRadioButtons(session, \"yaxis2\", selected = x)\n })\n\n observe({\n y <- input$yaxis2\n updateRadioButtons(session, \"yaxis1\", selected = y)\n })\n\n output$test1 <- renderPrint({cat(\"yaxis1\", input$yaxis1)})\n output$test2 <- renderPrint({cat(\"yaxis2\", input$yaxis2)})\n output$plot <- renderPlot({ plot(cars[['speed']], cars[[input$yaxis1]]) })\n output$summary <- renderPrint({ summary(cars[[input$yaxis2]]) })\n}\nshinyApp(ui, server)\n",
2017-11-11T14:45:28
yy