R 使用;如果;加上;“事件反应性”;闪亮的

R 使用;如果;加上;“事件反应性”;闪亮的,r,shiny,shiny-reactivity,R,Shiny,Shiny Reactivity,我对Shinny非常陌生,我正在尝试构建一个应用程序,使用web服务检索数据。在阅读了《闪亮》中关于“反应性”的内容后,我认为“事件反应性”将是最好的方式。它一直工作到没有“if”语句,但我一使用“if”语句,它就停止工作了。下面是我正在尝试做的一个简化示例- library(shiny) ui <- fluidPage( # Application title titlePanel("Get Data"), sidebarLayout( s

我对Shinny非常陌生,我正在尝试构建一个应用程序,使用web服务检索数据。在阅读了《闪亮》中关于“反应性”的内容后,我认为“事件反应性”将是最好的方式。它一直工作到没有“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)
库(闪亮)

ui如@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()
    })
}

server您能提供一个可复制的示例吗?@是的。很抱歉没有早点提供。我写了一些非常简单的东西,并更新了问题中的代码。当第一个“if”语句为true时,它不会呈现任何内容,但当第二个“if”语句为true时,它会给出所需的结果。添加
return(df1)
,并添加
return(my_df)
或使用
if(…){else{code>