r-在Shiny应用程序的不同选项卡中显示不同的数据帧



我有一个超级简单的闪亮应用程序,我想展示的主要内容是在MPG选项卡中,df1的表格和绘图;在"IRIS"选项卡中,我想显示表和df2的绘图。我的主要问题是如何在服务器输出中指示我想要不同选项卡中的不同数据帧。

library(tidyverse)
library(shiny)
df1 <- mpg 
df2 <- iris
ui <- fluidPage(
tabsetPanel(
tabPanel("MPG", 
dataTableOutput("dynamic"),
plotOutput("plot", width = "400px")
), 
tabPanel("IRIS", 
dataTableOutput("dynamic"),
plotOutput("plot", width = "400px"))
)
)
server <- function(input, output, session) {

output$dynamic <- renderDataTable(mpg, options = list(pageLength = 5))
output$plot <- renderPlot(ggplot(df1) +
geom_bar(aes(x=manufacturer)) +
theme_bw())

output$dynamic <- renderDataTable(iris, options = list(pageLength = 5))
output$plot <- renderPlot(ggplot(df2) +
geom_bar(aes(x=Species)) +
theme_bw())
}
shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:4563

感谢大家的评论!作为参考,这里有一个基于你的建议的答案。

library(tidyverse)
library(shiny)
df1 <- mpg 
df2 <- iris
ui <- fluidPage(
tabsetPanel(
tabPanel("MPG", 
dataTableOutput("table_one"),
plotOutput("plot_one", width = "400px")
), 
tabPanel("IRIS", 
dataTableOutput("table_two"),
plotOutput("plot_two", width = "400px"))
)
)
server <- function(input, output, session) {

output$table_one <- renderDataTable(mpg, options = list(pageLength = 5))
output$plot_one <- renderPlot(ggplot(df1) +
geom_bar(aes(x=manufacturer)) +
theme_bw())

output$table_two <- renderDataTable(iris, options = list(pageLength = 5))
output$plot_two <- renderPlot(ggplot(df2) +
geom_bar(aes(x=Species)) +
theme_bw())
}
shinyApp(ui, server)

最新更新