How to run a function that gets data from inside eventReactive and plot in table?

Tra*_*acy 2 r function reactive-programming shiny

I'm very new to shiny and am having some trouble and have been searching all day, so hopefully someone can help me. Once an action button (actionButton, on UI) is selected by a user, I would like the server script to call a function (evenReactive in server) I wrote (myfunction, see below) that uses the input items from the UI and gets the right parameters I need to run myfunction and produce a n X2 data matrix that will be plotted as a table (renderTable in server, below). The data is a n X 2 matrix.

I have some sample code below. It's not the entre code, so you will not see the UI with the inputs I am putting in my function, or the server parts associated. But, it is the part I am trying to fix. I hope that's ok. I don't need the renderText, but when I take it out I get an error. Sorry for the formatting. Copy and pasting changed it a bit.

library(shiny)

ui <- shinyUI(fluidPage
     (column(4,actionButton("gobutton", "Run"),verbatimTextOutput("ntext1")), 
      column(4, DT::dataTableOutput("table",width = "75%"))))

library(shiny)

shinyServer(function(input, output, session) 


  ntext1 <- eventReactive(input$gobutton, {
            if (input$gobutton==1){ 
              data=myfunction(input$checkbox,input$dateRange)}
   })


  output$ntext1 <- renderText({ntext1()})

  output$table <- DT::renderDataTable(DT::datatable({
    data
  })
))

myfunction <-function(All,date1,date2,source_cd,tran_cd,airline_list,mag_level) {
 print(All); print(date1); print(date2); print(source_cd);print(tran_cd);print(airline_list);print(mag_level)

    setwd("C:/Users/TRomano/Documents/Projects/TrendAnalysis/Data")
    data = read.csv("Airlines.csv",header = TRUE)
    return(data)
  }
Run Code Online (Sandbox Code Playgroud)

Sym*_*xAU 6

对于这类问题,我喜欢利用reactiveValues()旨在以反应方式存储数据的问题。

这是一个简单的应用程序(单个应用程序,未拆分为服务器和用户界面),它演示了我认为您正在尝试做的事情

library(shiny)
library(DT)

ui <- shinyUI(
    fluidPage(
        column(width = 4,
                     actionButton("gobutton", "Run")
        column(width = 4, 
                     DT::dataTableOutput("table",
                                         width = "75%"))))

server <- shinyServer(function(input, output, session){ 

    rv <- reactiveValues()
    rv$data <- NULL

    observe({    ## will 'observe' the button press

        if(input$gobutton){ 
            print("here")  ## for debugging
            rv$data <- myfunction()   ## store the data in the reactive value
            rv$data
            }
        })

    output$table <- DT::renderDataTable({
        ## The data has been stored in our rv, so can just return it here
        rv$data
    })
})

myfunction <- function(){
    data <- data.frame(id = c(1,2,3),
                       val = letters[1:3])
    return(data)
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)