2013-11-21 10 views
10

पर चमकदार पास वैरिएबल मैं चमकीले पैकेज के साथ अपना पहला प्रयास कर रहा हूं, यह कमाल है। लेकिन, हमेशा की तरह, मुझे कुछ समस्याएं आ रही हैं। मैंने विभिन्न मीटियो स्टेशनों के दो अलग-अलग भूखंड दिखाने के लिए चमकदार ट्यूटोरियल और Googleिंग का पालन किया है, लेकिन दोनों एक ही चर दिखा रहे हैं।आर चुनिंदा सूची से प्रतिक्रियाशील साजिश

enter image description here

मैं किसी अन्य इनपुट सूची में जोड़ने के लिए इसलिए यह संभव है चुनने के लिए जो वर साजिश रची जा रहा है चाहता था। स्क्रिप्ट चलाने की कोशिश करते समय ऐसा लगता है कि बिना किसी त्रुटि के चलते हैं लेकिन कोई प्लॉट दिखाई नहीं देता है, केवल चुनिंदा मेनू। enter image description here

शायद सर्वर पर चर पारित करने में कोई गलती है। इसलिए आउटपुट ऑब्जेक्ट प्लॉट ठीक से निर्माण नहीं कर रहे हैं, बस एक अनुमान है। वार्स, लेकिन मैं कुछ याद आ रही है, हो सकता है वे प्रतिक्रियाओं के बारे में, हो सकता है सही ढंग से वार्स गुजर इनपुट के आधार पर एक सामान्य तरीके से काम करता है बनाने में काम करने के लिए कोशिश की, ...

ये ui.R

