简体   繁体   中英

read a file and save it into an object. [No display of loaded data ] in Shiny R

I know there are a few questions asked about this here is SO.

But I couldn't figure out the solution to my problem from them.

I want to Upload a file into R through shiny and assign it to an object say the object name "Object_name".

I used fileInput in ui.R and did this

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

Now I accesses the "file1" and tried to recover my uploaded file through file1$datapath. I couldn't actually do this. I am getting console errors like....

1. Error in terms.formula(formula, data = data) : 
  'data' argument is of the wrong type

And in GUI in the plot plane its showing Error:'data' argument is of the wrong type

I have added this piece of code in shinyserver server.R

mydata <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    object_name <- read.table(inFile$datapath, header = TRUE)
  })

Is it allowed to access the file directly from the datapath as I am doing above. Why doesn't it load the uploaded data into "a".

If somebody knows the answer please help me out.

Update:

My dataset is of header = False. So once I upload the data I have to change the names(my dataset) and then further calculations can be performed.

Considering this code

a <- reactive({
   inFile <- input$file1
   if (is.null(inFile)) return(NULL)
   read.table(inFile$datapath, header = TRUE)
 })

Still I cant access the a() [i,e the data I want] and use it into further calculations. I cant assign like names(a) <- c(.......) outside reactive because it throws error if I do that ...

Error in names(a) <- c("Ei", "Mi", "hours", "Nphy", "Cphy", "CHLphy",  : 
  names() applied to a non-vector

And I cant do it inside reactive because I can only assign names once the data "a" or "a()" is ready. so how can I achieve this.

In your server.r you need to do read.csv (or whatever corresponds to your input file format) to get at the data. Something like:

MyData<-reactive({ read.csv(input$file1$datapath, ...) })

Where ... is whatever other arguments that you need for read.csv() . Then you can access your data by referring to it as MyData() .

Edit If you want to add column names (that aren't in the original .csv) they you can use the col.names argument in read.csv() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM