简体   繁体   English

呈现不同页面的两个 actionButton(s)

[英]two actionButton(s) that renders different pages

I have a shiny app (apologies in advance for the length but wanted to provide a reprex):我有一个闪亮的应用程序(提前道歉,但想提供一个 reprex):

library(shiny)
library(dplyr)
library(ggplot2)
library(DT)
data('mtcars')

ui <- navbarPage("Test App",
                 tabPanel("Scatter Plot",
                          fluidPage(
                            ## Side Panel
                            fluidRow(
                              column(width = 3,
                                     selectInput(inputId = "cyl", 
                                                 label = "cylender", 
                                                 choices = c(4, 6, 8), 
                                                 selected = 4), 
                                     actionButton(inputId = "load", 
                                                  label =  "Load", 
                                                  width = "100%")
                              ),
                              column(width = 8,
                                     tabPanel("Scatter Plot",
                                              plotOutput("plot1", height = "90%", width = "90%"))
                              )))),
                 tabPanel("Data Table",
                          fluidRow(column(width = 12, div(dataTableOutput("plot2"))))))

server <- function(input, output, session) {
  cars = eventReactive(eventExpr = input$load, valueExpr = {
    res =  mtcars %>% filter(cyl == input$cyl)
    return(res)  
  })
  
  getGraph = function() {
      dat = cars()
      p = ggplot(dat, aes(x = disp, y = mpg)) +
        geom_point()
      return(p)
    }
    
    getDataTable = function(input) {
      dat = cars()
      return(dat)
    }
  
  output$plot1 = renderPlot({
    getGraph()
  }, height = function(){
    session$clientData$output_plot1_width })

  output$plot2 = renderDT(
    getDataTable()
    ,
    class = "display nowrap compact", 
    filter = "top", 
    options = list(scrollX = TRUE))}


shinyApp(ui = ui, server = server)

I want to add a second button next to 'Load' called 'Load Data Table' that would take the user right to the second tab.我想在“加载”旁边添加一个名为“加载数据表”的按钮,该按钮会将用户带到第二个选项卡。 Is there any way to do this?有没有办法做到这一点?

Thanks!谢谢!

You can use updateNavbarPage to select the tabPanel you want to display.您可以使用updateNavbarPage来选择要显示的tabPanel For this, you have to include an id in your navbarPage .为此,您必须在您的navbarPage包含一个id I also changed your car reactive so that it also updates if you click on the "Load Data Table" button:我还更改了您的car反应式,以便在您单击“加载数据表”按钮时它也会更新:

library(shiny)
library(dplyr)
library(ggplot2)
library(DT)
data('mtcars')

ui <- navbarPage("Test App",
                 id = "main_navbar",
                 tabPanel("Scatter Plot",
                          fluidPage(
                            ## Side Panel
                            fluidRow(
                              column(width = 3,
                                     selectInput(inputId = "cyl", 
                                                 label = "cylender", 
                                                 choices = c(4, 6, 8), 
                                                 selected = 4), 
                                     actionButton(inputId = "load", 
                                                  label =  "Load", 
                                                  width = "100%"),
                                     actionButton(inputId = "table",
                                                  label = "Load Data Table",
                                                  width = "100%")
                              ),
                              column(width = 8,
                                     tabPanel("Scatter Plot",
                                              plotOutput("plot1", height = "90%", width = "90%"))
                              )))),
                 tabPanel("Data Table",
                          fluidRow(column(width = 12, div(dataTableOutput("plot2"))))))

server <- function(input, output, session) {
  cars = eventReactive(eventExpr = c(input$load, input$table), valueExpr = {
    res =  mtcars %>% filter(cyl == input$cyl)
    return(res)  
  },
  ignoreInit = TRUE)
  
  getGraph = function() {
    dat = cars()
    p = ggplot(dat, aes(x = disp, y = mpg)) +
      geom_point()
    return(p)
  }
  
  getDataTable = function(input) {
    dat = cars()
    return(dat)
  }
  
  output$plot1 = renderPlot({
    getGraph()
  }, height = function(){
    session$clientData$output_plot1_width })
  
  output$plot2 = renderDT(
    getDataTable()
    ,
    class = "display nowrap compact", 
    filter = "top", 
    options = list(scrollX = TRUE))
  
  observeEvent(input$table, {
    updateNavbarPage(
      session,
      inputId = "main_navbar",
      selected = "Data Table"
    )
  })
  
  }


shinyApp(ui = ui, server = server)

Edit I've added ignoreInit = TRUE to the eventReactive so that it doesn't create the data.frame on startup.编辑我已将ignoreInit = TRUE添加到eventReactive以便它不会在启动时创建 data.frame。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用两个不同的 actionButton 触发相同的图,但不重复 Shinyapp 中的整个功能? - How to trigger the same plot with two different actionButton but not repeating the whole function in shinyapp? 在两个闪亮的仪表板选项卡中带有actionButton的renderDataTable - renderDataTable with actionButton in two shinydashboard tabs 如何通过 Shinyapp 中不同条件下的操作按钮刷新 plot - How to refresh the plot by actionbutton in different conitions in shinyapp 如何使用两个单独的闪亮observeEvent两个actionButton? - How to use the two actionButton with two separate shiny observeEvent? 在闪亮时,如何在选择不同的变量时具有新的ActionButton? - In shiny, how to have a new actionButton when a different variable is selected? Shiny DataTable中的actionButton按行具有不同的单击URL - actionButton in Shiny DataTable that has a Different on-click URL by Row 如何通过闪亮的模块在数据表中使用闪亮的动作按钮? - How to use shiny's actionButton in DataTable through shiny module? actionButton 和 if 条件 - actionButton and if condition 使用其他actionLink之后,为什么我的Shiny(R)actionButton没有响应? - Why doesn't my Shiny (R) actionButton respond after I use a different actionLink? 根据每次按下任何 actionButton() 在闪亮的应用程序中显示不同的图像对 - Display different pair of images in a shiny app based on each time any actionButton() is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM