我想要一个ui元素,例如闪亮应用的一个标签上的单选按钮是"相同的"或在另一个标签上链接为ui。可能还有其他标签不共享该ui元素。
例如,当我在下面的代码中进入摘要选项卡时,我想在绘图选项卡中选择距离时也选择它。
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)
您可以使用以下命令运行该应用程序:
shiny::runGist('c8d5131c4e903817316e23e98cf088f9')
以下问题链接标签,但有超链接:
R shiny build links between tabs
它也与以下情况相反:
How to use the same output binding on different tab panels in shiny
答案 0 :(得分:3)
您可以使用observe({})
功能链接radioButtons
。它将创建javascript,监视您引用的input$variables
的更改,然后执行操作,例如更新其他按钮选项以匹配。
library(shiny)
# UI ----------------------------------------------------------
ui <- navbarPage("Navbar!",
tabPanel("Plot", sidebarLayout(sidebarPanel(
radioButtons("yaxis1", "y-axis", c("speed"="speed", "dist"="dist"),
selected = "speed"
)),
mainPanel( plotOutput("plot"),
textOutput("test2")))), # for input checking
tabPanel("Summary", sidebarLayout(sidebarPanel(
radioButtons("yaxis2", "grouping-var", c("speed"="speed", "dist"="dist")
)),
mainPanel(
verbatimTextOutput("summary"),
textOutput("test1")
)))
)
# Server ------------------------------------------
server <- function(input, output, session) {
observe({
x <- input$yaxis1
updateRadioButtons(session, "yaxis2", selected = x)
})
observe({
y <- input$yaxis2
updateRadioButtons(session, "yaxis1", selected = y)
})
output$test1 <- renderPrint({cat("yaxis1", input$yaxis1)})
output$test2 <- renderPrint({cat("yaxis2", input$yaxis2)})
output$plot <- renderPlot({ plot(cars[['speed']], cars[[input$yaxis1]]) })
output$summary <- renderPrint({ summary(cars[[input$yaxis2]]) })
}
shinyApp(ui, server)
答案 1 :(得分:-1)
将标签放在侧边栏布局中是一种方式。
ui <- fluidPage(sidebarLayout(
sidebarPanel(radioButtons("yaxis1", "y-axis",
c("speed"="speed", "dist"="dist"),selected = "speed"
)),
mainPanel(tabsetPanel(
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary"))
)
)
)
)
# Server ------------------------------------------
server <- function(input, output, session) {
output$plot <- renderPlot({ plot(cars[['speed']],
cars[[input$yaxis1]]) })
output$summary <- renderPrint({ summary(cars[[input$yaxis1]])
})
}
shinyApp(ui, server)
```