Streamline R code for Shiny usage

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I have the following code to create a table for overall percentages and the past 30 day percentages. I would like to add this to a Shiny app I'm making, but I feel there is a simpler or shorter way to create this code/table than what I have, which would work better in Shiny. I'm not asking for the Shiny code, just a way to reduce the code I have.



a$FAIL<-ifelse(a$OPERATION_STATUS %in% "FAIL", 1, 0)
cc<-as.data.frame((table(a$CRIT_CODE))) #Dataframe with frequency of each crit code
cf<-aggregate(FAIL~CRIT_CODE,a,sum) #Total number of fails based on crit codes
cc<-cbind(cc[,],cf[,2])
names(cc)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
cc<-transform(cc, Percent=FAIL/Freq*100)

last30<-a[which(a$DATE>=(Sys.Date()-30)),]
last<-as.data.frame((table(last30$CRIT_CODE))) #Dataframe with frequency ofeach crit code
lastfails<-aggregate(FAIL~CRIT_CODE,last30,sum) #Total number of fails based on crit codes
last<-cbind(last[,],lastfails[,2])
names(last)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
last<-transform(last, Percent=FAIL/Freq*100)

rates<-merge(cc[,c(1,4)], last[,c(1,4)], by="CRIT_CODE")
rates$Percent.x<-round(rates$Percent.x, 2)
rates$Percent.y<-round(rates$Percent.y, 2)
library(gridExtra)
grid.table(rates, rows=NULL, cols=c("Area", "Overall Percent", "30 Day Percent"))


Sample Data



