简体   繁体   中英

ggplotly in shiny app keeps crashing (Rstudio)

I am trying to render a ggplotly in a shiny app, but my code doesn't even run. Any help is highly appreciated! Where am I making a mistake? I am very new to R. See my code below.

**

ui <- fluidPage(  
titlePanel("Plotly"),
sidebarLayout(
sidebarPanel(),
mainPanel(
  plotlyOutput("plot2"))))

server <- function(input, output) { output$plot2 <- renderPlotly(print(
  ggplotly(
    q<-ggplot(plotdata,aes(x=DateValue, y=Count, col=Country, 
                                 group=1
                                 
                                 ))+
  geom_line(aes(text=paste
                ( '<br>Number of Launches:', Count, 
                  '<br>Country:', Country)
                
                   )) 
    +
  scale_colour_manual(values = c('yellow','orange','turquoise1','mediumspringgreen','tan3','indianred4','plum1','slateblue2','violetred2','greenyellow','blue1','black','firebrick2'))+
  theme( axis.line = element_line(colour = "black", 
                      size = 1, linetype = "solid"))+
scale_x_continuous(breaks=c(1945,1955,1965,1975,1985,1995,2005,2015,2025))+
scale_y_continuous(breaks=c(0,20,40,60,80,100,120))+
theme_bw()+
guides(colour = guide_legend(override.aes = list(shape = 3))) +
labs(title="Space Launches per Year",
subtitle="Colored by country",
caption="Source: ...",
 y="Number of Launches",
x="Year")),
ggplotly(q, tooltip = "text"))}}

**

Change your server code as shown below and ensure that you have a dataframe named plotdata to plot. Initial print statement was unnecessary. Also, you were calling ggplotly inside ggplotly.

server <- function(input, output) { 
  output$plot2 <- renderPlotly({ 
    q<-ggplot(plotdata,aes(x=DateValue, y=Count, col=Country, group=1))+
      geom_line(aes(text=paste
                    ( '<br>Number of Launches:', Count, 
                      '<br>Country:', Country))) +
    
      scale_colour_manual(values = c('yellow','orange','turquoise1','mediumspringgreen','tan3','indianred4','plum1','slateblue2','violetred2','greenyellow','blue1','black','firebrick2'))+
      theme( axis.line = element_line(colour = "black", 
                                      size = 1, linetype = "solid"))+
      scale_x_continuous(breaks=c(1945,1955,1965,1975,1985,1995,2005,2015,2025))+
      scale_y_continuous(breaks=c(0,20,40,60,80,100,120))+
      theme_bw()+
      guides(colour = guide_legend(override.aes = list(shape = 3))) +
      labs(title="Space Launches per Year",
           subtitle="Colored by country",
           caption="Source: ...",
           y="Number of Launches",
           x="Year")
    
  ggplotly(q, tooltip = "text")
  })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM