简体   繁体   中英

R shiny - perform seperate operations on loaded data with action buttons

I'm new at R shiny and I'm trying to develop an app which uploads files and combines the data. This is my code so far:

library(shiny)
library(DT)
library(leaflet)
library(tidyverse)
library(reshape2)

ui <- fluidPage(
  fileInput('SNP_raw_data', 'Choose SNP_raw_data to upload', accept = c('.csv')),
  fileInput('GPS_raw_data', 'Choose GPS_raw_data to upload', accept = c('.csv')),
  actionButton('long2wide_table_button', 'Show conversion long2wide as table'),
  actionButton('samples_map_button', 'Show map of listed samples'),
  DT::dataTableOutput("long2wide_table"),
  uiOutput("samples_map")
)

server <- function(input, output) {
  
  df_SNP_upload <- reactive({
    inFile_SNP <- input$SNP_raw_data
    if (is.null(inFile_SNP))
      return(NULL)
    df_SNP <- read.csv(inFile_SNP$datapath, header = TRUE,sep = input$separator)
    return(df_SNP)
  })
  
  df_GPS_upload <- reactive({
    inFile_GPS <- input$GPS_raw_data
    if (is.null(inFile_GPS))
      return(NULL)
    df_GPS <- read.csv(inFile_GPS$datapath, header = TRUE,sep = input$separator)
    return(df_GPS)
  })
  
  conversion_long2wide <- eventReactive(input$long2wide_table_button, {
    data_arranged <- df_SNP_upload() %>%
      select(ID, Assay, Name, Converted) %>%
      arrange(Assay)
    data_mutated <- data_arranged %>%
      mutate(data_arranged, Converted = gsub(":", "", data_arranged$Converted))
    NameID <- paste(data_arranged$Name, data_arranged$ID)
    Name_ID_long <- gsub(" ", "_", NameID)
    Name_ID <- gsub("....$", "", Name_ID_long)
    data_combined <- data_mutated[,c(2,4)] %>%
      add_column(Name_ID, .after=1)
    convA <- mutate(data_combined, Converted = gsub("A", "01", data_combined$Converted))
    convC <- mutate(convA, Converted = gsub("C", "02", convA$Converted))
    convG <- mutate(convC, Converted = gsub("G", "03", convC$Converted))
    convT <- mutate(convG, Converted = gsub("T", "04", convG$Converted))
    data_na <- mutate(convT, Converted = gsub("No 02all", "0000", convT$Converted))
    order0201 <- mutate(data_na, Converted = gsub("0201", "0102", data_na$Converted))
    order0301 <- mutate(order0201, Converted = gsub("0301", "0103", order0201$Converted))
    order0302 <- mutate(order0301, Converted = gsub("0302", "0203", order0301$Converted))
    order0401 <- mutate(order0302, Converted = gsub("0401", "0104", order0302$Converted))
    order0402 <- mutate(order0401, Converted = gsub("0402", "0204", order0401$Converted))
    order0403 <- mutate(order0402, Converted = gsub("0403", "0304", order0402$Converted))
    data_converted <- dcast(order0403, formula = Name_ID ~ Assay, value.var = "Converted")
    NameNameID <- data_mutated %>%
      add_column(Name_ID, .after=3) %>%
      select(Name, Name_ID) %>%
      unique() %>%
      rename(Sample_ID = Name) %>%
      arrange(Name_ID)
    Sample_ID <- NameNameID$Sample_ID
    data_converted2 <- data_converted %>%
      add_column(Sample_ID, .after = 1)
    coords_selected <- df_GPS_upload() %>%
      mutate(coordinates, Sample_ID = gsub(" ", "", coordinates$Sample_ID)) %>%
      select(Sample_ID, Population, Latitude, Longitude)
    data_joined <- left_join(data_converted2, coords_selected, by = "Sample_ID") %>%
      relocate(Population, .after = Name_ID) %>%
      relocate(Latitude, .after = Population) %>%
      relocate(Longitude, .after = Latitude)
    SNPs_Coords <- select(data_joined, -5)
    return(SNPs_Coords)
  })
  
  output$long2wide_table <- DT::renderDataTable({
    conversion_long2wide()
  })

  output$samples_map_button <- renderUI({
    
  })
}


shinyApp(ui = ui, server = server)

The file input is working well. However, afterwards I want to perform some operations on the data by using actionbuttons, like 'long2wide_table_button'. This somehow doesn't work, maybe I'm not sure how to wire these functions correctly. conversion_long2wide() is ment to be the central hub of integration, I think like data() in other apps. It should provide a dataframe on which I want to perform further operations, like mapping or statistics, by pushing actionbuttons. conversion_long2wide works well as a seperate script, but leeds to error warnings when integrated into the app, like 'column ID doesn't exist' or 'function select() not found', even though they are there. This is a sample of SNP data to upload:

        ID           Assay  Name Converted
1  S06-A01 LL_A1_01275_308  R289       A:G
2  S95-A01 LL_A1_01275_308  R419       A:G
3  S05-A01 LL_A1_01275_308  P573       A:G
4  S94-A01 LL_A1_01275_308  R345       A:A
5  S04-A01 LL_A1_01275_308  P098       A:G
6  S93-A01 LL_A1_01275_308  R337       G:G
7  S03-A01 LL_A1_01275_308 N0509       G:G
8  S92-A01 LL_A1_01275_308  R328       A:G
9  S02-A01 LL_A1_01275_308 N0494       A:G
10 S91-A01 LL_A1_01275_308  R320       G:G

... and the GPS data:

    Sample_ID  Population  Latitude Longitude
234      P573 Poland_east 52,814444 23,643333
353       R419 Russia_northeast   66,888081  136,176853
264       R289      Russia_west   59,988323   47,909056
303       R345      Russia_west   60,816269   48,402507
223      P098  Poland_east   52,891802    23,51427
295       R337      Russia_west   60,816269   48,402507
198     N0509       Norway   63,134665    8,477809
286       R328      Russia_west    59,80816   50,419522
183     N0494       Norway   60,611573     8,49342
278       R320      Russia_west    59,80816   50,419522

Many thanks in advance!

The input$GPS_raw_data returns a data.frame with the information needed to read in the data. You use this information to read in the selected file and store it in the reactive df_GPS_upload . Therefore, you can then access the data in this object:

conversion_long2wide <- eventReactive(input$long2wide_table_button, {
      data_arranged <- df_GPS_upload() %>%
            select(ID, Assay, Name, Converted) %>%
            arrange(Assay)
      return(data_arranged)
}

Because df_GPS_upload is a reactive, you need to use df_GPS_upload() to access the data.

The select_ error may come from the fact that you try to apply it to input$GPS_raw_data , see if it goes away if you use the above code. In general, there are no compatibility problems with the tidyverse and shiny .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM