2014-11-20 17 views
5

questa domanda è con riferimento a This SO questionCome posso costruire una lista selectInput lunga e quindi utilizzarlo per modificare le etichette in facet_wrap

La domanda di cui sopra è per quanto riguarda la modifica delle etichette di un facet_wrap, la risposta è perfetta - > aggiungi un'etichetta modificata come nuova colonna del set di dati.

Ora, il problema che sto affrontando è - seleziona

utente più variabili selectInput("select", h4("Variables:"), choices=var.both1(), selected=var.both1()[1], multiple=T, width="100%")

(es: permette di prendere in considerazione input$select lunghezza può essere 6) ora input$select contiene sei variabili, voglio verificare ogni unità variabili e assegnare, e posso parzialmente raggiungere questo obiettivo con il seguente componente reattiva

variableunit <- reactive ({ 
    if(input$select == "TEPC") {"degC"} 
    else if(input$select == "AT"){"µmol/kg"} 
    else if(input$select == "DIC" | input$select == "DIN" | input$select == "PIC" | input$select == "POC" | input$select == "PON" | input$select == "POP" | input$select == "DOC" | input$select == "DON" | input$select == "DOP" | input$select == "TEP"){c("µmol/L")} 
    else if(input$select == "Chla"){"µg/L"} 
    else ("Meters") 
}) 

il variableunit qui ottiene un singolo valore, anche l'utente inserisce 6 variabili, variableunit può darmi solo un singolo valore.

come posso avere una lista di 6 valori all'interno variableunit modo che io possa usarlo nel facet_wrap ggplot sotto

Il Codice

server <- function(input, output) { 

    a <- reactive({ 
    fileinput1 <- input$file1 
    if (is.null(fileinput1)) 
     return(NULL) 
    read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH")) 
    #read.table(fileinput1$datapath, header = TRUE, col.names = c("Experiment","Mesocosm","Hour","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","PAR","Temperature","Salinity","CO2atm","u10","DICflux","CO2ppm","CO2mol","pH")) 
    #a$Chla <- a$CHLphy + a$CHLcocco #Add new columns as per observation data 
    #a$PON <- a$Nphy + a$Nhet + a$Ndet + a$Ncocco 
    }) 

    output$showMapPlot <- renderUI({ 
{ list(plotOutput("plot",height="100%"), br()) } 
    }) 



    output$select <- renderUI({ 
    if(!is.null(a())){selectInput("select", h4("Variables:"), choices=names(a()), selected=NULL, multiple=T, width="100%")} 
    }) 


variableunit <- reactive ({ 
    if(input$select == "TEPC") {"degC"} 
    else if(input$select == "AT"){"µmol/kg"} 
    else if(input$select == "DIC" | input$select == "DIN" | input$select == "PIC" | input$select == "POC" | input$select == "PON" | input$select == "POP" | input$select == "DOC" | input$select == "DON" | input$select == "DOP" | input$select == "TEP"){c("µmol/L")} 
    else if(input$select == "Chla"){"µg/L"} 
    else ("Meters") 
}) 


    plot_4 <- function(var1 = input$select[1], var2 = input$select[2], var3 = input$select[3], var4 = input$select[4], var5 = input$select[5], var6 = input$select[6]) { 
    myvars <- c(var1,var2,var3,var4,var5,var6) 
    withProgress(message = 'Processing please wait...', value = 0, { 
    gg4 <- aggregate(cbind(get(var1),get(var2),get(var3),get(var4),get(var5),get(var6))~Mi+hours,a(), FUN=mean) 
    names(gg4)[3] <- var1 
    names(gg4)[4] <- var2 
    names(gg4)[5] <- var3 
    names(gg4)[6] <- var4 
    names(gg4)[7] <- var5 
    names(gg4)[8] <- var6 
    dd <- melt(gg4,id.vars=c("Mi","hours"), measure.vars=myvars) 
    dd$label <- paste(as.character(dd$variable), "(", (variableunit()), ")", sep="") 
    print(ggplot(dd,aes(x=hours, y=value)) + 
      geom_point(aes(color=factor(Mi)), size = 3, position = position_jitter(width = 0.1)) + 
      geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess", color = "blue") + 
      facet_wrap(~label, nrow=3, ncol=2,scales = "free_y") + scale_color_discrete("Mesocosm")) 
}) 
    } 

    output$plot <- renderPlot({ 
    if(length(input$select) == 6){ 
    plot_4() 
    } 
}, 
height=700, width=1100 
) 
} 

