1

I am very new to Shiny and I am trying to build an app which retrieves data using a web service. After reading regarding "reactivity" in Shiny, I figured that "eventReactive" would be the best way to go. It worked until until there was no "if" statement but as soon as I used the "if" statement, it stopped working. Below is a simplified example of what I am trying to do-

    library(shiny)


ui <- fluidPage(

   # Application title
   titlePanel("Get Data"),


   sidebarLayout(
      sidebarPanel(
        textInput("stationID", "Station ID(s)", value = ""), 
        radioButtons("DownloadType", "Download",
                     choices = c("Yes","No"),
                     selected = "No"),



        br(),
        radioButtons("DataType", "Data Availability",
                     choices = c("All","Selected"),
                     selected = "All"),
        actionButton("goButton","Go!")

      ),


      mainPanel(
        (tableOutput("results"))
      )
   ))


# Define server logic required to draw a histogram
server <- function(input, output) {
  data<-eventReactive(input$goButton, {
  if(input$DataType == "All" && input$DownloadType=="No"){
    employee <- c('X','Y','Z')
    salary <- c(100, 200, 300)
    df1<-data.frame(employee, salary)
    df1
  }

    if(input$DataType =="Selected" && input$DownloadType=="No"){
      employee <- c('A','B','C')
      salary <- c(100, 200, 300)
      df1<-data.frame(employee, salary) 

      employee2<-c('A','B','C')
      salary2 <- c(500, 600, 700)
      df2<-data.frame(employee2, salary2) 

      my_df<-merge(df1,df2,by.x="employee",by.y="employee2")
      my_df
    }
  })

  output$results<-renderTable({
    data()
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Could someone please point me in the right direction? When the first "if" statement is true, no table is rendered. However, when the second "if" statement is true, it works. The idea is to click the "Go" button on the app to render a table based on the combination of inputs. Thank you in advance for any help.

asmi
  • 449
  • 1
  • 6
  • 16
  • 1
    Can you provide a reproducible example? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – yusuzech Sep 24 '19 at 19:57
  • @ yifyan - Yes. Sorry about not providing it earlier. I wrote something very simple and updated the code in the question. When the first "if" statement is true, it renders nothing but when the second "if" statement is true it gives the desired results. – asmi Sep 24 '19 at 20:39
  • 2
    add a `return(df1)` and for good measure add a `return(my_df)` or use `if(...){...}else{...}` – Sada93 Sep 24 '19 at 20:56

1 Answers1

2

As @Sada93 suggested, adding return to return values explicitly.

server <- function(input, output) {
    data<-eventReactive(input$goButton, {
        if(input$DataType == "All" && input$DownloadType=="No"){
            employee <- c('X','Y','Z')
            salary <- c(100, 200, 300)
            df1<-data.frame(employee, salary)
            return(df1) # add return
        }

        if(input$DataType =="Selected" && input$DownloadType=="No"){
            employee <- c('A','B','C')
            salary <- c(100, 200, 300)
            df1<-data.frame(employee, salary) 

            employee2<-c('A','B','C')
            salary2 <- c(500, 600, 700)
            df2<-data.frame(employee2, salary2) 

            my_df<-merge(df1,df2,by.x="employee",by.y="employee2")
            return(my_df) # add return
        }
    })

    output$results<-renderTable({
        data()
    })
}
yusuzech
  • 5,896
  • 1
  • 18
  • 33