我对Shiny还是很陌生,我正在尝试构建一个使用Web服务检索数据的应用程序。在阅读《 Shiny》中有关“反应性”的文章后,我认为“ eventReactive”将是最好的选择。它一直工作到没有“ if”语句为止,但是一旦我使用了“ if”语句,它就会停止工作。以下是我正在尝试做的简化示例-
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)
有人可以指出正确的方向吗?当第一个“ if”语句为true时,不呈现任何表。但是,当第二个“ if”语句为true时,它将起作用。想法是在应用程序上单击“执行”按钮以根据输入的组合来呈现表。预先感谢您的帮助。
答案 0 :(得分:1)
按照@ Sada93的建议,添加return
以显式返回值。
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()
})
}