Home > OS >  Shiny app displays plot only in one tab. What is the reason?
Shiny app displays plot only in one tab. What is the reason?

Time:12-01

At the moment I am learning shiny to visualize forecast data versus original data in a web application. As I want to have separate tabs for original and forecast data, I am using a navbar Panel.

In the given reprex, both dataframes containing the same data, because it is meant for technical illustration only.

Currently I am struggling to display the plot2 data representing the forecast plot. It has to do with wrong syntax I guess. Otherwise it could be a html issue.

Please help me out here:

library(shiny)
library(ggplot2)
library(dplyr)
library(hrbrthemes)
library(shinythemes)

dates <- ymd("2016-01-01")  months(0:59)
fake_values <- 
  c(661,678,1094,1987,3310,2105,1452,983,1107)

replicate <- rep(1,60) %*% t.default(fake_values)
replicate <- as.data.frame(replicate)

df <- bind_cols(replicate, dates) %>%
  rename(c(dates = ...10))

## melt it down
data <- reshape2::melt(df, id.var='dates')
data$variable <- as.character(data$variable)
data$dates <- as.Date(data$dates)
data2 <- reshape2::melt(df, id.var='dates')
data2$variable <- as.character(data2$variable)
data2$dates <- as.Date(data2$dates)


#### UI

ui <- 
  navbarPage(
    
    title="Zeitreihenvorhersage",
    theme=shinytheme("spacelab"),
    inverse=TRUE,
    
    # first tab
    tabPanel(
      "Original Zeitreihe",
      fluidPage(
        sidebarPanel(
          selectInput(
            inputId = "variable",
            label = "Zeitreihe selektieren",
            choices = unique(data$variable),
            selected = "V1")
        ),
        mainPanel(
          plotOutput("plot", click = "plot_click"),
          verbatimTextOutput("info")
        )
      )
    ),
    
    # second tab
    tabPanel(
      "Forecast",
      fluidPage(
        sidebarPanel(
          selectInput(
            inputId = "variable",
            label = "Zeitreihe selektieren",
            choices = unique(data2$variable),
            selected = "V6")
            ),
      mainPanel(
        plotOutput("plot2", click = "plot_click2"),
        verbatimTextOutput("info2")
      )
    )
  )
)



#### SERVER

server <- function(input, output,  session) {
  
  output$plot <- renderPlot({
    data %>%
      filter(variable == input$variable) %>%
      ggplot(aes(dates, value, group = 1))  
      geom_line( color="steelblue", size = 1.2)   
      geom_point(size = 2.5)  
      xlab("")  
      ylab("Absatzmenge")  
      scale_x_date(date_breaks = "2 months")   
      theme_bw()   
      theme(
        panel.border = element_blank(),
        axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)
      )
  }
  )
  output$info <- renderText({
    paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
  }
  )
  #}
  
  
  output$plot2 <- renderPlot({
    data2 %>%
      filter(variable == input$variable) %>%
      ggplot(aes(dates, value, group = 1))  
      geom_line( color="steelblue", size = 1.2)  
      geom_point(size = 2.5)  
      xlab("")  
      ylab("Absatzmenge")  
      scale_x_date(date_breaks = "2 months")   
      theme_bw()  
      theme(
        panel.border = element_blank(),
        axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)
      )
    output$info2 <- renderText({
      paste0("x=", input$plot_click2$x, "\ny=", input$plot_click2$y)
    }
    )
  }
  ) 
}

runApp(list(ui = ui, server = server),host="127.x.x.x",port=9999, launch.browser = TRUE)

CodePudding user response:

You have missed to close the second renderPlot before render output$info2 - as you can see, there is no closing curly bracket before output$info2.

This is improved version:

output$plot2 <- renderPlot({
    data2 %>%
      filter(variable == input$variable) %>%
      ggplot(aes(dates, value, group = 1))  
      geom_line( color="steelblue", size = 1.2)  
      geom_point(size = 2.5)  
      xlab("")  
      ylab("Absatzmenge")  
      scale_x_date(date_breaks = "2 months")   
      theme_bw()  
      theme(
        panel.border = element_blank(),
        axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1)
      )
  })
    output$info2 <- renderText({
      paste0("x=", input$plot_click2$x, "\ny=", input$plot_click2$y)
    }
    )
  • Related