Data <- data.frame(
DATE=sample(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"), 15),
OPERATION_STATUS=sample(c("PASS","FAIL"), 15, replace=TRUE),
CRIT_CODE=sample(c("A", "B", "C"), 15, replace=TRUE)
)






share|improve this question

















  • 1




    can you add example data set?
    – minem
    Jun 21 at 14:25










  • @minem I uploaded code for a sample data set
    – Lost
    Jun 21 at 15:15
















up vote
4
down vote

favorite












I have the following code to create a table for overall percentages and the past 30 day percentages. I would like to add this to a Shiny app I'm making, but I feel there is a simpler or shorter way to create this code/table than what I have, which would work better in Shiny. I'm not asking for the Shiny code, just a way to reduce the code I have.



a$FAIL<-ifelse(a$OPERATION_STATUS %in% "FAIL", 1, 0)
cc<-as.data.frame((table(a$CRIT_CODE))) #Dataframe with frequency of each crit code
cf<-aggregate(FAIL~CRIT_CODE,a,sum) #Total number of fails based on crit codes
cc<-cbind(cc[,],cf[,2])
names(cc)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
cc<-transform(cc, Percent=FAIL/Freq*100)

last30<-a[which(a$DATE>=(Sys.Date()-30)),]
last<-as.data.frame((table(last30$CRIT_CODE))) #Dataframe with frequency ofeach crit code
lastfails<-aggregate(FAIL~CRIT_CODE,last30,sum) #Total number of fails based on crit codes
last<-cbind(last[,],lastfails[,2])
names(last)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
last<-transform(last, Percent=FAIL/Freq*100)

rates<-merge(cc[,c(1,4)], last[,c(1,4)], by="CRIT_CODE")
rates$Percent.x<-round(rates$Percent.x, 2)
rates$Percent.y<-round(rates$Percent.y, 2)
library(gridExtra)
grid.table(rates, rows=NULL, cols=c("Area", "Overall Percent", "30 Day Percent"))


Sample Data



Data <- data.frame(
DATE=sample(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"), 15),
OPERATION_STATUS=sample(c("PASS","FAIL"), 15, replace=TRUE),
CRIT_CODE=sample(c("A", "B", "C"), 15, replace=TRUE)
)






share|improve this question

















  • 1




    can you add example data set?
    – minem
    Jun 21 at 14:25










  • @minem I uploaded code for a sample data set
    – Lost
    Jun 21 at 15:15












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I have the following code to create a table for overall percentages and the past 30 day percentages. I would like to add this to a Shiny app I'm making, but I feel there is a simpler or shorter way to create this code/table than what I have, which would work better in Shiny. I'm not asking for the Shiny code, just a way to reduce the code I have.



a$FAIL<-ifelse(a$OPERATION_STATUS %in% "FAIL", 1, 0)
cc<-as.data.frame((table(a$CRIT_CODE))) #Dataframe with frequency of each crit code
cf<-aggregate(FAIL~CRIT_CODE,a,sum) #Total number of fails based on crit codes
cc<-cbind(cc[,],cf[,2])
names(cc)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
cc<-transform(cc, Percent=FAIL/Freq*100)

last30<-a[which(a$DATE>=(Sys.Date()-30)),]
last<-as.data.frame((table(last30$CRIT_CODE))) #Dataframe with frequency ofeach crit code
lastfails<-aggregate(FAIL~CRIT_CODE,last30,sum) #Total number of fails based on crit codes
last<-cbind(last[,],lastfails[,2])
names(last)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
last<-transform(last, Percent=FAIL/Freq*100)

rates<-merge(cc[,c(1,4)], last[,c(1,4)], by="CRIT_CODE")
rates$Percent.x<-round(rates$Percent.x, 2)
rates$Percent.y<-round(rates$Percent.y, 2)
library(gridExtra)
grid.table(rates, rows=NULL, cols=c("Area", "Overall Percent", "30 Day Percent"))


Sample Data



Data <- data.frame(
DATE=sample(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"), 15),
OPERATION_STATUS=sample(c("PASS","FAIL"), 15, replace=TRUE),
CRIT_CODE=sample(c("A", "B", "C"), 15, replace=TRUE)
)






share|improve this question













I have the following code to create a table for overall percentages and the past 30 day percentages. I would like to add this to a Shiny app I'm making, but I feel there is a simpler or shorter way to create this code/table than what I have, which would work better in Shiny. I'm not asking for the Shiny code, just a way to reduce the code I have.



a$FAIL<-ifelse(a$OPERATION_STATUS %in% "FAIL", 1, 0)
cc<-as.data.frame((table(a$CRIT_CODE))) #Dataframe with frequency of each crit code
cf<-aggregate(FAIL~CRIT_CODE,a,sum) #Total number of fails based on crit codes
cc<-cbind(cc[,],cf[,2])
names(cc)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
cc<-transform(cc, Percent=FAIL/Freq*100)

last30<-a[which(a$DATE>=(Sys.Date()-30)),]
last<-as.data.frame((table(last30$CRIT_CODE))) #Dataframe with frequency ofeach crit code
lastfails<-aggregate(FAIL~CRIT_CODE,last30,sum) #Total number of fails based on crit codes
last<-cbind(last[,],lastfails[,2])
names(last)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
last<-transform(last, Percent=FAIL/Freq*100)

rates<-merge(cc[,c(1,4)], last[,c(1,4)], by="CRIT_CODE")
rates$Percent.x<-round(rates$Percent.x, 2)
rates$Percent.y<-round(rates$Percent.y, 2)
library(gridExtra)
grid.table(rates, rows=NULL, cols=c("Area", "Overall Percent", "30 Day Percent"))


Sample Data



Data <- data.frame(
DATE=sample(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"), 15),
OPERATION_STATUS=sample(c("PASS","FAIL"), 15, replace=TRUE),
CRIT_CODE=sample(c("A", "B", "C"), 15, replace=TRUE)
)








share|improve this question












share|improve this question




share|improve this question








edited Jun 21 at 15:14
























asked Jun 21 at 14:20









Lost

235




235







  • 1




    can you add example data set?
    – minem
    Jun 21 at 14:25










  • @minem I uploaded code for a sample data set
    – Lost
    Jun 21 at 15:15












  • 1




    can you add example data set?
    – minem
    Jun 21 at 14:25










  • @minem I uploaded code for a sample data set
    – Lost
    Jun 21 at 15:15







1




1




can you add example data set?
– minem
Jun 21 at 14:25




can you add example data set?
– minem
Jun 21 at 14:25












@minem I uploaded code for a sample data set
– Lost
Jun 21 at 15:15




@minem I uploaded code for a sample data set
– Lost
Jun 21 at 15:15










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Changed a little bit input data:



n <- 60
set.seed(21)
a <- data.frame(
DATE =
rev(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"))[1:n],
OPERATION_STATUS = sample(c("PASS","FAIL"), n, replace = TRUE),
CRIT_CODE = sample(c("A", "B", "C"), n, replace = TRUE)
)


Using data.table I would do it like this:



require(data.table)
setDT(a)
a[, group_i := DATE >= (Sys.Date() - 30)]
rates3 <- a[, .(
Percent1 = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
Percent2 = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = CRIT_CODE]

rates3
# CRIT_CODE Percent1 Percent2
# 1: A 71.43 75.00
# 2: B 63.16 66.67
# 3: C 59.26 45.45


Presumably for large data one of the fastest approaches.



P.S. results changes depending on Sys.Date()



Names can be added directly:



rates3 <- a[, .(
'Overall Percent' = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
'30 Day Percent' = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = .(Area = CRIT_CODE)]





share|improve this answer

















  • 1




    Thank you so much! It works exactly like I needed it to!
    – Lost
    Jun 22 at 12:33










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196979%2fstreamline-r-code-for-shiny-usage%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Changed a little bit input data:



n <- 60
set.seed(21)
a <- data.frame(
DATE =
rev(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"))[1:n],
OPERATION_STATUS = sample(c("PASS","FAIL"), n, replace = TRUE),
CRIT_CODE = sample(c("A", "B", "C"), n, replace = TRUE)
)


Using data.table I would do it like this:



require(data.table)
setDT(a)
a[, group_i := DATE >= (Sys.Date() - 30)]
rates3 <- a[, .(
Percent1 = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
Percent2 = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = CRIT_CODE]

rates3
# CRIT_CODE Percent1 Percent2
# 1: A 71.43 75.00
# 2: B 63.16 66.67
# 3: C 59.26 45.45


Presumably for large data one of the fastest approaches.



P.S. results changes depending on Sys.Date()



Names can be added directly:



rates3 <- a[, .(
'Overall Percent' = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
'30 Day Percent' = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = .(Area = CRIT_CODE)]





share|improve this answer

















  • 1




    Thank you so much! It works exactly like I needed it to!
    – Lost
    Jun 22 at 12:33














up vote
1
down vote



accepted










Changed a little bit input data:



n <- 60
set.seed(21)
a <- data.frame(
DATE =
rev(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"))[1:n],
OPERATION_STATUS = sample(c("PASS","FAIL"), n, replace = TRUE),
CRIT_CODE = sample(c("A", "B", "C"), n, replace = TRUE)
)


Using data.table I would do it like this:



require(data.table)
setDT(a)
a[, group_i := DATE >= (Sys.Date() - 30)]
rates3 <- a[, .(
Percent1 = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
Percent2 = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = CRIT_CODE]

rates3
# CRIT_CODE Percent1 Percent2
# 1: A 71.43 75.00
# 2: B 63.16 66.67
# 3: C 59.26 45.45


Presumably for large data one of the fastest approaches.



P.S. results changes depending on Sys.Date()



Names can be added directly:



rates3 <- a[, .(
'Overall Percent' = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
'30 Day Percent' = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = .(Area = CRIT_CODE)]





share|improve this answer

















  • 1




    Thank you so much! It works exactly like I needed it to!
    – Lost
    Jun 22 at 12:33












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Changed a little bit input data:



n <- 60
set.seed(21)
a <- data.frame(
DATE =
rev(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"))[1:n],
OPERATION_STATUS = sample(c("PASS","FAIL"), n, replace = TRUE),
CRIT_CODE = sample(c("A", "B", "C"), n, replace = TRUE)
)


Using data.table I would do it like this:



require(data.table)
setDT(a)
a[, group_i := DATE >= (Sys.Date() - 30)]
rates3 <- a[, .(
Percent1 = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
Percent2 = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = CRIT_CODE]

rates3
# CRIT_CODE Percent1 Percent2
# 1: A 71.43 75.00
# 2: B 63.16 66.67
# 3: C 59.26 45.45


Presumably for large data one of the fastest approaches.



P.S. results changes depending on Sys.Date()



Names can be added directly:



rates3 <- a[, .(
'Overall Percent' = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
'30 Day Percent' = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = .(Area = CRIT_CODE)]





share|improve this answer













Changed a little bit input data:



n <- 60
set.seed(21)
a <- data.frame(
DATE =
rev(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"))[1:n],
OPERATION_STATUS = sample(c("PASS","FAIL"), n, replace = TRUE),
CRIT_CODE = sample(c("A", "B", "C"), n, replace = TRUE)
)


Using data.table I would do it like this:



require(data.table)
setDT(a)
a[, group_i := DATE >= (Sys.Date() - 30)]
rates3 <- a[, .(
Percent1 = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
Percent2 = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = CRIT_CODE]

rates3
# CRIT_CODE Percent1 Percent2
# 1: A 71.43 75.00
# 2: B 63.16 66.67
# 3: C 59.26 45.45


Presumably for large data one of the fastest approaches.



P.S. results changes depending on Sys.Date()



Names can be added directly:



rates3 <- a[, .(
'Overall Percent' = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
'30 Day Percent' = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
),
keyby = .(Area = CRIT_CODE)]






share|improve this answer













share|improve this answer



share|improve this answer











answered Jun 22 at 7:15









minem

232139




232139







  • 1




    Thank you so much! It works exactly like I needed it to!
    – Lost
    Jun 22 at 12:33












  • 1




    Thank you so much! It works exactly like I needed it to!
    – Lost
    Jun 22 at 12:33







1




1




Thank you so much! It works exactly like I needed it to!
– Lost
Jun 22 at 12:33




Thank you so much! It works exactly like I needed it to!
– Lost
Jun 22 at 12:33












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196979%2fstreamline-r-code-for-shiny-usage%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation