首页 > 解决方案 > Plotly 不适用于 HTML 函数

问题描述

我正在处理一个非常复杂的闪亮应用程序,我想在其中创建一个服务器函数内的 UI 输出。UI 不是那么容易,它依赖于在服务器端创建的许多项目,所以我正在创建它连接 UI 的 HTML 部分。一切正常,直到我遇到plotly图表。

我创建了一个更简单的应用程序版本,以便更容易理解我的问题。

通常我会这样做:

library("shiny")
library("plotly")
library("dplyr")

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
        ),

        mainPanel(
            plotlyOutput("distPlot1"),
            plotOutput("distPlot2")
        )
    )
)

server <- function(input, output) {

    output$distPlot1 <- renderPlotly({

        x <- faithful[, 2]

        plot_ly(x = x, type = "histogram")
    })

    output$distPlot2 <- renderPlot({

        x <- faithful[, 2]

        hist(x)
    })

}

shinyApp(ui = ui, server = server)

获得这个:

在此处输入图像描述

但是当我开始像这里一样在服务器端创建 ui 时(编辑部分在 ui 中有更多 div):

library("shiny")
library("plotly")
library("dplyr")

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
        ),

        mainPanel(
            htmlOutput("ui1"),
            uiOutput("ui2")
        )
    )
)

server <- function(input, output) {

    output$distPlot1 <- renderPlotly({

        x <- faithful[, 2]

        plot_ly(x = x, type = "histogram")
    })

    output$distPlot2 <- renderPlot({

        x <- faithful[, 2]

        hist(x)
    })

    output$ui1 <- renderUI({

        show <- h1("lfkhg")
        show <- paste0(show, plotlyOutput("distPlot1") %>% as.character())

        HTML(show)

    })

    output$ui2 <- renderUI({

        show <- h1("lfkhg")
        show <- paste0(show, plotOutput("distPlot2") %>% as.character())

        HTML(show)

    })

}

# Run the application
shinyApp(ui = ui, server = server)

情节情节没有出现...

在此处输入图像描述

你知道为什么以及如何处理这个问题吗?

标签: htmlrshinyplotlyr-plotly

解决方案


我不知道你为什么需要%>% HTML()在那里,因为没有它它对我有用。另外,如果您想在其中添加更多内容,renderUI只需使用tagList并将它们组合在一起,我将h1根据您的评论添加

library("shiny")
library("plotly")
library("dplyr")

ui <- fluidPage(
  sidebarLayout(sidebarPanel(),
    mainPanel(
      uiOutput("ui1"),
      uiOutput("ui2")
    )
  )
)

server <- function(input, output) {

  output$distPlot1 <- renderPlotly({
    x <- faithful[, 2]
    plot_ly(x = x, type = "histogram")
  })

  output$distPlot2 <- renderPlot({
    x <- faithful[, 2]
    hist(x)
  })

  output$ui1 <- renderUI({
    tagList(h1("lfkhg"),plotlyOutput("distPlot1"))
  })

  output$ui2 <- renderUI({
    plotOutput("distPlot2")
  })

}

# Run the application
shinyApp(ui = ui, server = server)

在此处输入图像描述


推荐阅读