3
votes

I need to develope a shiny interface with many csv loaded in it. Based on my past experience with shiny, I prefer to import this data before the server function, in this way I hope that each session will run faster. The app will be restarted each morning, so data will be daily refreshed. The problem is that I need to consider an extra refresh during the day, performed manualy with a button that source an external updating script. I can't (but I hope that is possibele), refresh the data loaded at the start of the app. Below my (dummy) code:

server:

library(shinydashboard)
library(plotly)
library(data.table)
library(dplyr)


path1<-"C:/Users/.../DATA/"
path2<-"C:/Users/../DATA/csv/"

##load dataset at first start
table<-fread(file=paste0(path2,"main.csv"),data.table=FALSE))


shinyServer(function(input, output,session) {


    ##### refresh data with button####
    observeEvent(input$refresh_data,{
         source(paste0(path1,"any_script.r"),local = FALSE)
         table<<-fread(file=paste0(path2,"main.csv"))
    })

    #####...ui####
    table_r<-reactive({
    ##obs populate the input for choosing rows to be plotted
        obs<-rev(unique(table$anycolumn))
        curve_sint<-list(
            lotti=obs,
            data=obs
        )
    })


    output$obs_ui<-renderUI({
        selectInput("input_obs","Please choose the batch:",
                    choices =table()$obs ,multiple = T)
    })


    output$plot<-renderPlotly({
        table_r()$data%>%
            filter(anycolumn==input$input_obs)%>%
            plot_ly(
                x=~x,
                y=~y,
                color=~anycolumn,
                type="scatter"
            )
    })
})


ui:

library(shinydashboard)

ui <- dashboardPage(

  dashboardHeader(
    title = "shiny"                
  ),
  dashboardSidebar(
    width=250,
    sidebarMenu(
      menuItem(
        "plot data"
        tabName = "clhc",
        icon = NULL
      ),
      menuItem(
        "Update data",
        icon=icon("gear"),
        tabName="update_data"
      )
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "clhc",
        fluidRow(
          column(width=3,
                 uiOutput("obs_ui")
          ),
        ),
        fluidRow(
          column(
            width=12,
            fluidRow(
              plotlyOutput("plot")
            )
          )
        )
      ),
      tabItem(
        tabName = "update_data",
        fluidRow(
          box(
            width=12,
            title="Sint HC",
            actionButton("refresh_sint_hc","Refresh", icon=icon("refresh"))
          )
        )
      )
    )
  )
)

I'm sure that the script inside observeevent works fine, because if I put a print(nrow(table)) after the fread I can see that table is correctly refreshed. I can't understand where the new data is stored because from the plot panel is stil available the old data before the update. Is my attempt completley wrong?

1

1 Answers

2
votes

Using <<- will make table accessible globally, and after terminating your shiny app, but you need it to be reactive. Here is a brief example on using a reactiveVal (setting to table1 as default) that gets modified when the actionButton is selected and a new data file is read.

library(shiny)
library(data.table)

table1 <- fread(file = 'atest1.csv')

ui <- fluidPage(
  verbatimTextOutput("text"),
  actionButton("refresh", "Refresh")
)

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

  rv <- reactiveVal(table1)

  output$text <- renderText({
     names(rv())
  })

  observeEvent(input$refresh, {
    print("Refresh")
    table1 <<- fread(file = 'atest2.csv')
    rv(table1)
  })

}

shinyApp(ui, server)