【发布时间】:2021-11-25 10:34:15
【问题描述】:
我想创建一个饼图,当我单击某个部分时,我应该会显示一个数据框。 例如,我可能会创建以下饼图:
# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")
# Give the chart file a name.
png(file = "city.png")
# Plot the chart.
pie(x,labels)
现在让我们说当我点击诸如“伦敦”之类的切片时,我得到了 IRIS 数据集。
我使用的解决方案:
library(shiny)
library(plotly)
df <- https://drive.google.com/file/d/1RT5AkCef4cehEaGelK0avbXAtck1f-Ap/view #READ THIS DATA HERE
setDT(df)
dtnum <- df[ , .N, by="V3"]
dtnum2 <- df[ , .N, by="V2"]
ui <- fluidPage(
plotlyOutput("myPlot"),
plotlyOutput("myPlot2"),
DTOutput("mydt")
)
server <- function(input, output, session) {
observe({
d <- event_data("plotly_click")
print(d)
if (is.null(d)) {
df
} else {
output$mydt <- renderDT({
df[V3 == d$customdata]
})
}
})
output$myPlot2 <- renderPlotly({
plot_ly(dtnum2, labels = ~V2, values = ~N, type = 'pie', customdata = ~V2)
})
output$myPlot <- renderPlotly({
plot_ly(dtnum, labels = ~V3, values = ~N, type = 'pie', customdata = ~V3)
})
}
shinyApp(ui, server)
【问题讨论】:
-
pie()会给你一张照片。因此,我们需要其他东西来构建饼图。也许“highcharter”是你想要的。有几个主题。如果你想把它连接到一张桌子上,我认为你需要使用闪亮的。祝你好运。我渴望看到结果。听起来是个好主意! -
在我的编辑中查看我的解决方案
标签: r