在R中单击操作按钮时运行代码

在R中单击操作按钮时运行代码,r,shiny,action-button,R,Shiny,Action Button,我正在运行一堆代码(大约100行)在R。 我希望首先打开闪亮的界面,应该有一个操作按钮,单击该按钮将执行代码并显示一个弹出窗口,表明它已经完成。 在单击操作按钮之前,不应执行代码 ` ui我建议将长代码转换为函数,并通过observeEvent调用它 长码 应用程序R long\u code我建议将长代码转换为函数,并通过observeEvent调用它 长码 应用程序R long\u代码请告诉我们您做了什么?如果还没有开始,请告诉我们你做了什么?如果还没有开始的话 ui <-nav

我正在运行一堆代码(大约100行)在R。 我希望首先打开闪亮的界面,应该有一个操作按钮,单击该按钮将执行代码并显示一个弹出窗口,表明它已经完成。 在单击操作按钮之前,不应执行代码

`


ui我建议将长代码转换为函数,并通过
observeEvent
调用它

长码 应用程序R
long\u code我建议将长代码转换为函数,并通过
observeEvent
调用它

长码 应用程序R
long\u代码请告诉我们您做了什么?如果还没有开始,请告诉我们你做了什么?如果还没有开始的话
    ui <-navbarPage("Calculator",
       tabPanel("Cal 1", fluidPage(
           headerPanel("Calculation 1"), 
                    sidebarLayout(
                        sidebarPanel(selectInput("Segment","Select Segment",list("Micro"="Micro","PF"="PF","Retail"="Retail",
                                                                      "Corporate"="Corporate"))
                        ),

                        mainPanel(
                          actionButton("Run_1","Run 1"),
                          actionButton("Summary", "Summary"),
                          hr(),
                          tableOutput("variable"),
                          plotOutput("plot1"),
                          plotOutput("plot2"),
                          verbatimTextOutput("Finish_1")
                        )))),
   tabPanel("Cal 2", fluidPage(headerPanel("Calculation 2"),
                        sidebarLayout(
                        sidebarPanel(textInput("CustomerId","Enter Customer ID",value = NULL),
                                     submitButton(text = "Submit")
                        ),

                        mainPanel(
                          actionButton("Run_2","Run 2"),
                          tableOutput("Variable"),
                          verbatimTextOutput("Finish_2")

                        )))),
  )
server <- function(input, output) {
  model <- reactiveValues(Data=NULL) 
observeEvent(input$Run_1,{
    model$Finish_1<-
  ##### Run  Codes 1##########
})
output$Finish_1<-renderPrint({
  model$Finish_1
   print("Calculation 1 Finished")
 })

####table1 is a table created during run of above codes
observeEvent(input$Summary,{
  model$plot1<-ggplot(table1,aes(x=Stage,y=`Number`,label=`Number of Cust.`))+
    geom_bar(stat = "identity",width=0.7, fill = "#FF6667")+
    geom_text(size = 6, position = position_stack(vjust = 0.5))+ggtitle("Stage")

})
output$plot1 <- renderPlot({
  model$plot1
})
observeEvent(input$Segment,{
  model$plot2<-ggplot(filter(table,Segment==input$Segment), aes(Stage, `Number of Cust`,label=`Number of Cust`)) +
    geom_bar(aes(fill = Stage), position = "dodge", stat="identity")+ggtitle(input$Segment)+
    geom_text(size = 6, position = position_stack(vjust = 0.5))

})
output$plot2<-renderPlot({
  model$plot2
})
model1 <- reactiveValues(Data=NULL) 

  observeEvent(input$Run_2,{
    model1$Finish_2<-

##### Run Codes 2####################
})
  output$Finish_2<-renderPrint({
    model1$Finish_2
    print("Calculation 2 Finished")
    })
  observeEvent(input$CustomerId,{
    model1$Variable<-which(total3$CUSTOMER_ID==input$CustomerId)
  })
####total3 is a table

  output$Variable = renderTable({
    total3[model1$Variable(), ]    #use the search.critera() reactive to determine rows to display
  })
}
function(){
  ## perform actions here
  message("running code...")
  return("some output")
}
long_code <- source("long_code.R")$value
library(shiny)

shinyApp(
  fluidPage(actionButton("run", "run code")),
  function(input, output, session){
    vals <- reactiveValues()

    observeEvent(input$run, {
      vals$long_code_out <- long_code()
      showModal(modalDialog("calculation finished!"))
    })
  }
)