3
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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))
 })
| improve this answer | |
$\endgroup$
2
$\begingroup$

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.

| improve this answer | |
$\endgroup$
2
$\begingroup$

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!

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.