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)?
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).
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With