我有一个显示指标数据的应用程序。每个指标(30+)都有一个定义。我们在单个html文件中定义了所有指标(比30个不同的指标更容易维护)。
在我的ShinyApp中,我只想显示与所选指标相关的部分,而不是整个文档。
我想知道怎么才能做到这一点...
下面是一个示例,它只显示了完整的“文档”,而不是在侧边栏中选择的部分:
documentation <- structure("<div> <div id=\"part1\"> <h1>Part 1</h1> <p>This is part 1 out of 2</p> <p> </p> </div> <div id=\"part2\"> <p> </p> <h1>Part 2</h1> <p>This is part 2 out of 2.</p> </div> </div>", html = TRUE, class = c("html", "character"))
documentation
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = "part",label = "Select the part you want to see", choices = c("part1", "part2"))
),
dashboardBody(
uiOutput("section")
)
)
server <- function(input, output) {
output$section <- renderUI({
HTML(documentation) # This needs subsetting based on input$part
})
}
shinyApp(ui, server)
发布于 2019-05-16 01:45:32
我想出了一个又快又脏的解决方案,就是将HTML作为字符串进行操作。我想知道是否有更优雅的方法...
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = "part",label = "Select the part you want to see", choices = c("part1", "part2"))
),
dashboardBody(
uiOutput("section")
)
)
server <- function(input, output) {
output$section <- renderUI({
# HTML(documentation)
t1 <- strsplit(documentation, "<div")[[1]][grepl(input$part, strsplit(documentation, "<div")[[1]])]
t2 <- paste0("<div", t1)
t2 <- gsub("</div> </div>", "</div>", t2)
HTML(t2)
})
}
shinyApp(ui, server)
https://stackoverflow.com/questions/56159389
复制相似问题