lundi 25 septembre 2017

checkboxGroupInput and plotting in shiny

In my shiny app I have a checkboxGroupInput

How should I do the plot command in server function, in a way that I plot the TurbInt_mean against MeanWindSpeed_mean and add lines (curves) to the plot by user selection ?

I have tried to summaries my shiny app as reproduce-able code as follow (you have to first load the sample data that I have provided)

library(shiny)
ui <- fluidPage(
  checkboxGroupInput("variable", "Select IEC Classes for TI",c("A Plus" = "ap","A" = "a","B" = "b","C"="c")),
  plotOutput("plotmeanTI",width = "100%") )

server <- function(input, output, session){


  output$plotmeanTI <- renderPlot({
  plot(as.matrix(TI_plot[,1]),as.matrix(TI_plot[,2]),t='o',ylim=c(0,1),xaxs="i",
       xlab="Mean Wind Speed", ylab="<TI>")

  if(input$variable=="ap"){lines(as.matrix(TI_plot[,1]),TI_plot$NTM_A_Plus_mean,col=6)}
  if(input$variable=="a"){lines(as.matrix(TI_plot[,1]),TI_plot$NTM_A_mean,col=2)}
  if(input$variable=="b"){lines(as.matrix(TI_plot[,1]),TI_plot$NTM_B_mean,col=3)}
  if(input$variable=="c"){lines(as.matrix(TI_plot[,1]),TI_plot$NTM_C_mean,col=4)}
})

}

shinyApp(ui=ui,server=server)

If user select 1, one curve should be added, if select 2, 2 curve and so on ! Any solution with ggplot is appreciated very much.

My data set looks like :

dput(TI_plot)
structure(list(MeanWindSpeed_mean = c(0.292023070097604, 1.12011882699226, 
2.0283906614786, 3.00947886508396, 4.01428066037736, 5.01250749719984, 
6.0080377166157, 7.00777409860191, 8.0049941822883, 9.00201938353988, 
9.99646762244478, 10.9883558855227, 11.9798700705476, 12.976996101646, 
13.9653724394786, 14.9495068163593, 15.9628459343795, 16.9708685581934, 
17.9623943661972, 18.992621231979, 19.9643220338983, 20.9834693877551, 
22.0170278637771, 22.9658904109589, 24.0025266903915, 24.9935025380711
), TurbInt_mean = c(3.02705430346051, 0.420402191213343, 0.264195029831388, 
0.215109260166585, 0.18794121258946, 0.16699392997796, 0.148261539245668, 
0.134479958525654, 0.122038442146089, 0.110595865904036, 0.097103704211826, 
0.0836329541372291, 0.0708397249149876, 0.0622491842333237, 0.0591184473929236, 
0.0611678829190056, 0.0652080242510699, 0.0690131441806601, 0.073762588028169, 
0.0756961992136304, 0.0805696610169492, 0.0817446428571429, 0.0830263157894737, 
0.0827277397260274, 0.0749537366548043, 0.0765532994923858), 
    NTM_A_Plus_mean = c(Inf, 1.10260388189292, 0.642329939163608, 
    0.473065816856713, 0.387417559923049, 0.336769624752903, 
    0.303163441845455, 0.27908457313955, 0.261084722917897, 0.247090026094941, 
    0.235918715179959, 0.226796351934008, 0.219190019655214, 
    0.212713243118379, 0.20720881268079, 0.202452008587075, 0.19816685602934, 
    0.19441329542209, 0.191131377464549, 0.188086340606011, 0.185500707351721, 
    0.18304730715887, 0.180790073836667, 0.178898058874634, 0.177002145398197, 
    0.175335040729601), NTM_A_mean = c(Inf, 0.98009233946037, 
    0.570959945923208, 0.420502948317078, 0.344371164376044, 
    0.299350777558136, 0.269478614973738, 0.248075176124045, 
    0.232075309260353, 0.219635578751059, 0.209705524604408, 
    0.201596757274674, 0.194835573026857, 0.189078438327448, 
    0.184185611271814, 0.179957340966289, 0.176148316470525, 
    0.172811818152969, 0.169894557746266, 0.167187858316455, 
    0.164889517645975, 0.162708717474551, 0.160702287854815, 
    0.159020496777452, 0.157335240353953, 0.155853369537423), 
    NTM_B_mean = c(Inf, 0.857580797027824, 0.499589952682807, 
    0.367940079777444, 0.301324768829038, 0.261931930363369, 
    0.23579378810202, 0.217065779108539, 0.203065895602809, 0.192181131407176, 
    0.183492334028857, 0.176397162615339, 0.1704811263985, 0.165443633536517, 
    0.161162409862837, 0.157462673345503, 0.154129776911709, 
    0.151210340883848, 0.148657738027983, 0.146289376026898, 
    0.144278327940228, 0.142370127790232, 0.140614501872963, 
    0.139142934680271, 0.137668335309708, 0.136371698345246), 
    NTM_C_mean = c(Inf, 0.735069254595278, 0.428219959442406, 
    0.315377211237809, 0.258278373282033, 0.224513083168602, 
    0.202108961230303, 0.186056382093034, 0.174056481945265, 
    0.164726684063294, 0.157279143453306, 0.151197567956005, 
    0.146126679770143, 0.141808828745586, 0.13813920845386, 0.134968005724717, 
    0.132111237352894, 0.129608863614727, 0.127420918309699, 
    0.125390893737341, 0.123667138234481, 0.122031538105913, 
    0.120526715891111, 0.119265372583089, 0.118001430265464, 
    0.116890027153068)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -26L), .Names = c("MeanWindSpeed_mean", 
"TurbInt_mean", "NTM_A_Plus_mean", "NTM_A_mean", "NTM_B_mean", 
"NTM_C_mean"))

the head of TI_plot is like :

head(TI_plot)
# A tibble: 6 x 6
  MeanWindSpeed_mean TurbInt_mean NTM_A_Plus_mean NTM_A_mean NTM_B_mean NTM_C_mean
               <dbl>        <dbl>           <dbl>      <dbl>      <dbl>      <dbl>
1          0.2920231    3.0270543             Inf        Inf        Inf        Inf
2          1.1201188    0.4204022       1.1026039  0.9800923  0.8575808  0.7350693
3          2.0283907    0.2641950       0.6423299  0.5709599  0.4995900  0.4282200
4          3.0094789    0.2151093       0.4730658  0.4205029  0.3679401  0.3153772
5          4.0142807    0.1879412       0.3874176  0.3443712  0.3013248  0.2582784
6          5.0125075    0.1669939       0.3367696  0.2993508  0.2619319  0.2245131




Aucun commentaire:

Enregistrer un commentaire