gpt4 book ai didi

html - Plotly 不适用于 HTML 函数

转载 作者:太空宇宙 更新时间:2023-11-04 15:49:59 25 4
gpt4 key购买 nike

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

获得这个:

enter image description here

但是当我像这里一样开始在服务器端创建 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)

plotly plotly 没有出现...

enter image description here

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

最佳答案

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

enter image description here

关于html - Plotly 不适用于 HTML 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51376033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com