我有以下ui.R
library(shiny)
library(anytime)
x<-read.csv("reddit_data_by_month.csv",header=T)
x["date"]<-anydate(x$month)
shinyUI(fluidPage(
# Application title
title ="Reddit Data By Month",
plotOutput("graph"),
hr(),
fluidRow(
column(6,
dateRangeInput("dates", label = h2("Date Range"), start = x$date[1], end = x$date[142], min = x$date[1], max = x$date[142], format = "mm/yyyy", startview = "year")
),
column(6,
checkboxGroupInput("checkGroup", label = h2("Data Sets"),
choices = list("Accounts Created" = 1, "Comments" = 2, "Submissions" = 3, "Votes" = 4),
selected = NULL)
#NULL
)
)
)
)
以下server.R
library(shiny)
library(ggplot2)
library(anytime)
library(dplyr)
x<-read.csv("reddit_data_by_month.csv",header=T)
x["date"]<-anydate(x$month)
shinyServer(function(input, output) {
subsetData <- reactive({
newData <- subset(x, date > as.Date(input$dates[1]) & as.Date(date) < as.Date(input$dates[2]))
return(newData)
})
output$graph <- renderPlot({
reactive({
graph <- ggplot()
if (grepl("1",input$checkGroup)){
graph+geom_line(data=subsetData(),aes(x=date,y=accounts_created,color="red"))
}
})
})
})
我的数据是
structure(list(month = structure(1:142, .Label = c("2005-06",
"2005-07", "2005-08", "2005-09", "2005-10", "2005-11", "2005-12",
"2006-01", "2006-02", "2006-03", "2006-04", "2006-05", "2006-06",
"2006-07", "2006-08", "2006-09", "2006-10", "2006-11", "2006-12",
"2007-01", "2007-02", "2007-03", "2007-04", "2007-05", "2007-06",
"2007-07", "2007-08", "2007-09", "2007-10", "2007-11", "2007-12",
"2008-01", "2008-02", "2008-03", "2008-04", "2008-05", "2008-06",
"2008-07", "2008-08", "2008-09", "2008-10", "2008-11", "2008-12",
"2009-01", "2009-02", "2009-03", "2009-04", "2009-05", "2009-06",
"2009-07", "2009-08", "2009-09", "2009-10", "2009-11", "2009-12",
"2010-01", "2010-02", "2010-03", "2010-04", "2010-05", "2010-06",
"2010-07", "2010-08", "2010-09", "2010-10", "2010-11", "2010-12",
"2011-01", "2011-02", "2011-03", "2011-04", "2011-05", "2011-06",
"2011-07", "2011-08", "2011-09", "2011-10", "2011-11", "2011-12",
"2012-01", "2012-02", "2012-03", "2012-04", "2012-05", "2012-06",
"2012-07", "2012-08", "2012-09", "2012-10", "2012-11", "2012-12",
"2013-01", "2013-02", "2013-03", "2013-04", "2013-05", "2013-06",
"2013-07", "2013-08", "2013-09", "2013-10", "2013-11", "2013-12",
"2014-01", "2014-02", "2014-03", "2014-04", "2014-05", "2014-06",
"2014-07", "2014-08", "2014-09", "2014-10", "2014-11", "2014-12",
"2015-01", "2015-02", "2015-03", "2015-04", "2015-05", "2015-06",
"2015-07", "2015-08", "2015-09", "2015-10", "2015-11", "2015-12",
"2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-06",
"2016-07", "2016-08", "2016-09", "2016-10", "2016-11", "2016-12",
"2017-01", "2017-02", "2017-03"), class = "factor"), accounts_created = c(48L,
218L, 661L, 523L, 587L, 1385L, 3175L, 3694L, 4828L, 6907L, 6100L,
7331L, 9847L, 9182L, 13517L, 14172L, 15607L, 20457L, 13691L,
35775L, 23302L, 25936L, 29160L, 44229L, 28057L, 31420L, 35582L,
35225L, 62448L, 28496L, 27419L, 33332L, 30771L, 33643L, 31474L,
37465L, 40301L, 41403L, 38573L, 43099L, 44447L, 42032L, 43404L,
52888L, 51064L, 58812L, 57449L, 54475L, 56395L, 64072L, 69943L,
73429L, 79232L, 78275L, 87945L, 96497L, 92172L, 100400L, 99713L,
99793L, 103067L, 115865L, 133784L, 152043L, 147386L, 156233L,
170626L, 192205L, 194640L, 230680L, 219910L, 237391L, 233268L,
243923L, 283644L, 322543L, 327499L, 325497L, 348052L, 425189L,
419233L, 446002L, 468498L, 437879L, 543711L, 560730L, 574559L,
523669L, 497116L, 490460L, 503286L, 628410L, 567514L, 623222L,
632132L, 609353L, 566556L, 613224L, 639265L, 630462L, 650250L,
696966L, 758969L, 799461L, 719222L, 735193L, 740941L, 703823L,
683390L, 781972L, 807650L, 838559L, 829927L, 961855L, 1204939L,
1082964L, 1098516L, 1494934L, 1009347L, 1020310L, 1048679L, 1294614L,
1343828L, 1387719L, 1314019L, 1585696L, 1351193L, 1556725L, 2798107L,
1376827L, 1343441L, 1290658L, 1328986L, 1429274L, 1803343L, 1204529L,
1201884L, 1192114L, 1366015L, 1568402L, 1429326L, 1254460L),
comments = c(0L, 0L, 0L, 0L, 0L, 0L, 2357L, 7953L, 9541L,
14321L, 19866L, 27423L, 29983L, 37587L, 50659L, 50773L, 54536L,
62362L, 60820L, 82659L, 97745L, 113684L, 127091L, 170643L,
180439L, 207659L, 225203L, 260123L, 274924L, 373975L, 362979L,
455129L, 441245L, 464387L, 468865L, 537406L, 579049L, 593005L,
597998L, 683651L, 789113L, 797219L, 852556L, 1058022L, 948344L,
1055005L, 1099697L, 1206715L, 1264047L, 1477059L, 1760218L,
2040509L, 2240352L, 2226624L, 2557647L, 2905450L, 2686848L,
3243501L, 3211545L, 3283363L, 3548140L, 4060855L, 4281423L,
4718729L, 5049686L, 5735450L, 5957395L, 6668561L, 6388389L,
7571304L, 7601725L, 8896402L, 9814580L, 10633712L, 12423445L,
12222905L, 13577570L, 13753896L, 14542605L, 16527084L, 16422774L,
17954424L, 19216041L, 20510389L, 21949445L, 24387949L, 25817623L,
23609848L, 24936572L, 24834710L, 26230630L, 30694351L, 27416555L,
30977337L, 33551836L, 33281887L, 33022329L, 35257639L, 35009325L,
32325018L, 36210545L, 37827440L, 40045414L, 43017721L, 39198081L,
42942717L, 42884781L, 42886243L, 42508122L, 47416897L, 47541298L,
45680630L, 47949180L, 46891046L, 49234627L, 54590153L, 48901554L,
55307000L, 55748704L, 55148495L, 55142182L, 59234553L, 58899315L,
56227874L, 60400360L, 58276635L, 59156435L, 62749684L, 59854458L,
64461004L, 64968014L, 65928553L, 66531676L, 67606088L, 70203145L,
67565561L, 72550880L, 71914026L, 73367953L, 79851397L, 72457238L,
74989033L), submissions = c(113L, 1075L, 2088L, 2439L, 3508L,
3714L, 5330L, 8128L, 9627L, 12683L, 12783L, 14941L, 17079L,
24899L, 42277L, 55163L, 38748L, 37220L, 36360L, 44261L, 47415L,
59045L, 62039L, 65580L, 62900L, 73995L, 84961L, 91521L, 101662L,
107142L, 110957L, 143287L, 147732L, 169897L, 168891L, 178929L,
192977L, 219036L, 213981L, 258796L, 284929L, 276948L, 287571L,
337394L, 334391L, 370394L, 367355L, 364984L, 392516L, 436016L,
445941L, 450769L, 468877L, 464344L, 498628L, 565217L, 518733L,
623178L, 634455L, 536295L, 499033L, 522386L, 561752L, 620834L,
653032L, 699986L, 750552L, 862205L, 837849L, 992189L, 984104L,
1101438L, 1172192L, 1281391L, 1474301L, 1503691L, 1627731L,
1666608L, 1796186L, 2028265L, 1996717L, 2188966L, 2315044L,
2336047L, 2430131L, 2727478L, 2822897L, 2645828L, 2848964L,
2769537L, 2802846L, 3224703L, 2969247L, 3286715L, 3458898L,
3334460L, 3290711L, 3470590L, 3404255L, 3342654L, 3657559L,
3617673L, 3897270L, 4311176L, 4258096L, 4457622L, 4256699L,
4327371L, 4248739L, 4654776L, 4760451L, 4736550L, 5021485L,
4977439L, 5156490L, 5705886L, 5298054L, 5851273L, 5819198L,
5979365L, 6061807L, 6143449L, 6390745L, 6524258L, 6690333L,
6578227L, 6762600L, 7814491L, 8011045L, 7427206L, 7358231L,
8018299L, 7952227L, 8157043L, 8430184L, 7943082L, 8657116L,
9036437L, 9275209L, 10079594L, 9141514L, 8320634L), votes_cast = c(1593,
7697, 21001, 25623, 38336, 53578, 140677, 251891, 281917,
354070, 412558, 527969, 508352, 670992, 916705, 872289, 969661,
1080387, 1000432, 1372822, 1418432, 1585620, 1870638, 2331648,
2298172, 2651797, 2712152, 3180503, 3140253, 3496048, 3966451,
4925835, 4929946, 4915463, 4153792, 4755783, 5567076, 6043979,
6311657, 7227888, 8008763, 7723053, 7969214, 9919486, 8544935,
9172783, 9684676, 10097245, 11040009, 12708002, 14921321,
15477325, 17129919, 16854507, 21118879, 22799456, 21307718,
23955640, 24015058, 24352578, 26711537, 29005400, 31540253,
36783663, 38717930, 42922895, 44721742, 51762788, 49547623,
60411718, 61635008, 76643286, 85136762, 91292574, 110235621,
111005564, 125330096, 131946776, 146746427, 170252197, 170174242,
192702118, 213439714, 227738040, 242492335, 270949434, 291082814,
276403018, 299625008, 303961085, 323300156, 367665686, 336187498,
386367195, 418442929, 417627992, 405759094, 420771491, 414721909,
389736074, 412008384, 422974888, 452019068, 477554760, 433357873,
476138296, 469425693, 469225454, 457406295, 489737473, 499537753,
481776130, 506470420, 507657165, 528570710, 572878135, 540936548,
613690994, 618378213, 622298648, 673896944.5, 716120978.6,
745813321.7, 788014837, 828691834, 842793081, 834890351,
914619383, 803209155, 825434703, 888020408, 936598904, 998290062,
863629375, 990641834, 842687530, 1118434460, 1243171165,
1266718016, 1362587431, 1296037003, 1290797207), date = structure(c(12935,
12965, 12996, 13027, 13057, 13088, 13118, 13149, 13180, 13208,
13239, 13269, 13300, 13330, 13361, 13392, 13422, 13453, 13483,
13514, 13545, 13573, 13604, 13634, 13665, 13695, 13726, 13757,
13787, 13818, 13848, 13879, 13910, 13939, 13970, 14000, 14031,
14061, 14092, 14123, 14153, 14184, 14214, 14245, 14276, 14304,
14335, 14365, 14396, 14426, 14457, 14488, 14518, 14549, 14579,
14610, 14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853,
14883, 14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126,
15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400,
15431, 15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675,
15706, 15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949,
15979, 16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222,
16252, 16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495,
16526, 16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770,
16801, 16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045,
17075, 17106, 17136, 17167, 17198, 17226), class = "Date", tzone = "America/New_York")), row.names = c(NA,
-142L), .Names = c("month", "accounts_created", "comments", "submissions",
"votes_cast", "date"), class = "data.frame")
显然服务器端没有完全充实,但是当我检查第一个复选框时,我无法显示情节。控制台中没有错误。不确定我做错了什么......
答案 0 :(得分:1)
以下代码有效。我刚刚消除了renderPlot
函数中的冗余被动(这本身就是一个急切的反应,与observe
相当)。
我对使用grepl
并不十分肯定,因此在此代码的上下文中我刚刚使用了if
。
请参阅此答案中的代码,了解一组复杂的组件,以解决checkboxGroupInput
的不同行为:checkBoxGroupInput and if statements (data included)
示例代码接近我的评论结尾。
library(shiny)
library(ggplot2)
library(anytime)
library(dplyr)
x<-read.csv("accounts_created_by_month.csv",header=T, stringsAsFactors = F)
x["date"]<-anydate(x$month)
ui <- fluidPage(
# Application title
title ="Reddit Data By Month",
plotOutput("graph"),
hr(),
fluidRow(
column(6,
dateRangeInput("dates", label = h2("Date Range"), start = x$date[1], end = x$date[142], min = x$date[1], max = x$date[142], format = "mm/yyyy", startview = "year")
),
column(6,
checkboxGroupInput("checkGroup", label = h2("Data Sets"),
choices = list("Accounts Created" = 1, "Comments" = 2, "Submissions" = 3, "Votes" = 4),
selected = NULL)
#NULL
)
)
)
server = function(input, output,session) {
subsetData <- reactive({
newData <- subset(x, date > as.Date(input$dates[1]) & as.Date(date) < as.Date(input$dates[2]))
return(newData)
})
output$graph <- renderPlot({
if(length(input$checkGroup) == 0) return()
graph <- ggplot()
if (input$checkGroup == 1){
graph+geom_line(data=subsetData(),aes(x=date,y=accounts_created,color="red"))
}
})
}
shinyApp(ui,server)