使用 DiagrammeR 在 shiny 中绘制甘特图的问题
Problem with Gantt chart plotting in shiny with DiagrammeR
我在使用 DiagrammeR 的美人鱼绘图功能在 Shiny 中显示甘特图时遇到问题。
一切都按预期执行,但是绘图显示在 R studio 的查看器中,而不是在闪亮的页面上(它有一个用于显示绘图的 tabPanel)。
我已经看到这个 asked/solved 使用 ggVis .. 但是那个实例中的解决方案是特定于 ggVis 的。我想继续使用 DiagrammeR/mermaid,因为它会产生非常好看的图。
附上一个最小的可执行示例:-)
library(shiny)
library(lubridate)
library(DiagrammeR)
# --- Input datafile
AllData <- data.frame(Project = c("Phoenix", "Phoenix", "Phoenix"),
task = c("Establish plan", "Build management tool", "Get funding"),
status = c("done", "active", "crit, active"),
pos = c("first_1", "first_2", "import_1"),
start = c("2018-12-01", "2019-01-21", "2019-02-01"),
end = c("12w 6d", "4w", "8w 1d"),
stringsAsFactors = FALSE)
# Define UI for application
ui <- fluidPage(
titlePanel("XXX Project Management Tool"),
sidebarLayout(
sidebarPanel( # --- setup LHS data input frames ---
selectInput("Proj", "Project",
c(unique(as.character(AllData$Project)))),
selectInput("Stg", "Stage",
c("All", unique(as.character(AllData$name)))),
width = 3),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Gantt Chart", plotOutput("plot")),
tabPanel("Data Table", tableOutput("table"))))
)
)
server <- function(input, output) {
# --- filter the selected project into a reactive function (access later using () suffix) ---
SelectedProject <- reactive({dplyr::filter(AllData, Project == input$Proj)})
output$plot <- renderPlot({
mermaid(
paste0(
"gantt", "\n",
"dateFormat YYYY-MM-DD", "\n",
"title Gantt Chart - Project ", input$Proj, "\n",
# --- unite the first two columns (task & status) and separate them with ":" ---
# --- then, unite the other columns and separate them with "," ---
paste(SelectedProject() %>%
unite(i, task, status, sep = ":") %>%
unite(j, i, pos, start, end, sep = ",") %>%
.$j,
collapse = "\n"
), "\n"
)
)
})
output$table <- renderTable({SelectedProject()})
}
# --- run application ---
shinyApp(ui = ui, server = server)
绘图最终显示在查看器中 - 但是另一个选项卡面板按预期显示表格数据。
您需要使用 DiagrammeROutput()
而不是 plotOutput()
和 renderDiagrammeR()
而不是 renderPlot()
:
library(shiny)
library(lubridate)
library(DiagrammeR)
library(tidyr)
# --- Input datafile
AllData <- data.frame(Project = c("Phoenix", "Phoenix", "Phoenix"),
task = c("Establish plan", "Build management tool", "Get funding"),
status = c("done", "active", "crit, active"),
pos = c("first_1", "first_2", "import_1"),
start = c("2018-12-01", "2019-01-21", "2019-02-01"),
end = c("12w 6d", "4w", "8w 1d"),
stringsAsFactors = FALSE)
# Define UI for application
ui <- fluidPage(
titlePanel("XXX Project Management Tool"),
sidebarLayout(
sidebarPanel( # --- setup LHS data input frames ---
selectInput("Proj", "Project",
c(unique(as.character(AllData$Project)))),
selectInput("Stg", "Stage",
c("All", unique(as.character(AllData$name)))),
width = 3),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Gantt Chart", DiagrammeROutput("plot")),
tabPanel("Data Table", tableOutput("table"))))
)
)
server <- function(input, output) {
# --- filter the selected project into a reactive function (access later using () suffix) ---
SelectedProject <- reactive({dplyr::filter(AllData, Project == input$Proj)})
output$plot <- renderDiagrammeR({
mermaid(
paste0(
"gantt", "\n",
"dateFormat YYYY-MM-DD", "\n",
"title Gantt Chart - Project ", input$Proj, "\n",
# --- unite the first two columns (task & status) and separate them with ":" ---
# --- then, unite the other columns and separate them with "," ---
paste(SelectedProject() %>%
unite(i, task, status, sep = ":") %>%
unite(j, i, pos, start, end, sep = ",") %>%
.$j,
collapse = "\n"
), "\n"
)
)
})
output$table <- renderTable({SelectedProject()})
}
# --- run application ---
shinyApp(ui = ui, server = server)
我在使用 DiagrammeR 的美人鱼绘图功能在 Shiny 中显示甘特图时遇到问题。
一切都按预期执行,但是绘图显示在 R studio 的查看器中,而不是在闪亮的页面上(它有一个用于显示绘图的 tabPanel)。 我已经看到这个 asked/solved 使用 ggVis .. 但是那个实例中的解决方案是特定于 ggVis 的。我想继续使用 DiagrammeR/mermaid,因为它会产生非常好看的图。
附上一个最小的可执行示例:-)
library(shiny)
library(lubridate)
library(DiagrammeR)
# --- Input datafile
AllData <- data.frame(Project = c("Phoenix", "Phoenix", "Phoenix"),
task = c("Establish plan", "Build management tool", "Get funding"),
status = c("done", "active", "crit, active"),
pos = c("first_1", "first_2", "import_1"),
start = c("2018-12-01", "2019-01-21", "2019-02-01"),
end = c("12w 6d", "4w", "8w 1d"),
stringsAsFactors = FALSE)
# Define UI for application
ui <- fluidPage(
titlePanel("XXX Project Management Tool"),
sidebarLayout(
sidebarPanel( # --- setup LHS data input frames ---
selectInput("Proj", "Project",
c(unique(as.character(AllData$Project)))),
selectInput("Stg", "Stage",
c("All", unique(as.character(AllData$name)))),
width = 3),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Gantt Chart", plotOutput("plot")),
tabPanel("Data Table", tableOutput("table"))))
)
)
server <- function(input, output) {
# --- filter the selected project into a reactive function (access later using () suffix) ---
SelectedProject <- reactive({dplyr::filter(AllData, Project == input$Proj)})
output$plot <- renderPlot({
mermaid(
paste0(
"gantt", "\n",
"dateFormat YYYY-MM-DD", "\n",
"title Gantt Chart - Project ", input$Proj, "\n",
# --- unite the first two columns (task & status) and separate them with ":" ---
# --- then, unite the other columns and separate them with "," ---
paste(SelectedProject() %>%
unite(i, task, status, sep = ":") %>%
unite(j, i, pos, start, end, sep = ",") %>%
.$j,
collapse = "\n"
), "\n"
)
)
})
output$table <- renderTable({SelectedProject()})
}
# --- run application ---
shinyApp(ui = ui, server = server)
绘图最终显示在查看器中 - 但是另一个选项卡面板按预期显示表格数据。
您需要使用 DiagrammeROutput()
而不是 plotOutput()
和 renderDiagrammeR()
而不是 renderPlot()
:
library(shiny)
library(lubridate)
library(DiagrammeR)
library(tidyr)
# --- Input datafile
AllData <- data.frame(Project = c("Phoenix", "Phoenix", "Phoenix"),
task = c("Establish plan", "Build management tool", "Get funding"),
status = c("done", "active", "crit, active"),
pos = c("first_1", "first_2", "import_1"),
start = c("2018-12-01", "2019-01-21", "2019-02-01"),
end = c("12w 6d", "4w", "8w 1d"),
stringsAsFactors = FALSE)
# Define UI for application
ui <- fluidPage(
titlePanel("XXX Project Management Tool"),
sidebarLayout(
sidebarPanel( # --- setup LHS data input frames ---
selectInput("Proj", "Project",
c(unique(as.character(AllData$Project)))),
selectInput("Stg", "Stage",
c("All", unique(as.character(AllData$name)))),
width = 3),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Gantt Chart", DiagrammeROutput("plot")),
tabPanel("Data Table", tableOutput("table"))))
)
)
server <- function(input, output) {
# --- filter the selected project into a reactive function (access later using () suffix) ---
SelectedProject <- reactive({dplyr::filter(AllData, Project == input$Proj)})
output$plot <- renderDiagrammeR({
mermaid(
paste0(
"gantt", "\n",
"dateFormat YYYY-MM-DD", "\n",
"title Gantt Chart - Project ", input$Proj, "\n",
# --- unite the first two columns (task & status) and separate them with ":" ---
# --- then, unite the other columns and separate them with "," ---
paste(SelectedProject() %>%
unite(i, task, status, sep = ":") %>%
unite(j, i, pos, start, end, sep = ",") %>%
.$j,
collapse = "\n"
), "\n"
)
)
})
output$table <- renderTable({SelectedProject()})
}
# --- run application ---
shinyApp(ui = ui, server = server)