Jamie McManus
Jamie McManus

Reputation: 43

R Shiny run function on file upload

I have a Shiny App that takes a text file from FileInput , processed it and sets a few variables / data frames which are then displayed in InfoBoxOutputs .

I know I can currently process the file in the output of the server function for elements eg a Datable like in my code below, but this doesnt seem to update the InfoBoxOutputs even though the variables used are set during the Seed() function (they are definetly set ) that runs for the Datatable output.

So is there a way to run my Seed() function to process all the data at once so that all my elements an use the processed data or do I have to rerun the function foreach element ?

#Initialize variables which will later be set in Seed() 
mostwanted_variable <- NULL
key_vaiable <- NULL

ui <- dashboardPage(skin = "blue",
             dashboardHeader( title = "Text Check"),
                  dashboardSidebar(
                      fileInput("file1", "Upload File",
                                multiple = FALSE,
                                accept = c( ".txt"))
                       ),
                      dashboardBody(
                      
                      tabItems(
                        
                        tabItem(tabName = "statistics",
                                fluidRow(
                                  infoBoxOutput("info1"),
                                  infoBoxOutput("info2")
                                ),
                                
                                # infoBoxes with fill=TRUE
                                fluidRow(
                                  DT::dataTableOutput("mytable")
                                ) 
                                
                        )
          
                      )
                    )
)

server <- function(input, output) {
  # Main List Output 
  output$mytable = DT::renderDataTable({
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    
    req(file)
    validate(need(ext == ".txt", "Please upload a text file"))
    
    ## Processes Text File and sets many variables & frames 
    Seed(file$datapath)
    
    ## Select maindatasource which is set in the Seed function
    maindatasource
  })
  # Info Box 1 
  # uses variable set during Seed function 
  output$info1 <- renderInfoBox({
    infoBox(
      "Key", key_variable, icon = icon("user-secret"),
      color = "aqua" , fill = TRUE
    )
  })
  # Info Box 2
  # uses variable set during Seed function 
  output$info2 <- renderInfoBox({
    infoBox(
      "Most Wanted", mostwanted_variable, icon = icon("earlybirds"),
      color = "aqua"
    )
  })
}

Upvotes: 0

Views: 620

Answers (1)

HubertL
HubertL

Reputation: 19544

You could set reactiveValues in the Seed() function, that would be used to update info bpxes.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  skin = "blue",
  dashboardHeader(title = "Text Check"),
  dashboardSidebar(fileInput(
    "file1",
    "Upload File",
    multiple = FALSE,
    accept = c(".txt")
  )),
  dashboardBody(
    fluidRow(infoBoxOutput("info1"),
             nfoBoxOutput("info2")),
    fluidRow(DT::dataTableOutput("mytable"))
  )
)

server <- function(input, output) {
  rv <- reactiveValues(
    mostwanted_variable = NULL, 
    key_variable = NULL)
  
  Seed <- function(filename) {
    rv$mostwanted_variable <- "a"
    rv$key_variable <- 123
    return(data.frame(
      main = c(1, 2),
      data = c("a", "b"),
      source = c(3, 4)
    ))
  }
  
  # Main List Output
  output$mytable = DT::renderDataTable({
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    req(file)
    validate(need(ext == "txt", "Please upload a text file"))
    
    ## Processes Text File and sets many variables & frames
    maindatasource <- Seed(file$datapath)
    
  })
  # Info Box 1
  # uses variable set during Seed function
  output$info1 <- renderInfoBox({
    infoBox(
      "Key",
      rv$key_variable,
      icon = icon("user-secret"),
      color = "aqua" ,
      fill = TRUE
    )
  })
  # Info Box 2
  # uses variable set during Seed function
  output$info2 <- renderInfoBox({
    infoBox(
      "Most Wanted",
      rv$mostwanted_variable,
      icon = icon("earlybirds"),
      color = "aqua"
    )
  })
}

shinyApp(ui, server)

Upvotes: 1

Related Questions