【问题标题】:Plotly not working properly within R shiny App在 R Shiny App 中无法正常工作
【发布时间】:2021-10-31 04:25:57
【问题描述】:

我试图在单击按钮后显示一个绘图图表,这意味着该图表只会在单击按钮时显示,但该图表只是没有显示......这有什么线索吗?代码如下:

shinyApp(
  ui = fluidPage(
    sidebarLayout(
      sidebarPanel(
        actionButton("button", "Show Chart")
      ),
      mainPanel(
        plotOutput("bar")
      )
    )
  ),
  server = function(input, output) {
    observeEvent(input$button, {
      category <- c('task1', 'task2', 'task2','task1','task1')
      start_min <- c(0, 0, 16, 45, 40)
      stop_min <- c(14.9,18.8,17.5,65.5, 70)
      group <- c('A', 'B', 'A', 'A', 'B')
      data <- data.frame(category,start_min,stop_min,group)
      
      task_bars <- ggplot(data, mapping=aes(ymin=0, ymax=1,
                                            xmin=start_min, xmax=stop_min,
                                            fill=as.factor(category),
                                            text=paste("Task:", str_wrap(string = category, width = 70,),
                                                       "<br>Start: ", format(start_min, digits=1), "min", 
                                                       "<br>Stop: ", format(stop_min, digits=1), "min")
      )) +
        geom_rect(alpha=0.8) +
        theme_minimal()+
        theme(
          axis.title.x=element_text(color="white"), axis.text.x=element_text(color="white"),
          axis.text.y=element_blank(), axis.ticks.y=element_blank(),
          panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
          panel.border = element_blank(), panel.background = element_blank()) +
        scale_fill_discrete(breaks=data$category)
      
      task_bars <- plotly::ggplotly(task_bars, tooltip="text", width = 970, height = 120) %>%
        plotly::config(displayModeBar = TRUE) %>% 
        plotly::layout(plot_bgcolor='black', paper_bgcolor='black', margin = list(b=30, l=0, r=10, t=30))
      
      print(task_bars)
      output$bar <- renderPlotly(task_bars)
    })
  }
)

【问题讨论】:

    标签: r plotly shinyapps


    【解决方案1】:

    您需要将output$bar 移出observeEvent。我为您的绘图创建了一个 reactiveValues 对象。 observeEvent 创建绘图,然后将其显示在输出中。

    您还在 ui 中使用 plotOutput 而不是在 ui 中使用 plotlyOutput。

    library(shiny)
    library(ggplot2)
    library(plotly)
    
    task_bars <- reactiveValues(plot = NULL)
    
    shinyApp(
      ui = fluidPage(
        sidebarLayout(
          sidebarPanel(
            actionButton("button", "Show Chart")
          ),
          mainPanel(
            plotlyOutput("bar")
          )
        )
      ),
      server = function(input, output) {
        observeEvent(input$button, {
          category <- c('task1', 'task2', 'task2','task1','task1')
          start_min <- c(0, 0, 16, 45, 40)
          stop_min <- c(14.9,18.8,17.5,65.5, 70)
          group <- c('A', 'B', 'A', 'A', 'B')
          data <- data.frame(category,start_min,stop_min,group)
          
          task_bars$plot <- ggplot(data, mapping=aes(ymin=0, ymax=1,
                                                xmin=start_min, xmax=stop_min,
                                                fill=as.factor(category)
          )) +
            geom_rect(alpha=0.8) +
            theme_minimal()+
            theme(
              axis.title.x=element_text(color="white"), axis.text.x=element_text(color="white"),
              axis.text.y=element_blank(), axis.ticks.y=element_blank(),
              panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
              panel.border = element_blank(), panel.background = element_blank()) +
            scale_fill_discrete(breaks=data$category)
    
        })
          
        output$bar <- renderPlotly({
    if(input$button == 0){
    return()
    } else {
          print(plotly::ggplotly(task_bars$plot, tooltip="text", width = 970, height = 120) %>%
            plotly::config(displayModeBar = TRUE) %>% 
            plotly::layout(plot_bgcolor='black', paper_bgcolor='black', margin = list(b=30, l=0, r=10, t=30))
          )
    }
        })
      }
    )
    

    【讨论】:

      猜你喜欢
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 2022-01-10
      相关资源
      最近更新 更多