Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File browser in R

Tags:

I need to write a small R script for people who never used R before that imports a file and does some things with it. I would like to minimize user input as much as possible, and since assigning the file-path is basically all the user input required I was wondering, is it possible to get a popup screen (basically your usual "open file" screen) allowing someone to select a file (import the name as string in R or something)?

like image 461
Sacha Epskamp Avatar asked Jun 06 '11 16:06

Sacha Epskamp


People also ask

How do I search for a file in R?

You can use getwd() (to find the current path of the R process) or file. choose() to interactively find the file (it will return a character of the full path to the file).

How do I navigate folders in R?

R is always pointed at a directory on your computer. You can find out which directory by running the getwd (get working directory) function; this function has no arguments. To change your working directory, use setwd and specify the path to the desired folder.

How do I list files in working directory in R?

To list all files in a directory in R programming language we use list. files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories.


2 Answers

The file.choose function performs this, eg:

fname <- file.choose()  source(file.choose()) 

You may also want to look at choose.files (for multiple files) and choose.dir (for just selecting a directory path).

like image 127
James Avatar answered Oct 13 '22 21:10

James


The tcltk package gives you tk_choose.files.

If you want to go beyond file choosers then you can use the package to build user interfaces.

like image 39
Spacedman Avatar answered Oct 13 '22 21:10

Spacedman