2016-02-16 162 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