0
votes

Recently I'm working on the data analysis on cancer drugs using the "shiny" packages. In the third tab, I'd like to aggregate and sum the values of each year into those of a certain range of years, eg. into two years like 1984~1985, 1986~1987, ... or into five years like 1984~1988, 1989~1993,..., etc. The beginning year and the last year should be flexible, too. In that case, if I set the interval to 6 years and choose the beginning year as 1990 and the last year as 2012, the x axis of the heatplot should be presented as 1990~1995, 1996~2001, 2002~2007, 2008~2012(only five years rather than six). It's quite complicated for me and I've search for the answers yet cannot find a relatable one.

The code is as the followings. And the data is shared by the hyperlink. Thanks!

library(ggplot2)
library(gapminder)
library(magrittr)
library(shiny)
library(tidyverse)
library(reshape)
library(tm)
library(dplyr)
library(data.table)
my_data=drug %>% distinct(drugbank, cancerType, .keep_all = TRUE)
my_druglist=my_data[!is.na(my_data$drugbank), ]
my_druglist=as.data.frame(gsub("[[:punct:]]", "", as.matrix(my_druglist))) 
A=table(my_druglist$cancerType)
B=as.data.frame(A)
Z=table(my_druglist$drugbank)
Y=as.data.frame(Z)
X=table(Y$Freq)
V=as.data.frame(X)
M=table(my_druglist$drugName,my_druglist$PMIDYear)
data_melt=melt(M)
ui = fluidPage(
  tags$script(type="text/javascript", src = "tabinfo.js"),
  titlePanel(div("藥物數據分析")),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      tabsetPanel(              
        tabPanel(title = "各癌症藥物種數",
          selectInput(inputId="variable", label="Options:",
            choices=c("descending order"="reorder(Var1,Freq)","ascending order"="reorder(Var1,-Freq)")
          ),
          plotOutput(outputId ="CancerType")
        ),
      tabPanel(title = "藥物與文獻的關係",
        plotOutput(outputId ="papers")
        ),
      tabPanel(title = "每藥物於各年度的文獻發表數",
        sliderInput(inputId = "num",
                           label = "set the interval of years:",
                           value = 3, min = 1, max = 10),
        selectInput(inputId="start", label="The beginning year:",
                           selected=min(as.numeric(my_druglist$PMIDYear)),
                           choices=sort(unique(as.numeric(my_druglist$PMIDYear)))),
        selectInput(inputId="end", label="The last year:",
                           selected=max(as.numeric(my_druglist$PMIDYear)),
                           choices=sort(unique(as.numeric(my_druglist$PMIDYear)))),
        selectInput(inputId="order", label="Order:",
                           choices=c("from A to Z"="reorder(Var.1, desc(Var.1))","from Z to A"="Var.1","ascending order"="reorder(Var.1,-value)","descending order"="reorder(Var.1,value)")),
        plotOutput(outputId ="publishment")
        )
      )
    )
  )
)
server = function(input, output) {
  output$CancerType <- renderPlot({
    ggplot(data=D) +
      aes_string(x="Freq", y=input$variable)+
      geom_bar(stat="identity") +
      geom_text(aes(label=Freq), vjust=0, hjust=-0.5, size=3, col="red")+
      theme_minimal() +
      ggtitle("各癌症藥物種數") +
      xlab("藥物種數") + 
      ylab("癌症種類")
  },height =1200)
  output$papers <- renderPlot({
    ggplot(data=V, aes(x=Freq, y=Var1)) +
      geom_bar(stat="identity") +
      geom_text(aes(label=Freq), vjust=-0.5, hjust=0.5, size=4, col="red")+
      theme_minimal() +
      coord_flip()+
      ggtitle("藥物與文獻的關係") +
      ylab("藥物已有文獻之癌症數") + 
      xlab("藥物數")
  })
  output$publishment <- renderPlot({
    ggplot(data_melt%>% filter(Var.2 %in% (as.numeric(input$start):as.numeric(input$end))), aes_string("Var.2", input$order)) + 
      geom_tile(aes(fill=value))+
      labs(x="Years", y="Drugs", title="每藥物於各年度的文獻發表數")+
      scale_fill_gradient(low="white", high="royalblue4")+
      theme(axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.5))
  },height =4000)
}
shinyApp(ui = ui, server = server)

translation: 藥物數據分析 the data analysis on cancer drugs 各癌症藥物種數 the number of drug species for each cancer 藥物與文獻的關係 the correlation between drugs and papers 每藥物於各年度的文獻發表數 the number of papers published for each drug per year 藥物(種)數 the number of drug species 癌症種類 cancer type 藥物已有文獻之癌症數 the number of cancers based on papers for drugs

1

1 Answers

0
votes

The easiest way to do this is to employ the "%/%", which generates the integer after dividing the value by a specified number. And then, multiply that integer by that specified number. Here's the code:

N$Var2=(as.numeric(as.numeric_version(N$Var2))%/%input$num)*input$num
Q=merge(x=aggregate(Freq~Var1+Var2,N,sum),y=N[,c("Var1","labels")],by="Var1", all.x = TRUE)

Hope this can help anyone who have the similar problem, thanks!