Automatic generate 3D pie Graph

Hello all,

I need the scripts automatic generate 3D pie Graph. What I have now is as below:

Run at terminal by $ CMD R BATCH graph.R

# 3D Exploded Pie Chart
library(plotrix)
slices <- c(20, 15, 4, 15, 8)
lbls <- c("Media", "Document", "DB", "Others", "Available")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie3D(slices,labels=lbls,explode=0.1,
main="Storage Profiler ")

But the problem is I need to key in manually in this scripts. For example at:

slices <- c(20, 15, 4, 15, 8)
lbls <- c("Media", "Document", "DB", "Others", "Available")

How about this scripts will read the data from *.csv file

Sample of CSV:

Media,20
Document,15
DB,4
Others,15
Available,8

#!/bin/bash
Val=
Name=
while IFS="," read name val
do
        if [ -z "$Name" ];
        then
                Name=\""$name"\"
        else
                Name=$Name,\""$name"\"
        fi
        if [ -z "$Val" ];
        then
                Val=$val
        else
                Val="$Val, $val"
        fi
done < $1

echo "# 3D Exploded Pie Chart
library(plotrix)
slices <- c($Val)
lbls <- c($Name)
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,\"%\",sep=\"\") # ad % to labels
pie3D(slices,labels=lbls,explode=0.1,
main=\"Storage Profiler \")"
# /temp/script file.cvs
# 3D Exploded Pie Chart
library(plotrix)
slices <- c(20, 15, 4, 15, 8)
lbls <- c("Media","Document","DB","Others","Available")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie3D(slices,labels=lbls,explode=0.1,
main="Storage Profiler ")
1 Like

Thanks Sir. But how to run? How about if I have data from CSV or txt.

Sample:
Media,20
Document,15
DB,4
Others,15
Available,8

Because I need this scripts will get from this data CSV or txt.

Just save output of script to a file and then call rscript.exe to produce your pdf.

e.g. in cygwin under windows, you would:

... ( rest of script here )...
pie3D(slices,labels=lbls,explode=0.1
main=\"Storage Profiler \")" > disk.r
/cygdrive/c/Program\ Files/R/R-2.12.2/bin/rscript.exe disk.r

How will this code take the csv file or text file as input? can someone help

First I'll show you the whole script:

#!/bin/bash
Val=
Name=
while IFS="," read name val
do
        if [ -z "$Name" ];
        then
                Name=\""$name"\"
        else
                Name=$Name,\""$name"\"
        fi
        if [ -z "$Val" ];
        then
                Val=$val
        else
                Val="$Val, $val"
        fi
done < $1

echo "# 3D Exploded Pie Chart
library(plotrix)
slices <- c($Val)
lbls <- c($Name)
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,\"%\",sep=\"\") # ad % to labels
pie3D(slices,labels=lbls,explode=0.1,
main=\"Storage Profiler \")" > disk.r
/cygdrive/c/Program\ Files/R/R-2.12.2/bin/rscript.exe disk.r

Note the green text above needs to point to your rscript program, this example is for cygwin (Version 2.12.2 of r). If your path is setup to contain the r binary dir you simply replace with rscript.

Now, if you save this script as buildgraph.sh and make is executable (chmod +x buildgraph.sh) you could call it like this:

$ buildgraph.sh mydoc.csv