library("shiny") 
shinyUI(pageWithSidebar(

    headerPanel('Comparación de zonas - Temperatura'), 

    sidebarPanel(
    selectInput("panel1", "Zona:", 
          list("Zona 1" = "1", 
           "Zona 2" = "2", 
           "Zona 3" = "3", 
           "Zona 4" = "4")), 
    selectInput("panel2", "Zona:", 
       list("Zona 1" = "1", 
        "Zona 2" = "2", 
        "Zona 3" = "3", 
        "Zona 4" = "4")), 
    selectInput("var", "Variable:", 
       list("tempc" = "tempc", 
        "relhum" = "relhum")),  
    helpText('Al seleccionar la zona se crearán automáticamente 
    el gráfico de evolución temporal.') 
), 

    mainPanel(
    conditionalPanel(condition = "inputId == 'panel1'",plotOutput('myplot') 
    ), 
    conditionalPanel(condition = "inputId == 'panel2'",plotOutput("myplot") 
    )  
) 
)) 

के लिए कोड हैं और server.R

library(shiny) 
library(plyr) 
library(ggplot2) 

shinyServer(function(input, output) { 

    formulaText <- reactive(function() { 
    paste("Gràfica de ggplot: Zona ", input$zona1) 
    }) 

    # Return the formula text for printing as a caption 
    output$caption <- reactiveText(function() { 
    formulaText() 
    }) 

    # datasets 
    datos=read.table("data.dat",header=T) 
    data=as.data.frame(datos) 
    data=within(data, datetime <- as.POSIXct(paste(date, time),format = "%Y%m%d %H%M%S")) 

    rams <- reactive({ 
    subset(data,data$stat_id %in% places$stat_id[places$Zona == input$panel1])  
    }) 


    plot <- function(var) { 
    p <- ggplot(rams(),aes(x=datetime, y=var, colour=as.character(stat_id))) + 
     geom_line() 
    } 

    plot=p(input$var) 

    if(input$var == "tempc") { 
    plot <- plot + ylab("Temperatura (ºC)") + xlab(" ") + 
     ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") + 
     scale_y_continuous(limits = c(-20,ylim),breaks=c(seq(-20,ylim,by=2))) } 

    if (input$var == "relhum") { 
    plot <- plot + 
     ylab("Humedad relativa (%)") + xlab(" ") + 
     ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") + 
     scale_y_continuous(limits = c(0,100),breaks=c(seq(0,100,by=5))) } 

    output$myplot <- reactivePlot(function() { 
    print(plot) 
    }) 

}) 

आपकी मदद और सलाह

+0

'साजिश = पी (इनपुट $ var) 'गलत है –

+0

हां, लेकिन यह एकमात्र समस्या नहीं थी। नीचे मेरा जवाब देखें। धन्यवाद। – pacomet

+0

इस उत्तर को पोस्ट करने के लिए आपके पास 'aes_string()' जानना अच्छा है। –

उत्तर

10

अंत में मैं सफल रहा है के लिए अग्रिम धन्यवाद। समस्याएं दोनों तरह से होती हैं जैसे यूई.आर इनपुट से सर्वर। आर इनपुट और इस तरह के इनपुट के साथ ggplot कैसे सौदों। मुझे चर नामों को प्रबंधित करने के लिए aes_string का उपयोग करना पड़ा है। अब, दो पहली चुनिंदा सूची ऊपरी/निचले भूखंडों में उपयोग किए गए डेटा फ्रेम का चयन करने की अनुमति देती है जबकि तीसरा चरम को अलग-अलग ggplot कमांड बनाने के माध्यम से प्लॉट किया जाता है।

हालांकि स्क्रिप्ट अभी काम कर रही है, फिर भी कोड को अनुकूलित करने के लिए कुछ समस्याएं हैं, इसलिए यह एक सामान्य तरीके से काम कर सकती है। मैं बड़े से सबसेट करके दो अलग-अलग डेटा फ्रेम बना देता हूं और शायद केवल एक और सबसेट होना बेहतर होता है। मैंने दो साजिश आउटपुट (myplot1 और myplot2) को परिभाषित किया है क्योंकि मुझे नहीं पता था कि दो सशर्त पैनलों के लिए केवल एक को कैसे प्रबंधित किया जाए। अभी भी काम है लेकिन चमकदार में नौसिखिया के रूप में मुझे खुशी है कि यह चल रहा है।

किसी भी मामले में मैं मेरे लिए काम कर कोड को देते हैं और उम्मीद करते हैं कि यह मदद मिलेगी किसी:

ui.R

library("shiny") 
shinyUI(pageWithSidebar(

    headerPanel('Comparación de zonas - Temperatura'), 

    sidebarPanel(
    selectInput("panel1", "Zona:", 
          list("Zona 1" = "1", 
           "Zona 2" = "2", 
           "Zona 3" = "3")), 
    selectInput("panel2", "Zona:", 
       list("Zona 1" = "1", 
        "Zona 2" = "2", 
        "Zona 3" = "3")), 
    selectInput("var", "Variable:", 
       list("tempc" = "tempc", 
        "relhum" = "relhum")),  
    helpText('Al seleccionar la zona se crearán automáticamente 
    el gráfico de evolución temporal.') 
), 

    mainPanel(
    conditionalPanel(condition = "inputId == 'panel1'",plotOutput(outputId='myplot1')), 
    conditionalPanel(condition = "inputId == 'panel2'",plotOutput(outputId='myplot2')) 
) 
)) 

server.R

library(shiny) 
library(plyr) 
library(ggplot2) 

shinyServer(function(input, output) { 

    datos=read.table("data.dat",header=T) 
    pobles=read.table("pobles-zona.dat",header=T) 

    data=as.data.frame(datos) 
    places=as.data.frame(pobles) 

    data$time[data$time == "0"] = "000000" 
    data$time[data$time == "10000"] = "010000" 
    data$time[data$time == "20000"] = "020000" 
    data$time[data$time == "30000"] = "030000" 
    data$time[data$time == "40000"] = "040000" 
    data$time[data$time == "50000"] = "050000" 
    data$time[data$time == "60000"] = "060000" 
    data$time[data$time == "70000"] = "070000" 
    data$time[data$time == "80000"] = "080000" 
    data$time[data$time == "90000"] = "090000" 

    data=within(data, datetime <- as.POSIXct(paste(date, time),format = "%Y%m%d %H%M%S")) 

    formulaText <- reactive(function() { 
    paste("Gràfica de ggplot: Zona ", input$panel1, input$panel2, input$var) 
    }) 

    # Return the formula text for printing as a caption 
    output$caption <- reactiveText(function() { 
    formulaText() 
    }) 

    rams1 <- reactive({ 
    subset(data,data$stat_id %in% places$stat_id[places$Zona == input$panel1])  
    }) 
    rams2 <- reactive({ 
    subset(data,data$stat_id %in% places$stat_id[places$Zona == input$panel2])  
    }) 

    p <- function(data){ 
    p=ggplot(data(),aes_string(x="datetime", y=input$var,colour="as.character(stat_id)")) + 
    geom_line() 
    } 

    output$myplot1 <- reactivePlot(function() { 

    gtitol=paste("Zona ",input$panel1) 
    yx=round(max(rams1()$tempc)+2) 
    yn=round(min(rams1()$tempc)-2) 

    plot=p(rams1) 

    if (input$var == "tempc") { 
     plot=plot + ylab("Temperatura (ºC)") + xlab(" ") + 
     ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") + 
     scale_y_continuous(limits = c(yn,yx),breaks=c(seq(yn,yx,by=2))) 
    } 

    if (input$var == "relhum" ){ 
     plot=plot + ylab("Humedad relativa (%)") + xlab(" ") + 
     ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") + 
     scale_y_continuous(limits = c(0,100),breaks=c(seq(0,100,by=5))) 
    } 
    print(plot) 
    }) 

    output$myplot2 <- reactivePlot(function() { 

     gtitol=paste("Zona ",input$panel2) 
     yx=round(max(rams2()$tempc)+2) 
     yn=round(min(rams2()$tempc)-2)  

     plot=p(rams2) 

     if (input$var == "tempc") { 
     ylim=max(rams2()$tempc)+2 
     plot=plot + ylab("Temperatura (ºC)") + xlab(" ") + 
     ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") + 
     scale_y_continuous(limits = c(yn,yx),breaks=c(seq(yn,yx,by=2))) 
     } 
     if (input$var == "relhum" ) { 
     ylim=100 
     plot=plot + ylab("Humedad relativa (%)") + xlab(" ") + 
     ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") + 
     scale_y_continuous(limits = c(0,100),breaks=c(seq(0,100,by=5))) 
     } 
    print(plot) 
    }) 
}) 
संबंधित मुद्दे