【问题标题】:Animated Bar plot not working/displaying on R shiny Web App动画条形图在 R 闪亮的 Web 应用程序上不起作用/显示
【发布时间】:2021-09-10 00:33:08
【问题描述】:

我想在 R 闪亮的网络应用程序中显示我的动画条形图。我在 Rstudio 查看器窗格中获得了我的动画情节,但不幸的是,它在应用程序中不起作用

代码:

# LIBRARIES
library(ggplot2)
library(shiny)
library(gganimate)

# LOADING CSV
undergradDATA <- read.csv(file="1-10 Undergraduates.csv", head=TRUE, sep=",")

# UI
ui <- fluidPage (
  mainPanel(
    plotOutput("myplot")
  )
)

# SERVER
server <- function(input, output) {
  output$plot <- renderPlot({
    undergrad_plot <- ggplot(data=undergradDATA , aes(x=HEI, y=Undergrad), height = 461, width = 644) +
      geom_bar(stat='identity', fill="darkolivegreen3", colour="white")
    undergrad_plot + ggtitle("Ranking of Top 10 HEI's w.r.t Undergraduates") +
      theme(
        plot.title = element_text(hjust = 0.5,
                                  colour = "darkolivegreen",
                                  size = 17,
                                  family = "courier"
        ) )
    undergrad_plot + transition_states(Undergrad, wrap = FALSE) +
      shadow_mark() +
      enter_grow() +
      enter_fade()

  })
}

# RUNNING APP
shinyApp(UI, server)

【问题讨论】:

    标签: r ggplot2 shiny gganimate


    【解决方案1】:

    除了你的代码中的一些小缺陷(它应该是“plot”而不是“myplot”和“ui”而不是“UI”,主要问题是你不能通过renderPlot渲染和输出动画图和plotOutput。相反,您必须使用renderImage 和imageOutput。

    改编此answer 并利用从ggplot2::mpg 创建的一些示例数据:

    library(ggplot2)
    library(shiny)
    library(gganimate)
    library(dplyr)
    
    ## Example data
    undergradDATA <- mpg %>%
      count(class, sort = TRUE) %>%
      mutate(class = reorder(class, n)) %>%
      rename(Undergrad = class, HEI = n)
    
    ui <- fluidPage(
      mainPanel(imageOutput("plot"))
    )
    
    server <- function(input, output) {
      output$plot <- renderImage(
        {
          undergrad_plot <- ggplot(data = undergradDATA, aes(x = HEI, y = Undergrad), height = 461, width = 644) +
            geom_bar(stat = "identity", fill = "darkolivegreen3", colour = "white")
          undergrad_plot + ggtitle("Ranking of Top 10 HEI's w.r.t Undergraduates") +
            theme(
              plot.title = element_text(
                hjust = 0.5,
                colour = "darkolivegreen",
                size = 17,
                family = "mono"
              )
            )
    
          anim <- undergrad_plot +
            transition_states(Undergrad, wrap = FALSE) +
            shadow_mark() +
            enter_grow() +
            enter_fade()
    
          anim_save("outfile.gif", animate(anim)) # New
    
          # Return a list containing the filename
          list(src = "outfile.gif", contentType = "image/gif")
        },
        deleteFile = TRUE
      )
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 谁能帮我把这个条形图转换成散点图/折线图?
    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 2018-07-14
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多