app.R 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. # * Author: Bangyou Zheng ([email protected])
  2. # * Created: 01:16 PM Wednesday, 17 June 2015
  3. # * Copyright: AS IS
  4. library(shiny)
  5. library(dplyr)
  6. library(magrittr)
  7. library(ggplot2)
  8. library(readxl)
  9. source('barcode.R')
  10. source('labels.R')
  11. designs <- read_excel('design.xlsx', 'Design')
  12. names(designs) <- tolower(names(designs))
  13. traits <- read_excel('design.xlsx', 'Traits')
  14. names(traits) <- tolower(names(traits))
  15. trials_name <- unique(designs$trial)
  16. genotypes <- unique(designs$genotype)
  17. measurements <- traits$measurement %>% unique
  18. researchers <- read_excel('design.xlsx', 'Researchers') %>% use_series('name')
  19. traits_sel <- traits %>%
  20. filter(measurement %in% measurements[1]) %>%
  21. use_series(name)
  22. # Create the basic plot
  23. pd_grid <- expand.grid(
  24. row = seq(min(designs$row), max(designs$row)),
  25. column = seq(min(designs$column), max(designs$column))) %>%
  26. mutate(row = as.factor(row),
  27. column = as.factor(column))
  28. p_grid <- ggplot(pd_grid) +
  29. geom_tile(aes(row, column),
  30. colour = 'gray',
  31. alpha = 0) +
  32. # geom_tile(aes(row, column),
  33. # colour = 'gray',
  34. # alpha = 0.3, data = pd_fill) +
  35. guides(fill = FALSE,
  36. colour = FALSE) +
  37. theme_bw() +
  38. xlab('Row') + ylab('Column') +
  39. theme(panel.grid = element_blank(),
  40. legend.position = 'bottom')
  41. # Product id of avery
  42. avery_product <- c('L7163', 'L7651')
  43. ui <- pageWithSidebar(
  44. # Application title
  45. headerPanel('Labels with barcode'),
  46. # Sidebar with a slider input for number of observations
  47. sidebarPanel(
  48. # wellPanel(
  49. # submitButton('Update View')),
  50. wellPanel(
  51. selectInput('measurement', 'Select a measurement',
  52. measurements),
  53. selectInput('trial', 'Select trials:', trials_name,
  54. selected = trials_name[1],
  55. multiple = TRUE),
  56. radioButtons('double_plot', 'Filter double plot',
  57. c('min', 'max', 'all'), 'min', inline = TRUE),
  58. selectInput('genotype', 'Select genotypes:', genotypes,
  59. selected = genotypes,
  60. multiple = TRUE),
  61. selectInput('trait', 'Select a traits:', traits_sel,
  62. selected = traits_sel,
  63. multiple = TRUE),
  64. # measurement date
  65. dateInput('measure_date', 'Measurement date',
  66. format = 'dd/mm/yyyy'),
  67. # Names of researchers
  68. textInput('researcher', 'Type in researcher', researchers),
  69. # Product of avery
  70. selectInput('product', 'Select a product of Avery', avery_product),
  71. # File name without extensions
  72. textInput('filename', 'Type in filename without extension',
  73. 'measurement'),
  74. # Download label
  75. downloadButton('download_excel', 'Download excel file'),
  76. # Download label
  77. downloadButton('download_label', 'Download labels')
  78. ),
  79. wellPanel(
  80. helpText(p('Bangyou Zheng'))
  81. )
  82. ),
  83. mainPanel(
  84. plotOutput('labels_plot', height = '1000px')
  85. )
  86. )
  87. server <- function(input, output, session)
  88. {
  89. # Render the table for design
  90. # Change genotype by measurement and trials
  91. observe({
  92. # mearsure <- input$measurement
  93. # trial_sel <- input$trial
  94. # genotypes <- designs %>%
  95. # filter(trial %in% trial_sel) %>%
  96. # select(genotype) %>%
  97. # distinct() %>%
  98. # use_series(genotype)
  99. #
  100. # if (mearsure == 'qh_key')
  101. # {
  102. # g <- c('7770', '7770tin', 'Hartog', 'HartogNoVigour')
  103. # g <- g[g %in% genotypes]
  104. # } else {
  105. # g <- genotypes
  106. # }
  107. #
  108. # updateSelectInput(session, 'genotype',
  109. # selected = g)
  110. })
  111. # Change traits by measurement
  112. observe({
  113. # traits <- traits_qh
  114. # if (input$measurement == 'head_dev') {
  115. # traits <- traits_head_dev
  116. # }
  117. # updateSelectInput(session, 'trait',
  118. # choices = traits,
  119. # selected = traits)
  120. })
  121. # Filter by trial
  122. r_trials <- reactive({
  123. i_trial <- input$trial
  124. designs %>%
  125. filter(trial %in% i_trial)
  126. })
  127. # Filter by double plot
  128. r_doubleplot <- reactive({
  129. doubleplot <- r_trials()
  130. # save(list = ls(), file = 'tmp.Rdata')
  131. # stop('AAAAAAAAAAAAAAA')
  132. # load('tmp.Rdata')
  133. #
  134. if (input$double_plot == 'min') {
  135. doubleplot <- doubleplot %>%
  136. group_by(year, site, trial,
  137. genotype, treatment, replicate) %>%
  138. filter(row == min(row),
  139. column == min(column)) %>%
  140. ungroup()
  141. } else if (input$double_plot == 'max') {
  142. doubleplot <- doubleplot %>%
  143. group_by(year, site, trial,
  144. genotype, treatment, replicate) %>%
  145. filter(row == max(row),
  146. column == max(column)) %>%
  147. ungroup()
  148. }
  149. doubleplot
  150. })
  151. # Filter by genotypes
  152. r_genotypes <- reactive({
  153. # r_trials() %>%
  154. r_doubleplot() %>%
  155. filter(genotype %in% input$genotype)
  156. })
  157. # Filter by measurement
  158. r_measurement <- reactive({
  159. measurement <- r_genotypes()
  160. if (grepl('^qh.*', input$measurement)) {
  161. measurement <- measurement %>%
  162. group_by(year, site, trial,
  163. replicate, management,
  164. density, genotype) %>%
  165. filter(row == min(row),
  166. column == min(column))
  167. } else if ('head_dev' == input$measurement) {
  168. measurement <- measurement %>%
  169. group_by(year, site, trial,
  170. replicate, management,
  171. density, genotype) %>%
  172. filter(row == max(row),
  173. column == max(column))
  174. }
  175. measurement %>%
  176. arrange(column, row)
  177. })
  178. # Get the sie index
  179. r_siteidx <- reactive({
  180. return(2)
  181. })
  182. # Generate Barcode
  183. r_barcode <- reactive({
  184. designs <- r_measurement()
  185. measure_date = input$measure_date
  186. site_idx = r_siteidx()
  187. measure_traits = input$trait
  188. sample_num = 1
  189. # measure_traits_map = traits
  190. # save(list = ls(), file = 'tmp.Rdata')
  191. # stop()
  192. # load('Shiny/tmp.Rdata')
  193. bc <- createBarcode(
  194. designs = r_measurement(),
  195. measure_date = input$measure_date,
  196. site_idx = r_siteidx(),
  197. measure_traits = input$trait,
  198. sample_num = 1,
  199. measure_traits_map = traits
  200. )
  201. bc
  202. })
  203. # Show a table of plots for measurements
  204. output$labels_design = renderDataTable({
  205. labels_df <- r_measurement()
  206. labels_df %>%
  207. select(year, site, trial, row, column,
  208. replicate,
  209. management, density, genotype)
  210. })
  211. # plot the selected plots
  212. output$labels_plot <- renderPlot({
  213. library(ggplot2)
  214. labels_df <- r_measurement()
  215. labels_df <- labels_df %>%
  216. ungroup() %>%
  217. mutate(
  218. row = as.factor(row),
  219. column = as.factor(column),
  220. treatment = as.factor(treatment))
  221. p_grid +
  222. geom_tile(aes(row, column,
  223. fill = treatment),
  224. data = labels_df) +
  225. geom_text(aes(row, column, label = treatment),
  226. data = labels_df)
  227. })
  228. # Download excel file
  229. output$download_excel <- downloadHandler(
  230. filename = function() {
  231. paste0(input$filename,'.xlsx')
  232. },
  233. content = function(file) {
  234. bc <- r_barcode() %>%
  235. mutate(Value = '') %>%
  236. select(
  237. Year = year,
  238. Site = site,
  239. TrialCode = trial,
  240. Row = row,
  241. Column = column,
  242. Replicate = replicate,
  243. Density = density,
  244. Genotype = genotype,
  245. MeasureTrait,
  246. MeasureDate,
  247. Barcode,
  248. Value
  249. )
  250. library(XLConnect)
  251. newWB <- loadWorkbook(filename = file, create = TRUE)
  252. createSheet(newWB, name = 'Measurement')
  253. writeWorksheet(
  254. newWB,
  255. data = as.data.frame(bc),
  256. sheet = 'Measurement',
  257. header = TRUE,
  258. rownames = NULL)
  259. saveWorkbook(newWB)
  260. }
  261. )
  262. # Download labels
  263. output$download_label <- downloadHandler(
  264. filename = function() {
  265. paste0(input$filename,'.pdf')
  266. },
  267. content = function(file) {
  268. bc <- r_barcode()
  269. measure_traits <- input$trait
  270. researcher <- input$researcher
  271. product <- input$product
  272. bc$researcher <- researcher
  273. designs <- r_measurement()
  274. # save(list = ls(), file = 'tmp.RData')
  275. # stop('A')
  276. # load('Shiny/tmp.RData')
  277. averyLabel(
  278. bc, file,
  279. product = product)
  280. }
  281. )
  282. }
  283. shinyApp(ui = ui, server = server)