2013-11-21 10 views
10

私は輝くパッケージで初めての試みをしています。しかし、いつものように、私はいくつかの問題に直面しています。私は光沢のあるチュートリアルとgoogleingの後、異なるmeteoステーションの2つの異なるプロットを表示するように管理しましたが、両方とも同じ変数を示しています。選択リストから反応プロットへの光沢のあるパス変数

enter image description here

私はプロットされるべきVAR選択することが可能であるので、別の入力リストを追加したいです。スクリプトを実行しようとすると、エラーなしで実行されるように見えますが、プロットは表示されません。 enter image description here

おそらく、変数obliterをserver.Rに渡す際に間違いがあり、出力oblotプロットが適切に構築されていないと推測されます。入力VARSに応じたが、私は多分反応性について何か、多分正しく渡すVARSをしないのです関数を作成する一般的な方法で動作するようにしようとしました、...

これらは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

'プロット= P(入力$ var) 'が間違っている –

+0

はい、これは唯一の問題ではありませんでした。下の私の答えを見てください。ありがとう。 – pacomet

+0

'aes_string()'をよく知っておいて、この回答を投稿してください。 –

答えて

10

のために事前にありがとうございます。問題は両方の方法で変数がui.Rの入力からserver.Rに渡され、どのようにggplotがそのような入力を処理するかにあります。私はaes_stringを使って変数名を管理しなければならなかった。ここで、2つの最初の選択リストは、上/下のプロットで使用されるデータフレームを選択することを許可し、3番目のプロットは、異なるggplotコマンドを作成することによって、プロットされる変数を選択します。

スクリプトは現在動作していますが、コードを最適化するためのいくつかの問題がありますので、より一般的な方法で動作します。私はより大きなものをサブセット化することによって2つの異なるデータフレームを構築し、1つとサブセットのみを有する方がよいかもしれない。私は2つのプロット出力(myplot1とmyplot2)を定義しました。私は2つの条件付きパネルに対して1つだけを管理する方法を見つけることができませんでした。まだ仕事はありますが、光栄の初心者としては、私はそれが実行されて満足しています。いずれにせよ、私は私のために働いコードを添付し、それが誰かを助ける期待して

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) 
    }) 
}) 
関連する問題