Search code examples
rggplot2shinyreticulate

Call 2 functions on R Shiny dashboard fluidrow()


I am trying to get multiple graphs or tables to show up on a single tab on my R Shiny Dashboard. The tables and graphs are dataframes created from different functions from a python file that the R file calls on. When put individually the functions run perfectly. However, when I try to put 2 graphs or tables together on the same tab, nothing renders. I am wondering what I am doing wrong or if there's a better way to go about putting 2 graphs on a single tab. The difference between the 2 images attached is in tabitem("table) I add a comma and uncomment the box() function.Table and plot trying to be renderedPlot commented out

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)

Solution

  • You cannot render and element with the same id again on a different tab, you will need to create it under a different name:

    UI

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

    Server

       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
        })