【问题标题】:R programming and shiny appR 编程和闪亮的应用程序
【发布时间】:2016-05-27 16:56:14
【问题描述】:
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)

我已经编辑了我的代码。现在收到以下错误:-

.getReactiveEnvironment()$currentContext() 中的错误: 如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能在反应式表达式或观察者内部完成的事情。)**

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案1】:

    由于您没有具体说明为什么需要为不同的图表使用不同的选项卡,因此用户只查看他们选择的季度的图表会更容易。

    您的问题似乎是读入和格式化的数据,您应该能够轻松地调整下面的解决方案以满足您的确切需求。如果您有更具体的要求,请更新您的问题并提供可重现的示例。

    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)
    

    【讨论】:

    • 在上述解决方案中,单选按钮没有集成到绘图变量中。我有四个与每个单选按钮相关联的不同图。我只希望通过单击单选按钮(一次一个图)将所有图显示在一个窗口中
    猜你喜欢
    • 1970-01-01
    • 2016-12-20
    • 2016-05-23
    • 2020-06-16
    • 2018-05-15
    • 2017-03-24
    • 2016-12-29
    • 2013-07-08
    • 2020-11-16
    相关资源
    最近更新 更多