2016-02-16 163 views
0
library(shiny) 
library(shinythemes) 
ui <- fluidPage(
sidebarLayout(
    sidebarPanel(radioButtons(
    "fiscal","Quarter:",c("2015Q1","2015Q2","2015Q3","2015Q4","2016Q1"))),      
    mainPanel({ 
    plotOutput('plot') 
    }))) 
server <- function (input , output ) 
{ 
myread <- function() 
{ 
inFile <- input$file1 
if (is.null(inFile)) 
return(NULL) 
mydata <- read.csv(inFile$datapath) 
return (mydata) 
} 

plotType <- function(type) 
{ 
    switch(type) 
     Q12015 <- { mydataq1 <- reactive (myread()) 
        x <- reactive(data.frame(mydataq1())) 
        y <- reactive ({x()[x()$Fiscal.Quarter=="2015Q1",] 
        resources <- factor (y()$Resource.Name) 
      stan <- tapply (y()$Standard.Hours,resources, sum , na.rm=TRUE) 
      bil <- tapply (y()$Billable.Hours,resources, sum , na.rm=TRUE) 
      bu <- bil*100/stan 
      mp <- barplot (bu,col=colors(27),las=2,yaxt="n",ylim=c(0,200)) 
      bu<- round(bu,2) 
      text(mp, bu,labels=bu, pos = 3) 
     }) 
     output$plot <- renderPlot ({ y() }) 

     } 
     Q22015= { mydataq1 <- reactive (myread()) 
     x <- reactive(data.frame(mydataq1())) 
     y <- reactive ({x()[x()$Fiscal.Quarter=="2015Q2",] 
      resources <- factor (y()$Resource.Name) 
      stan <- tapply (y()$Standard.Hours,resources, sum , na.rm=TRUE) 
      bil <- tapply (y()$Billable.Hours,resources, sum , na.rm=TRUE) 
      bu <- bil*100/stan 
      mp <- barplot (bu,col=colors(27),las=2,yaxt="n",ylim=c(0,200)) 
      bu<- round(bu,2) 
      text(mp, bu,labels=bu, pos = 3) 
     }) 
     output$plot <- renderPlot ({ y() }) 

     } 
     Q32015 = { mydataq1 <- reactive (myread()) 
     x <- reactive(data.frame(mydataq1())) 
     y <- reactive ({x()[x()$Fiscal.Quarter=="2015Q3",] 
      resources <- factor (y()$Resource.Name) 
      stan <- tapply (y()$Standard.Hours,resources, sum , na.rm=TRUE) 
      bil <- tapply (y()$Billable.Hours,resources, sum , na.rm=TRUE) 
      bu <- bil*100/stan 
      mp <- barplot (bu,col=colors(27),las=2,yaxt="n",ylim=c(0,200)) 
      bu<- round(bu,2) 
      text(mp, bu,labels=bu, pos = 3) 
     }) 
     output$plot <- renderPlot ({ y() }) 

     } 
     Q42015={ 
     mydataq1 <- reactive (myread()) 
     x <- reactive(data.frame(mydataq1())) 
     y <- reactive ({x()[x()$Fiscal.Quarter=="2015Q4",] 
      resources <- factor (y()$Resource.Name) 
      stan <- tapply (y()$Standard.Hours,resources, sum , na.rm=TRUE) 
      bil <- tapply (y()$Billable.Hours,resources, sum , na.rm=TRUE) 
      bu <- bil*100/stan 
      mp <- barplot (bu,col=colors(27),las=2,yaxt="n",ylim=c(0,200)) 
      bu<- round(bu,2) 
      text(mp, bu,labels=bu, pos = 3) 
     }) 
     output$plot <- renderPlot ({ y() }) 
     } 
     Q12016={ 
     mydataq1 <- reactive (myread()) 
     x <- reactive(data.frame(mydataq1())) 
     y <- reactive ({x()[x()$Fiscal.Quarter=="2015Q4",] 
     resources <- factor (y()$Resource.Name) 
     stan <- tapply (y()$Standard.Hours,resources, sum , na.rm=TRUE) 
     bil <- tapply (y()$Billable.Hours,resources, sum , na.rm=TRUE) 
     bu <- bil*100/stan 
     mp <- barplot (bu,col=colors(27),las=2,yaxt="n",ylim=c(0,200)) 
     bu<- round(bu,2) 
     text(mp, bu,labels=bu, pos = 3) 
     }) 
     output$plot <- renderPlot ({ y() }) 
    } 
} 

plotType(input$fiscal) 

} 
shinyApp(ui=ui ,server=server) 

我已經編輯我的代碼。立即獲取以下錯誤: -[R編程和閃亮的應用

.getReactiveEnvironment()中的錯誤$ currentContext(): 沒有活動的響應環境不允許操作。 (您試圖做一些事情,只能從做了反應表達或觀察者內。)**

回答

0

既然你不指定正是爲什麼你需要爲不同地塊不同的標籤,這將是爲用戶更容易只需查看他們選擇的季度的情節。

您的問題似乎是讀取和格式化數據,你應該能夠很容易地調整低於該解決方案以滿足您的需求。如果您有更具體的要求,請更新您的問題並提供可重複的示例。

library(shiny) 

ui <- fluidPage(
    sidebarLayout(
    sidebarPanel(
     textInput("file1", label = "Input File"), 
     actionButton("loadFile", "Load"), 
     radioButtons("fiscal", "Quarter:", c("2015Q1","2015Q2","2015Q3","2015Q4","2016Q1")) 
    ), 
    mainPanel({ 
     plotOutput("plot") 
    }) 
) 
) 

server <- function (input, output){ 
    mydata <- eventReactive(input$loadFile, { 
    validate(
     need(input$file1, "Enter a valid filename!") 
    ) 
    read.csv(input$file1) 
    }) 

    output$plot <- renderPlot({ 
    x <- mydata() 
    y <- x[x$Fiscal.Quarter == input$fiscal, ] 
    resources <- factor(y$Resource.Name) 
    stan <- tapply(y$Standard.Hours, resources, sum, na.rm = TRUE) 
    bil <- tapply(y$Billable.Hours, resources, sum, na.rm = TRUE) 
    bu <- bil*100/stan 
    mp <- barplot (bu, col = colors(27), las = 2, yaxt = "n", ylim = c(0,200)) 
    bu<- round(bu, 2) 
    text(mp, bu, labels = bu, pos = 3) 
    }) 

} 
shinyApp(ui = ui, server = server) 
+0

在上述解決方案中,單選按鈕無法集成到plot變量中。我有四個與每個單選按鈕相關的不同圖。我只需要單擊單選按鈕即可在一個窗口中顯示所有圖形(每次只能顯示一張圖) –

+0

請參閱上面的我的編輯 – mlegge