ui <- shinyUI(fluidPage(
    fluidRow(column(3, 
     uiOutput("showMapPlot"), 
     wellPanel(
     h4("Data Upload"), 
     fileInput('file1', h5('Choose Your Model Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT'))), 
     wellPanel(h4("Variable selection"),uiOutput("select")) 

    ), 
    column(9, 
      tabsetPanel(
      tabPanel("Conditional Plots",plotOutput("plot",height="auto"),value="barplots"), 
      id="tsp")) 
) 
)) 

shinyApp(ui = ui, server = server) 

file da caricare Download here

Basta copiare incollare il codice ed eseguirlo.

Ora il problema è la prima unità di variabili che si sta ripetendo per tutti gli altri grafici. So che questo è il problema con il componente reattivo che sto usando per ottenere le unità delle variabili.

La domanda Ora, come si fa?

Sono bloccato da molto tempo, sperando davvero che qualcuno conosca la soluzione alternativa. Grazie.

+0

Hai provato a usare 'reactiveValues ​​()'? Ad esempio: 'values ​​<- reactiveValues ​​()'. All'interno della tua funzione reattiva: 'variableUnit <- reactive ({if (input $ select ==" TEPC ") {valori $ var1 <-" degC "}}' –

+0

@ MikaelJumppanen va bene, ma ggplot inizia a tracciare una volta che ottiene tutti e sei variabili .... quindi se input $ select ha finalmente 6 valori, allora come posso gestire il methond di cui sopra in questo caso. (in definitiva, come posso avere una lista di 6 valori all'interno di variableunit, in base ai 6 valori selezionati dall'utente?) – statisticalbeginner

+0

In 'selectInput()' c'è una sola variabile selezionata al momento e l'input $ select restituirà quell'unico valore selezionato –

risposta

2

Ho provato con la risposta sopra e finirò per ottenere questo errore "Errore: nrow * ncol> = n non è VERO". Per favore fatemi sapere se qualcuno conosce il lavoro in giro.

server <- function(input, output) { 


    #File Upload 
    a <- reactive({ 
    fileinput1 <- input$file1 
    if (is.null(fileinput1)) 
     return(NULL) 
    read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH")) 
    }) 


    #Plot 
    output$showMapPlot <- renderUI({ 
{ list(plotOutput("plot",height="100%"), br()) } 
    }) 


    #Variable Input 
    output$select <- renderUI({ 
    if(!is.null(a())){selectInput("select", h4("Variables:"), choices=names(a())[-c(1,2,3)], selected=NULL, multiple=T, width="100%")} 
    }) 




    #Function to plot the variables 
    plot_4 <- function(var1 = input$select[1], var2 = input$select[2], var3 = input$select[3], var4 = input$select[4], var5 = input$select[5], var6 = input$select[6]) { 
    myvars <- c(var1,var2,var3,var4,var5,var6) 
    withProgress(message = 'Processing please wait...', value = 0, { 
    gg4 <- aggregate(cbind(get(var1),get(var2),get(var3),get(var4),get(var5),get(var6))~Mi+hours,a(), FUN=mean) 
    names(gg4)[3] <- var1 
    names(gg4)[4] <- var2 
    names(gg4)[5] <- var3 
    names(gg4)[6] <- var4 
    names(gg4)[7] <- var5 
    names(gg4)[8] <- var6 
    dd <- melt(gg4,id.vars=c("Mi","hours"), measure.vars=myvars) 


    #Reactive element to get the unit corresponding to the variable entered 
    variableunit <- reactive({ 
     test <- c("TEPC", "Chla", "DIN", "PIC", "AI", "PON")  
     values <- list() 
     for(i in 1:length(test)) { 

     if(test[[i]] == "TEPC") { 
      values[[i]] <-"degC" 
      next 
     }else if(test[[i]] == "AT"){ 
      values[[i]] <-"µmol/kg" 
      next 
     }else if(test[[i]] == "DIC" | test[[i]] == "DIN" | test[[i]] == "PIC" | test[[i]] == "POC" | test[[i]] == "PON" | test[[i]] == "POP" | test[[i]] == "DOC" | test[[i]] == "DON" | test[[i]] == "DOP" | test[[i]] == "TEP"){ 
      values[[i]] <-"µmol/L" 
     }else if(test[[i]] == "Chla"){ 
      values[[i]] <-"µg/L" 
     }else{ 
      values[[i]] <-"Meters" 
     } 
     } 

     return(values) 
     #return(paste(as.character(test), "(",values,")", sep="")) 
     #dd$label <- paste(as.character(test), "(",values,")", sep="") 
    }) 

    print(paste(variableunit())) 
    dd$label <- paste(as.character(dd$variable), "(", variableunit(), ")", sep="") 
    #dd$label <- variableunit() 

    print(names(dd)) 
    #print(unique(dd$variable)) 
    #print(unique(dd$value)) 
    print(ggplot(dd,aes(x=hours, y=value)) + 
      geom_point(aes(color=factor(Mi)), size = 3, position = position_jitter(width = 0.1)) + 
      geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess", color = "blue") + 
      facet_wrap(~label, nrow=3, ncol=2,scales = "free_y") + scale_color_discrete("Mesocosm")) 
}) 
    } 

    output$plot <- renderPlot({ 
    if(length(input$select) == 6){ 
    plot_4() 
    } 
}, 
height=700, width=1100 
) 
} 

ui <- shinyUI(fluidPage(
    fluidRow(column(3, 
     uiOutput("showMapPlot"), 
     wellPanel(
     h4("Data Upload"), 
     fileInput('file1', h5('Choose Your Model Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT'))), 
     wellPanel(h4("Variable selection"),uiOutput("select")) 

    ), 
    column(9, 
      tabsetPanel(
      tabPanel("Conditional Plots",plotOutput("plot",height="auto"),value="barplots"), 
      id="tsp")) 
) 
)) 

shinyApp(ui = ui, server = server) 

non ho potuto ottenere quello che volevo con i dati fuso, ma nel mio caso sto facendo aggregata dei dati allora la fusione.Quindi ho appena cambiato i dati in qualsiasi modo volessi dopo l'aggregato stesso e prima di fondere, quindi tutti i nomi delle variabili sono ora aggiornati e pronti per essere inseriti nei pannelli di sfaccettatura. Di seguito si riporta il codice:

server <- function(input, output) { 


    #File Upload 
    a <- reactive({ 
    fileinput1 <- input$file1 
    if (is.null(fileinput1)) 
     return(NULL) 
    read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH")) 
    }) 


    #Plot 
    output$showMapPlot <- renderUI({ 
{ list(plotOutput("plot",height="100%"), br()) } 
    }) 


#Variable Input 
output$select <- renderUI({ 
    if(!is.null(a())){selectInput("select", h4("Variables:"), choices=names(a())[-c(1,2,3)], selected=NULL, multiple=T, width="100%")} 
}) 

#Reactive Element to update the units 
variableunit <- reactive({ 
    #test <- c("TEPC", "Chla", "DIN", "PIC", "AI", "PON")  
    test <- input$select 
    values <- list() 
    for(i in 1:length(test)) { 

    if(test[[i]] == "TEPC") { 
     values[[i]] <-"degC" 
     next 
    }else if(test[[i]] == "AT"){ 
     values[[i]] <-"µmol/kg" 
     next 
    }else if(test[[i]] == "DIC" | test[[i]] == "DIN" | test[[i]] == "PIC" | test[[i]] == "POC" | test[[i]] == "PON" | test[[i]] == "POP" | test[[i]] == "DOC" | test[[i]] == "DON" | test[[i]] == "DOP" | test[[i]] == "TEP"){ 
     values[[i]] <-"µmol/L" 
    }else if(test[[i]] == "Chla"){ 
     values[[i]] <-"µg/L" 
    }else{ 
     values[[i]] <-"Meters" 
    } 
    } 

    return(values) 
}) 



#Function to plot the variables 
plot_4 <- function(var1 = input$select[1], var2 = input$select[2], var3 = input$select[3], var4 = input$select[4], var5 = input$select[5], var6 = input$select[6]) { 
    myvars <- c(var1,var2,var3,var4,var5,var6) 
    withProgress(message = 'Processing please wait...', value = 0, { 
    gg4 <- aggregate(cbind(get(var1),get(var2),get(var3),get(var4),get(var5),get(var6))~Mi+hours,a(), FUN=mean) 
    names(gg4)[3] <- paste(var1,"(",variableunit()[1],")") 
    names(gg4)[4] <- paste(var2,"(",variableunit()[2],")") 
    names(gg4)[5] <- paste(var3,"(",variableunit()[3],")") 
    names(gg4)[6] <- paste(var4,"(",variableunit()[4],")") 
    names(gg4)[7] <- paste(var5,"(",variableunit()[5],")") 
    names(gg4)[8] <- paste(var6,"(",variableunit()[6],")") 
    dd <- melt(gg4,id.vars=c("Mi","hours"), measure.vars=c(names(gg4)[3],names(gg4)[4],names(gg4)[5],names(gg4)[6],names(gg4)[7],names(gg4)[8])) 

    print(ggplot(dd,aes(x=hours, y=value)) + 
      geom_point(aes(color=factor(Mi)), size = 3, position = position_jitter(width = 0.1)) + 
      geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess", color = "blue") + 
      facet_wrap(~variable, nrow=3, ncol=2,scales = "free_y") + scale_color_discrete("Mesocosm")) 
    }) 
} 

output$plot <- renderPlot({ 
    if(length(input$select) == 6){ 
    plot_4() 
    } 
}, 
height=700, width=1100 
) 
} 

ui <- shinyUI(fluidPage(
    fluidRow(column(3, 
        uiOutput("showMapPlot"), 
        wellPanel(
        h4("Data Upload"), 
        fileInput('file1', h5('Choose Your Model Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT'))), 
        wellPanel(h4("Variable selection"),uiOutput("select")) 

), 
    column(9, 
     tabsetPanel(
      tabPanel("Conditional Plots",plotOutput("plot",height="auto"),value="barplots"), 
      id="tsp")) 
) 
)) 

shinyApp(ui = ui, server = server) 

Se qualcuno conosce un modo per raggiungere questo obiettivo dopo la fusione dei dati, allora per favore fatemelo sapere. Grazie.

2

D: "come posso avere una lista di 6 valori all'interno variableunit modo che io possa utilizzarlo nel sotto facet_wrap ggplot"

A: Si può avere la lista dei 6 valori all'interno funzione reattiva. Utilizzare per passare dal ciclo input$select. E salva l'unità corrispondente allo stesso indice in una lista.

server <- function(input, output) { 

     variableunit <- reactive({ 
     test <- c("TEPC", "Chla", "DIN", "PIC", "AI", "PON")  
     values <- list() 
     for(i in 1:length(test)) { 

     if(test[[i]] == "TEPC") { 
     values[[i]] <-"degC" 
     }else if(test[[i]] == "AT"){ 
     values[[i]] <-"µmol/kg" 
     }else if(test[[i]] == "DIC" | test[[i]] == "DIN" | test[[i]] == "PIC" | test[[i]] == "POC" | test[[i]] == "PON" | test[[i]] == "POP" | test[[i]] == "DOC" | test[[i]] == "DON" | test[[i]] == "DOP" | test[[i]] == "TEP"){ 
     values[[i]] <-"µmol/L" 
     }else if(test[[i]] == "Chla"){ 
     values[[i]] <-"µg/L" 
     }else{ 
     values[[i]] <-"Meters" 
     } 
    } 

    return(paste(as.character(test), "(",values,")", sep="")) 
}) 

     output$text <- renderText({ 
     variableunit() 
     print(paste(variableunit())) 
     }) 
    } 

    ui <- shinyUI(fluidPage(
     sidebarLayout(
     sidebarPanel(

     ), 
     mainPanel(textOutput("text")) 
    ) 
    )) 

    shinyApp(ui = ui, server = server) 

Questo esempio rende testo: TEPC (C) CHLA (ug/L) DIN (mol/L) PIC (mol/L) AI (tester) PON (mol/L)

+0

Grazie ... dopo aver generato le unità devo aggiornarle alle variabili corrispondenti ... che sto cercando di fare con paste (come.character (variabile dd $), "(", (variableunit()), ") ", sep =" ") ma finirò con errore Errore: nrow * ncol> = n non è TRUE – statisticalbeginner

+0

Scusa @statisticalbeginner, non mi hai evidenziato usando @ così non ho notato il tuo commento. Se modifico la mia risposta a 'return (incolla (as.character (test)," (", values,") ", sep =" "))' stampa: TEPC (degC) Chla (μg/L) DIN (μmol/L) PIC (μmol/L) AI (Metri) PON (μmol/L) è quello che vuoi? –

+0

L'errore si verifica perché la funzione 'variableunit' non restituisce solo una variabile. Nella mia risposta ho modificato la funzione in modo che restituisca la lista. –

Problemi correlati