在 R Shiny 仪表板上调用 2 个函数 fluidrow()

Call 2 functions on R Shiny dashboard fluidrow()

我正在尝试让多个图表或 table 显示在我的 R Shiny 仪表板上的单个选项卡上。 tables 和图形是由 R 文件调用的 python 文件的不同函数创建的数据帧。当单独放置函数时 运行 完美。但是,当我尝试将 2 个图形或 table 放在同一个选项卡上时,没有任何渲染。我想知道我做错了什么,或者是否有更好的方法将 2 个图表放在一个选项卡上。附加的 2 张图片之间的区别在于 tabitem("table) 我添加了一个逗号并取消注释 box() 函数。

library(shinydashboard)
library(shiny)
library(ggplot2)
library(DT)
library(reticulate) #so we can run some python stuff

ui <- dashboardPage(
  dashboardHeader(title= "Stocks"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Iris", tabName = "iris", icon = icon("tree")),
      menuItem("Table", tabName = "table", icon = icon("table")),
      menuItem("Graph", tabName = "graph", icon = icon("bar-chart-o"))
    )
  ),
  dashboardBody(

tabItems(
  tabItem("iris",
          #the iris page
          box(plotOutput("correlation_plot"), width = 6),
          box(
            #input tab to select what axis you'd like to compare to
            selectInput("features", "Features:", 
                        c("Sepal.Width", "Petal.Length",
                          "Petal.Width")), width = 4
          ),

          textOutput("textOutput")
  ), 
  tabItem("table",
         fluidPage(
           fluidRow(
             box(title = "Watchlist", dataTableOutput("watchtable"))
             #box(title = "Top Performer", plotOutput("correlation_plot"))
           )
         )
  ),
  tabItem("graph",
          #the graph page
          h1("Top Performer"),  
          box(plotOutput("best_plot"), width = 6)
      )
    )
  )
)
#python file to run
source_python("trade.py")

server <- function(input, output){
  output$correlation_plot <- renderPlot({
    #make plot and allow y axis to be altered by features tab
     plot(iris$Sepal.Length, iris[[input$features]],
          xlab= "Sepal.Length", ylab = "Feature")
  })

  output$watchtable <- renderDataTable({
    df = watchMethod()
    })

  output$best_plot <- renderPlot({
    #make plot and allow y axis to be altered by features tab
    df1 = topPerformer()
    #plot line graph of data and get rid of time metrics
    ggplot(data=df1, aes(x=Time, y=Value)) +
      geom_line(color="green") + theme(legend.position = "none",
                          axis.text.x = element_blank())
  })
}

shinyApp(ui, server)

您不能在不同的选项卡上再次呈现具有相同 ID 的元素,您需要使用不同的名称创建它:

UI

    tabItem("table",
            fluidPage(
                fluidRow(
                    box(title = "Watchlist", dataTableOutput("watchtable"))
                    box(title = "Top Performer", plotOutput("correlation_plot2"))
                )
            )
    ),

服务器

   output$correlation_plot <- renderPlot({
        plot(iris$Sepal.Length, iris[[input$features]],
             xlab= "Sepal.Length", ylab = "Feature")
    })

    output$correlation_plot2 <- renderPlot({
        plot(iris$Sepal.Length, iris[[input$features]],
             xlab= "Sepal.Length", ylab = "Feature")
    })

    output$watchtable <- renderDataTable({
        iris
    })