0

I sincerely apologize but it has been a month now of trying but I still cannot figure out. The main question: How to use Shiny R to appear "run code" button in one page and then when pressed & finished calculating two new results in separate tabs/windows appear?

I have a code saved as "Button_test.R" with the following content:

long_code <- source("long_code.R")$value

library(shiny)

shinyApp(
  fluidPage(
    titlePanel("Test results"),
     actionButton("run", "run code")),
 function(input, output, session){
    vals <- reactiveValues()
     mainPanel(titlePanel("Test results"))
     fluidRow(
       column(12,
           dataTableOutput('table'),
     )
   )

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

The "long_code.R" file contains the following files (gasoline pls data -- available using pls package) and codes:

function(){
library(pls)
library(prospectr)
library(Metrics)
library(factoextra)
 
data(gasoline)

gasTrain <- gasoline[1:50,]

gasTest <- gasoline[51:60,]

gas1 <- plsr(octane ~ NIR, ncomp = 10, data = gasTrain, validation = 
"LOO")

pred <- predict(gas1, ncomp = 2, newdata = gasTest)

write.csv(pred,"pred.csv")

jpeg('pca.jpg')

plot(RMSEP(gas1), legendpos = "topright")

dev.off()

}

When I first run the "Button_test.R" it showed "run code" with the page below:

run code page

Then, when I pressed the "run code," the calculation was finished. However, I am interested to produce in 2 separates pages (new tabs or new windows) the results for "pca.jpg" plot and "pred" results similar below:

pca.jpg plot

pred results

I am not sure how to do it and where do I put the code. I am sorry for this long code. It is just necessary to produce the results. My apologies.

  • Please post a reprex (a minimally reproducible example), so that others can help you. Please take a look at this: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – YBS Jul 17 '20 at 02:25
  • Thank you so much for the suggestion. I have revised the code and provide reproducible example. – Gerard Dumancas Jul 17 '20 at 17:51

1 Answers1

0

Try this code:

library(shiny)

long_code <- function(){
  library(pls)
  library(prospectr)
  library(Metrics)
  library(factoextra)

  data(gasoline)

  gasTrain <- gasoline[1:50,]

  gasTest <- gasoline[51:60,]

  gas1 <- plsr(octane ~ NIR, ncomp = 10, data = gasTrain, validation = "LOO")

  pred <- predict(gas1, ncomp = 2, newdata = gasTest)
  p1 <- plot(RMSEP(gas1), legendpos = "topright")
  p2 <- as.data.frame(pred)
  myresult <- list(p1,p2)
  
  ##  you can write files on server side
  # write.csv(pred,"pred.csv")
  # 
  jpeg('pca.jpg')

  plot(RMSEP(gas1), legendpos = "topright")

  dev.off()
  
  return(myresult)

}


ui <-  navbarPage("Test Results",id = "inTabset",
                  tabPanel ("Home",  value="tab1", actionButton("run", "run code")
                            
                  ),
                  tabPanel ("My Plot",  value="tab2",
                            fluidRow(column(10, plotOutput("myplot")) )
                  ),
                  tabPanel("My Table",  value="tab3",
                           fluidRow(column(12,dataTableOutput('table')))
                  )
)

server <- function(input, output, session){
    vals <- reactiveValues()

    observeEvent(input$run, {
      req(input$run)
      vals$long_code_out <- long_code()
      showModal(modalDialog("calculation finished!"))
      myresults <- long_code()
      output$myplot <- renderPlot({
        myresults <- long_code()
        myresults[[1]]
      })
      output$table <- renderDataTable({
        myresults[[2]]
      })
      #stopApp()
      ##  you can save files below this line
      write.csv(myresults[[2]],"pred.csv")
      
    })

}

shinyApp(ui = ui, server = server)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Hello sir! Thank you so much. It works. I will try harder on how I can apply this to my project. If I have more questions after trying so much and it won't work, may I ask another question here? I am new to ShinyApp but I try very hard. Thank you! – Gerard Dumancas Jul 19 '20 at 21:04
  • It is considered polite to accept an answer that has answered your query. – YBS Jul 19 '20 at 21:15