Reputation: 471
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.
Upvotes: 1
Views: 1687
Reputation: 6116
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()
})
}
Upvotes: 2