【问题标题】:Update ggplotly plot's subtitle based on shiny widget inputs根据闪亮的小部件输入更新 ggplotly 绘图的字幕
【发布时间】:2021-11-03 17:42:51
【问题描述】:

我需要根据shiny 小部件输入在我的shiny 应用程序中更新ggplotly() 绘图的字幕。但是当我尝试将它放在文本中时,它被隐藏了。

library(shiny)
library(plotly)
library(ggplot2)

ui <- fluidPage(
  fluidRow(
    
    shinyWidgets::checkboxGroupButtons(inputId = "intervals_A", label = "Bins:",
                                       choices = c("(0.0, 10.0]", "(10.0, 70.0]", "(70.0, 330.0]", "(330.0, inf]"), selected = c("(0.0, 10.0]", "(10.0, 70.0]", "(70.0, 330.0]", "(330.0, inf]"),  justified = TRUE, checkIcon = list(yes = icon("ok", lib = "glyphicon")))
  ),
  fluidRow(plotlyOutput("plot"))
)

server <- function(input, output) {
  output$plot<-renderPlotly({
    
    p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
      geom_boxplot()
    p <- p + labs(title = "Effect of Vitamin C on Tooth Growth",
                  subtitle = "Plot of length by dose"
                  )
    ggplotly(p)%>% 
      layout(title = list(text = paste0('Effect of Vitamin C on Tooth Growth"',
                                        '<br>',
                                        '<sup>',
                                        'Plot of length by'),paste0(input$intervals_A) ,paste0('interval','</sup>')))
  })
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 shiny ggplotly


    【解决方案1】:

    编写一个全局paste 作为包装器。为了粘贴多个选择,我为input$intervals_A添加了逗号

    library(shiny)
    library(plotly)
    library(ggplot2)
    
    ui <- fluidPage(
        fluidRow(
            
            shinyWidgets::checkboxGroupButtons(inputId = "intervals_A", label = "Bins:",
                                               choices = c("(0.0, 10.0]", "(10.0, 70.0]", "(70.0, 330.0]", "(330.0, inf]"), selected = c("(0.0, 10.0]", "(10.0, 70.0]", "(70.0, 330.0]", "(330.0, inf]"),  justified = TRUE, checkIcon = list(yes = icon("ok", lib = "glyphicon")))
        ),
        fluidRow(plotlyOutput("plot"))
    )
    
    server <- function(input, output) {
        output$plot<-renderPlotly({
            
            p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
                geom_boxplot()
            p <- p + labs(title = "Effect of Vitamin C on Tooth Growth",
                          subtitle = "Plot of length by dose"
            )
            ggplotly(p)%>% 
                layout(title = list(text = paste(paste0('Effect of Vitamin C on Tooth Growth"',
                                                  '<br>',
                                                  '<sup>',
                                                  'Plot of length by'),paste0(input$intervals_A,collapse = ',') ,paste0('interval','</sup>'))))
        })
        
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2021-01-14
      • 2020-06-23
      • 2021-04-18
      • 1970-01-01
      • 2016-12-04
      相关资源
      最近更新 更多