如何使actionButton和eventReactive适用于我的代码

时间:2019-07-18 18:53:34

标签: r shiny reactive

我正在尝试创建一个具有光泽的应用程序,该应用程序将根据用户输入的内容下载,过滤和绘制数据。因为我要从需要长时间加载和压缩的开源API下载数据,所以我想添加一个仅在用户选择了要绘制的所有年份后才在代码中运行的操作按钮。

我的应用程序没有操作按钮即可正常运行,但我想添加它,以便一旦选择了新的一年,它就不会不断尝试下载数据。

我尝试了一切。

我已经使用了许多不同的eventReactive和observeEvent组合(尽管我想我想将eventReactive用于我想让我的应用程序执行的操作),但是我没有想要的方法。


# Define UI for application 
ui <- fluidPage(

   #title commented out

    fluidRow(
         column(4, 
          # choose which site you want to observe
          pickerInput("select", label = h5(span("NEON Sites", style = "color:darkblue")), 
                      choices = list()
          ),

        column(4,

         # can choose if you want to look at phenophases, AGDDs, or both using checkbox buttons
         prettyRadioButtons("option", label = h5(span("Choose what to observe", style = "color:darkblue")), choices = c("Phenophases" ="phenos","AGDDs"="agdd", "Phenophases & AGDDs"="both"),
                             selected = "phenos", status = "danger", outline = TRUE, inline = TRUE)
        ),
          column(4,
          # checkbox so can pick as many years that are available for that site as you want
          prettyCheckboxGroup("checkGroup", label = h5(span("Years", style = "color:darkblue")), 
                             choices = list("2015"="2015","2016"="2016", "2017" = "2017", "2018"="2018", "2019"="2019"), 
                             selected = NULL, status = "danger", outline = TRUE, inline = TRUE)

          ,

         # button to plot that you have to click again to refresh the graph
          actionButton("goplot", label = "PLOT"))

        ), hr(),


  # depending on what the user chooses to observe, the following plots are made

        mainPanel(
          conditionalPanel(condition = "input.option == 'agdd'", plotOutput("yearsPlot", width = "150%", height = "500px")),
# other panels commented out
        )
    )


# Define server logic
server <- function(input, output, session) {


  # updating the choices based on years avaiable for each site.
  observe({
    # commented out

    })


 # input$goplot
 # if the user chooses to observe agdds, then this plot is generated

  # checkedGroups <- eventReactive(input$goplot, {
  #   yr <- input$checkGroup
  #   sort(yr)})


  eventReactive(input$goplot, {
 output$yearsPlot <- renderPlot({

     siteO <- input$select
     yr <- input$checkGroup
     sort(yr)

     #eventReactive(input$goplot,{

     dpid2 <- as.character('DP1.00002.001') 
     agddLoad <- loadByProduct(dpID=dpid2, site=siteO, package="basic", check.size = FALSE, 
                               startdate = c(paste(checkedGroups()[1],"-01", sep = "")), enddate = c(paste(tail(checkedGroups(), n=1), "-12", sep = "")), avg = "30")


     SAAT <- agddLoad$SAAT_30min


# a lot of code commented out that cleans and filters the data


      # plots the data with the different years as the different lines
  # observeEvent(
      ggplot(data=site_select, aes(x=dayOfYear, y=AGDD, color = as.factor(year))) + # commented out


   })

 })

我已经评论了很多我尝试过的内容,但是我也删除了一些内容。

我认为最接近的是我想要的方式是使用checkedGroups()。 当我使用checkedGroups()时,单击操作按钮时它可以工作,但是当选择了不同的年份时,代码将再次针对所选的前一年运行。

例如,如果我选择“ 2015”并点击绘图,则代码将运行并绘图。然后,如果我选择“ 2015”和“ 2016”,那么代码将在运行图之前再次在2015年运行,然后一旦我运行图,它将在2015年和2016年运行。我不希望它运行两次。我只希望它在单击图时运行一次。

在renderPlot周围使用eventReactive时,代码将运行,但不会显示该图。

我将不胜感激任何帮助!谢谢!

0 个答案:

没有答案