【问题标题】:share some ui between tabs with different output on shiny在闪亮的不同输出的选项卡之间共享一些 ui
【发布时间】:2018-04-23 21:10:14
【问题描述】:

我希望有一个 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

【问题讨论】:

  • 你可以使用侧边栏面板吗?
  • 不,因为侧边栏将被所有选项卡使用,而我仍然希望能够为每个选项卡自定义它。
  • @student 你可以在侧边栏中使用conditionalPanel。
  • 是的,你是对的。我认为条件面板会起作用。谢谢。

标签: r shiny


【解决方案1】:

您可以使用函数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)

【讨论】:

  • 我喜欢你的解决方案。但是我想知道有没有一种方法可以避免复制单选按钮的代码。
【解决方案2】:

将标签放在侧边栏布局中是一种方法。

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)
    ```

【讨论】:

  • 这不是我想要的,因为我想要其他不使用相同 ui 的选项卡。
猜你喜欢
  • 2023-04-11
  • 1970-01-01
  • 2023-03-13
  • 2018-04-03
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
相关资源
最近更新 更多