r - 如何将带有 plotly 的 ggplot 嵌入到 rshiny 应用程序或 flexdashboard 中?
问题描述
我有以下创建交互式绘图的脚本。我想把它嵌入到某种闪亮的应用程序或 flexdashboard 中。我该怎么做?
library(ggplot2)
library(dplyr)
library(plotly)
library(viridis)
library(hrbrthemes)
library(gapminder)
data <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)
p <- data %>%
mutate(gdpPercap=round(gdpPercap,0)) %>%
mutate(pop=round(pop/1000000,2)) %>%
mutate(lifeExp=round(lifeExp,1)) %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country)) %>%
mutate(text = paste("Country: ", country, "\nPopulation (M): ", pop, "\nLife Expectancy: ", lifeExp, "\nGdp per capita: ", gdpPercap, sep="")) %>%
ggplot( aes(x=gdpPercap, y=lifeExp, size = pop, color = continent, text=text)) +
geom_point(alpha=0.7) +
scale_size(range = c(1.4, 19), name="Population (M)") +
scale_color_viridis(discrete=TRUE, guide=FALSE) +
theme_ipsum() +
theme(legend.position="none")
# turn ggplot interactive with plotly
pp <- ggplotly(p, tooltip="text")
pp
# save the widget
# library(htmlwidgets)
# saveWidget(pp, file=paste0( getwd(), "/HtmlWidget/ggplotlyBubblechart.html"))
解决方案
一个shinydashboard
例子:
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(viridis)
library(hrbrthemes)
library(htmlwidgets)
library(gapminder)
gapminderdata <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
)
),
dashboardBody(
tabItems(
tabItem(tags$style(type = "text/css", "#myPlot {height: calc(87vh) !important;}"),
tabName = "dashboard",
fluidRow(
column(12,
plotlyOutput("myPlot"),
br(),
downloadButton("myDownloadBtn"))
)
)
)
)
)
server <- function(input, output, session) {
myData <- reactive({
gapminderdata %>%
mutate(gdpPercap=round(gdpPercap,0)) %>%
mutate(pop=round(pop/1000000,2)) %>%
mutate(lifeExp=round(lifeExp,1)) %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country)) %>%
mutate(text = paste("Country: ", country, "\nPopulation (M): ", pop, "\nLife Expectancy: ", lifeExp, "\nGdp per capita: ", gdpPercap, sep=""))
})
fig <- reactiveVal(plotly_empty())
output$myPlot <- renderPlotly({
p <- ggplot(data = myData(), aes(x=gdpPercap, y=lifeExp, size = pop, color = continent, text=text)) +
geom_point(alpha=0.7) +
scale_size(range = c(1.4, 19), name="Population (M)") +
scale_color_viridis(discrete=TRUE, guide=FALSE) +
theme_ipsum() +
theme(legend.position="none")
fig(ggplotly(p, tooltip="text")) # pass plotly object to reactiveVal
fig() # return plotly object
})
output$myDownloadBtn <- downloadHandler(
filename = function() {
paste0(gsub(" ","_", gsub(":",".", Sys.time())),"_plotly.html")
},
content = function(file) {
saveWidget(partial_bundle(fig()), file, selfcontained = TRUE)
}
)
}
shinyApp(ui, server)
推荐阅读
- c# - 使用 EF Core 3.1 查询 Azure Cosmos DB 时未映射的对象属性列表
- python - 如何添加排序功能?
- post - 错误,导入后,power bi rest api,发布 pbix 文件
- r - 如何在 RStudio 中为代码行添加书签?
- javascript - 使用 React 在 Laravel Project 上提交表单后执行 Axios POST 方法后出现“500 内部服务器错误”
- python - 如何在 Django ORM 中加入两个表?
- neo4j - 在 neo4j 中导入 csv 时导致 ArrayIndexOutOfBoundsException 的原因是什么?
- flutter - 从左到右拖放图像 Flutter
- python - 使用 Paramiko 将具有特定扩展名的本地文件夹中的所有文件上传到 SFTP 服务器
- php - 将文本文件解析为 4 列,第 3 列有时有值,有时没有(PHP)