3

I want to read a csv file as input from user in Shiny and assign it to a variable in global.r file.The code I have in ui.R is

fileInput('file1', 'Choose CSV File',
                     accept=c('text/csv', 'text/comma-separated-values,text/plain', .csv'))

The code I have in main Panel of server.R is

textOutput('contents')

The code I have currently in server.R is

output$contents <- renderText({    
      if(is.null(input$file1))return()
      inFile <- input$file1
      data<-read.csv(inFile$datapath)
  print(summary(data))

 })

I want to assign this input$file1 to a variable called 'data' in global.r. Please let me know if this is possible. Thanks

aeroNotAuto
  • 322
  • 2
  • 7

3 Answers3

3

I have added the following code to global.R file

data <- reactiveValues()

I used assign function in server.R to assign values to data in global.r

output$contents <- renderText({    
      if(is.null(input$file1))return()
      inFile <- input$file1
      data2<-read.csv(inFile$datapath)
  assign('data',data2,envir=.GlobalEnv)
  print(summary(data))
 })
2

I did not use shiny but I tried in my gui code which I wrote using gwidgets to make a ariable global I used data<<-read.csv(inFile$datapath) you can try this.

Jaishree Rout
  • 165
  • 1
  • 8
2

Sorry for the short answer, but if you use shiny with rmarkdown, it can be done as shown in this question:

https://stackoverflow.com/questions/29253481/data-specific-selectinput-choices-in-rmd-shiny/29255723#29255723

Create a reactive function that reads the file in!

jackStinger
  • 141
  • 2