How to access uploaded files by names in shiny

1,256 views
Skip to first unread message

Aaghaz

unread,
May 17, 2015, 7:58:00 AM5/17/15
to shiny-...@googlegroups.com
Hello everyone
I have uploaded some files in shiny. I can access the file at input$files[['datapath']] and i can use read.csv(input$files[['datapath']]) to read the file. suppose i have a file 1.txt uploaded, can i access it by read.table('1.txt') and i think its not working. I am very much confused , how to access the files uploaded by their names.

Here is a table shown in shiny
        name     size       type                                    datapath
1          1.txt      116 text/plain             /tmp/Rtmp01ot0X/d3e58fa61c59be8af1e402de/0
2          2.txt      176 text/plain             /tmp/Rtmp01ot0X/d3e58fa61c59be8af1e402de/1
3          3.txt      116 text/plain             /tmp/Rtmp01ot0X/d3e58fa61c59be8af1e402de/2
4          4.txt      116 text/plain             /tmp/Rtmp01ot0X/d3e58fa61c59be8af1e402de/3
5  GSM248238.CEL 13550880             /tmp/Rtmp01ot0X/d3e58fa61c59be8af1e402de/4
6  GSM248650.CEL 13551556             /tmp/Rtmp01ot0X/d3e58fa61c59be8af1e402de/5
7  GSM248651.CEL 13562404             /tmp/Rtmp01ot0X/d3e58fa61c59be8af1e402de/6

How to access the files by name??. Help highly appreciated
 

Yihui Xie

unread,
May 17, 2015, 12:48:33 PM5/17/15
to Aaghaz, shiny-discuss
You can write a helper function to do it, e.g.

read.table2 = function(file) {
files = input$files
file = files$datapath[files$name == file]
read.csv(file)
}

Then you read.table2('1.txt')

Regards,
Yihui

Agaz Hussain Wani

unread,
May 18, 2015, 12:45:58 AM5/18/15
to Yihui Xie, shiny-discuss
Thanks for your helpful comments. Can we have something like /tmp/Rtmp01ot0X/1.txt
 
/tmp/Rtmp01ot0X/2.txt. I mean the orignial file name at the path unlike random names like d3e58fa61c59be8af1e402de and so on. If possible that will be perfect.
--
Research Scholar 
Dept. Of Computer Science
Mangalore University
Mangalagangotri-574 199
Karnataka State
India

Joe Cheng

unread,
May 18, 2015, 12:48:29 AM5/18/15
to Yihui Xie, Agaz Hussain Wani, shiny-discuss
I chose the random names partially for security reasons--is there a reason why the non-human-readable name is a problem for you? What are you trying to do with the uploaded files?




--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/CAEra%2BeJdtpM_JYzcLVTDTJ4J%3DUFn-uJprOuYSDPgTv5gwBYZTA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Joe Cheng

unread,
May 18, 2015, 12:55:30 AM5/18/15
to Yihui Xie, Agaz Hussain Wani, shiny-discuss
Never mind, I see from your other thread what you're trying to do. You could also just rename those files to their original names, as you have the current and desired filenames in input$files and you can use file.rename. 

Sent from Outlook




On Sun, May 17, 2015 at 9:45 PM -0700, "Agaz Hussain Wani" <hussai...@gmail.com> wrote:

--

Agaz Hussain Wani

unread,
May 18, 2015, 12:55:48 AM5/18/15
to Joe Cheng, Yihui Xie, shiny-discuss
It gives a lot of issues with my application. I have a description file say 1.txt which contain the following information (names of different files)
                            Treatment
GSM248652.CEL         1
GSM248655.CEL         1
GSM248238.CEL         2
GSM248650.CEL         2
GSM248651.CEL         2
so when i read the file by simpleaffy bioconductor package read.affy('1.txt'), it should automatically read all the files given in my description file (1.txt). This i pretty cool in my local working directory as it works like a charm. But when i use shiny , its not able to find the files on the path given in the description file(1.txt). Really going through a hard time.

Agaz Hussain Wani

unread,
May 18, 2015, 1:10:32 AM5/18/15
to Joe Cheng, Yihui Xie, shiny-discuss
Thanks for your valuable comments.If you don't mind, would U please like to provide an example to rename the file.

Joe Cheng

unread,
May 18, 2015, 1:16:52 AM5/18/15
to Agaz Hussain Wani, Yihui Xie, shiny-discuss
from <- input$file$datapath
to <- file.path(basedir(from), basename(input$file$name))
file.rename(from, to)


Sent from Outlook

Agaz Hussain Wani

unread,
May 18, 2015, 1:21:32 AM5/18/15
to Joe Cheng, Yihui Xie, shiny-discuss
Thanks a lot, however it throws an error : Error in file.path(basedir(from), basename(input$file$name)) :
  could not find function "basedir"

Yihui Xie

unread,
May 18, 2015, 1:54:56 AM5/18/15
to Agaz Hussain Wani, Joe Cheng, shiny-discuss
I guess that is just a typo. You can replace basedir() with dirname().

Regards,
Yihui

Agaz Hussain Wani

unread,
May 18, 2015, 2:47:12 AM5/18/15
to Yihui Xie, Joe Cheng, shiny-discuss
Thanks dirname() works. I think here is also some type


from <- input$file$datapath
to <- file.path(basedir(from), basename(input$file$name))
file.rename(from, to)

I feel it should be like
from <- input$files[['datapath']]
to <- file.path(dirname(from), basename(input$files[['name']]))
file.rename(from, to)


Joe Cheng

unread,
May 18, 2015, 2:47:30 AM5/18/15
to Yihui Xie, Agaz Hussain Wani, shiny-discuss
Oh heh, yeah that's what I meant, thanks Yihui. 




On Sun, May 17, 2015 at 10:54 PM -0700, "Yihui Xie" <yi...@rstudio.com> wrote:

I guess that is just a typo. You can replace basedir() with dirname().

Regards,
Yihui


On Mon, May 18, 2015 at 12:21 AM, Agaz Hussain Wani

Yihui Xie

unread,
May 18, 2015, 12:06:46 PM5/18/15
to Agaz Hussain Wani, Joe Cheng, shiny-discuss
Either $ or [[ should work in this case. If you prefer correctness
over laziness, always use [[ instead of $ because of the potential
problem of partial matching (which does not exist in this particular
case).

Regards,
Yihui

Agaz Hussain Wani

unread,
May 18, 2015, 12:14:51 PM5/18/15
to Yihui Xie, Joe Cheng, shiny-discuss
Depending on the observations i made with $ in my code, it returns NULL unlike [[, which perfectly returns the desired file paths. So i totally prefer [[ over $ . I have never used something like input$files$datapath, so i thought may be a typo by Jeo.

Dean Attali

unread,
May 20, 2015, 7:58:07 PM5/20/15
to shiny-...@googlegroups.com, j...@rstudio.com, yi...@rstudio.com
I haven't read all the comments here fully so maybe this is redundant, but I do have a function that I've used exactly for this for quite some time and it seems to work for me. In general I prefer to keep the random filenames, and I only use this function if I really need the files to be named properly


#' When files get uploaded, their new filenames are gibberish.
#' This function renames all uploaded files to their original names
#' @param x The dataframe returned from a shiny::fileInput
fixUploadedFilesNames <- function(x) {
  if (is.null(x)) {
    return()
  }
  
  oldNames = x$datapath
  newNames = file.path(dirname(x$datapath),
                       x$name)
  file.rename(from = oldNames, to = newNames)
  x$datapath <- newNames
  x
}
Reply all
Reply to author
Forward
0 new messages