[英]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.