r - 呈现不同页面的两个 actionButton(s)
问题描述
我有一个闪亮的应用程序(提前道歉,但想提供一个代表):
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)
我想在“加载”旁边添加一个名为“加载数据表”的第二个按钮,它将用户带到第二个选项卡。有没有办法做到这一点?
谢谢!
解决方案
您可以使用updateNavbarPage
来选择tabPanel
要显示的内容。为此,您必须id
在navbarPage
. 我还更改了您的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)
编辑
我已添加ignoreInit = TRUE
到,eventReactive
以便它不会在启动时创建 data.frame。
推荐阅读
- elasticsearch - 进行搜索时是否可以将功能应用于字段?
- asp.net - 如何将具有完整 .Net 框架的 MVC 项目迁移到 Microsoft.Net.Sdk 项目?
- python - How to use lists as input?
- python - Pandas,根据条件在另一列中搜索结果
- html - Why google fonts not working after uploading site?
- sql - 如何简化和扩展 Firebase 数据库上的 BigQuery 子过滤 event_params
- vue.js - Vue.js 延迟加载自定义库组件
- ios - 如何摆脱“CocoaPods 找不到 pod 的兼容版本”?我尝试更新 cocoapod 和 pod repo
- android - 从后台读取的 socker 在 WebView 中写入文本
- c# - 如何在存储库中注入 IServiceProvider 来实现工作单元模式?