You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新手求助:如何用RStudio flexdashboard创建饼图?

Fixing Your Flexdashboard Pie Chart Issue

Hey there! Let's work through why your pie chart isn't running—there are a few straightforward fixes we can apply to get it working correctly. Here's what's going wrong and how to fix it:

Key Issues in Your Code

  • Wrong file reading function: You're using read.csv() to load an .xls Excel file, but read.csv() only works with CSV files. For Excel files, you need a package like readxl.
  • Column name mismatch: Your dataset has a column named Refugees, but your code references ~totalref$Total—this will throw an error because that column doesn't exist.
  • Missing pipe connection: Your layout() call isn't linked to the plot_ly() output, so the layout settings won't apply, and the plot might not render properly.

Corrected Full Code

First, make sure you have all required packages installed (if you haven't already):

install.packages(c("flexdashboard", "plotly", "readxl"))

Then use this updated code in your flexdashboard:

---
title: "Refugee Dashboard"
output: flexdashboard::flex_dashboard
---

```{r message=FALSE, warning=FALSE}
library(flexdashboard)
library(plotly)
library(readxl)

# Read the Excel file correctly with read_excel()
totalref <- read_excel("F:/HU/ANLY 512/Dashboarding Lab/total.xls")

# Create the pie chart with correct column names and pipe the layout
plot_ly(totalref, 
        labels = ~Country,  # No need for totalref$ when using formula syntax
        values = ~Refugees, # Match the actual column name from your dataset
        type = 'pie') %>%
  layout(title = 'Persons of Concern in 9 European Countries',
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

Quick Additional Checks

  • Double-check that your file path is correct (no typos, and the file exists at that location)
  • Verify that your Excel file's column names are exactly Country and Refugees (no extra spaces or capitalization differences)

内容的提问来源于stack exchange,提问作者Louis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:27:04