Ketty
Ketty

Reputation: 851

Data available in Global Environment but value not accessible

Hi fellow Shiny users,

I am been struggling with this bug for a day and would really appreciate your help.

Goal: To use updateSelectInput to refresh the variable selection in the drop down list.

Problem: The drop down list is empty, even though "exists("data", envir = .GlobalEnv)" indicates that "data" exists.

Below is my code. Note that "data" is a global dataset created in myFunctions.R.

rawdata:

colA <- c('1','2','3','3','2')
colB <- c('1','1','3','3','2')
colC <- c('14','12','33','33','26')
rawdata <- as.data.frame(cbind(colA,colB, colC))

ui.R:

fluidPage(
    navbarPage(strong("My Structure"), id = "allResults",
             tabPanel(value ='inputData', title = 'Run Structure',
                sidebarLayout(
                    sidebarPanel(
                        actionButton("runButton", "Run Structure!"),
                        br(),
                        br(),
                        selectInput("selectvar", label = ("Select a variable"), choices = "")
                    ),

                    mainPanel(
                        DT::dataTableOutput('structure')
                    )
                )
             ),

            tabPanel(value='temp',title="TEMP", verbatimTextOutput("temp"))
    )
)

server.R:

source("./myFunctions.R")    
library("DT")

function(input, output, session) {

    # Run functions

    structure_result <- eventReactive (input$runButton, {
       assign('data', rawdata, envir=.GlobalEnv)
       PrepareData(rawdata)
       as.data.frame(head(data))
    })

    # Make a reactive copy of 'data' (a global dataset created in myFunctions.R)

    data_copy <- reactiveValues()

    observe({
      if(exists("data") && is.data.frame(get("data", envir =.GlobalEnv))) {
        data_copy$df <- get("data", envir = .GlobalEnv)
      } 
    })        

    # Update SelectInput drop down list

    observe({
        req(data_copy$df) 
        updateSelectInput(session, "selectvar", choices = names(data_copy$df [ , 1:ncol(data_copy$df)]))
    })        

    # Selected variable

    var_dd <- reactiveValues()

    observeEvent(input$selectvar, {
        req(data_copy$df) 
        var_dd$selected <- match(input$selectvar, names(data_copy$df [ , 1:ncol(data_copy$df)]))
    })    

    # Display structure results

    output$structure <- DT::renderDataTable({
          DT::datatable(structure_result(), options = list(paging = FALSE, searching = FALSE))           
    })

}

myFunctions.R:

PrepareData<-function(data) {
  data<<-RankData(data)
}

RankData<-function(datax) {
  return(datax[order(datax[,1],datax[,2]),])
}

Any help would be greatly appreciated. Thank you!

Upvotes: 0

Views: 228

Answers (1)

Till
Till

Reputation: 707

You have to source your functions inside (rather than before) the server function and use the local=TRUE option.

library("DT")

function(input, output, session) {
    source("./myFunctions.R", local=TRUE)  
    # ...
}

Here you find more information on shiny's scoping rules: Link

Upvotes: 0

Related Questions