【问题标题】:Plotly does not work in shiny with HTML functionPlotly 不适用于 HTML 函数
【发布时间】:2018-12-24 20:09:36
【问题描述】:

我正在处理一个非常复杂的闪亮应用程序,我想在其中创建一个服务器函数内的 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)

情节情节没有出现...

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

【问题讨论】:

    标签: html r shiny plotly r-plotly


    【解决方案1】:

    我不知道你为什么需要%&gt;% 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)
    

    【讨论】:

    • 你能解释一下为什么你需要%&gt;% as.character() %&gt;% HTML()来换ui2吗?
    • 你也不需要,我删除了它
    • 因为当我需要这个 UI 部分也包含例如 h1 时,我需要做这样的事情:show &lt;- h1("lfkhg") show &lt;- paste0(show, plotlyOutput("distPlot1") %&gt;% as.character()) HTML(show)。没有这个 as.character()HTML() 它将无法工作。我只需要粘贴许多较小的部分即可获得此 UI。我想让这个例子变得简单,所以它可能看起来很奇怪。
    • @Marta 不在原始示例中,所以我仍然不清楚。您想在renderUI 中添加更多divs 吗?
    • 是的,我想要。我可以编辑我的问题。我只是想让它简单一点,对此感到抱歉。
    猜你喜欢
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2019-12-31
    • 2017-03-18
    • 1970-01-01
    相关资源
    最近更新 更多