【问题标题】:Display two dynamic plotly and highcharter plots based on condition in a shiny dashboard在闪亮的仪表板中根据条件显示两个动态绘图和高图表
【发布时间】:2019-10-04 12:07:43
【问题描述】:

我有下面闪亮的仪表板,最初您可以在其中看到selecctinput()。当用户选择返回分析时,会创建第二个selectInput()。对于前两个选择中的每一个,应显示不同的图。问题是我需要显示的图显示在顶部,而现在 highcharter 图显示在底部。

#ui.r
library(shiny)
library(shinydashboard)
library(plotly)
library(highcharter)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    selectInput("sec","Page",choices=c("Introduction","Return Analysis"),selected = "Introduction"),
    uiOutput("tab")

  ),
  dashboardBody(
    plotlyOutput("plot"),
    highchartOutput("hc2")
  )
)
#server.r
server <- function(input, output) { 
  output$tab<-renderUI({
    if(input$sec=="Introduction"){
      return(NULL)
    }
    else if(input$sec=="Return Analysis"){

      selectInput("tb", "Tabs",choices=c("Monthly Returns","Monthly Returns(Histogram)","Annual Returns"),selected = "Monthly Returns")
    }
  })

  output$plot <- renderPlotly({
    if(input$sec=="introduction"){
      return(NULL)
    }
    else if(input$tb=="Monthly Returns"){
      plot_ly(mtcars, x = ~mpg, y = ~wt)

    }
    else{
      return(NULL)
    }
  })
  output$hc2 <- renderHighchart({
    if(input$sec=="Introduction"){
      return(NULL)
    }
    else if(input$tb=="Monthly Returns(Histogram)"){
      data(diamonds, mpg, package = "ggplot2")

      hchart(mpg, "scatter", hcaes(x = displ, y = hwy, group = class))
    }

    else{
      return(NULL)
    }
  })

  }

【问题讨论】:

  • 如果你把highchartOuptut放在plotlyOutput之前,那么高图就会出现在顶部。您在应用中同时使用plotlyhighchart 是否有特殊原因?如果您使用一个包,您可以通过一个 *Output 调用来渲染两个不同的图。
  • 是的,我需要用 plotly 和 highcharter 来做。这个可以吗?
  • 猜猜我会使用相同的包,那将如何工作?
  • 如果你有一个输出容器,那么同样的方法,加上一个额外的if 语句应该可以工作。您只是在绘图类型之间切换,所有绘图类型都是由例如renderPlotly 创建并发送到相同的plotlyOutput
  • 好的,但让我印象深刻的是,2 个包没有解决方案

标签: r shiny


【解决方案1】:

这是一个同时使用highchartplotly 的解决方案。我们有一个 renderUI 将图表发送到 UI 和一个 reactive 监控用户输入并生成绘图和 *Output 处理程序。

#ui.r
library(shiny)
library(shinydashboard)
library(plotly)
library(highcharter)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    selectInput(
      "sec",
      "Page",
      choices = c("Introduction", "Return Analysis"),
      selected = "Introduction"
    ),
    uiOutput("tab")
  ),
  dashboardBody(uiOutput("various_plots"))
)

#server.r
server <- function(input, output) {
  output$tab <- renderUI({
    if (input$sec == "Return Analysis") {
      selectInput(
        "tb",
        "Tabs",
        choices = c(
          "Monthly Returns",
          "Monthly Returns(Histogram)",
          "Annual Returns"
        ),
        selected = "Monthly Returns"
      )
    }
  })

  what_to_render <- reactive({
    if (req(input$tb) == "Monthly Returns(Histogram)") {
      output$plot2 <- renderHighchart({
        hchart(mpg, "scatter", hcaes(
          x = displ,
          y = hwy,
          group = class
        ))
      })
      x <- highchartOutput("plot2")
    }

    if (req(input$tb) == "Monthly Returns") {
      output$plot1 <- renderPlotly({
        plot_ly(mtcars, x = ~ mpg, y = ~ wt)
      })
      x <- plotlyOutput("plot1")
    }

    return(x)
  })

  output$various_plots <- renderUI({
    what_to_render()
  })
}

shinyApp(ui, server)

【讨论】:

  • 我接受 tnx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 2018-05-18
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多