0

I've written a code and trying to create user-editable values so that another user who is not acquainted with coding, may change the variables they want without having to read and understand the code.

I would like the following examples to work:

#Name of file here:
  file <- "mydata.csv"
#Select columns desired:
  offsets <- "-50,-25,0,25,50"
  
setwd("~/R/projects")
df <- read.delim("~/R/projects/file", sep=",", header=FALSE)

keep <- c(offsets)

These are just two of many examples so I would really appreciate some pointers on how to achieve this.

Here are some of the outputs:

> offsets <- -50,-25,0,25,50
Error: unexpected ',' in "offsets <- -50,"
> 
> keep <- c(offsets)
Error: object 'offsets' not found
>
> offsets <- '-50','-25','0','25','50'
Error: unexpected ',' in "offsets <- '-50',"
> 
> keep <- c(offsets)
Error: object 'offsets' not found
>
> offsets <- "-20,-10,0,10,20"
> keep <- c(offsets)
> 
> str(keep)
 chr "-20,-10,0,10,20"

The original line that works is:

> keep <- c(-50, -25, 0 , 25, 50)
> keep
[1] -50 -25   0  25  50
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 2
    If you use the `offsets <- "-50,-25,0,25,50"` version as input, you'll need to transform that to numeric values using something like `offsets <- as.numeric(strsplit(offsets, ",")[[1]])` – MrFlick Feb 13 '23 at 14:30
  • 4
    What is preventing you from reading the user editable values from a separate file? Ideally the user should *not* have to open the code file, let alone edit it. – Konrad Rudolph Feb 13 '23 at 14:30
  • 1
    Your comment suggests that `keep` is for *"Select columns desired"*, but in its integer form, `-50` will remove the 50th column, and `50` will include the 50th column; similarly for `-25`/`25`; and `0` by itself is confusing. Further, mixing positive and negative integers in column indexing is not supported, it will fail with `only 0's may be mixed with negative subscripts`. What exactly are you intending to `keep`? – r2evans Feb 13 '23 at 14:33
  • 1
    Depending on how complex the rest of the code is, you might consider turning this into a shiny app. It doesn't have to be complex to do what you need, it can include easy parsing of text inputs (with default values, even), and it can run trivially on a local machine for anybody that has R (and `shiny`) installed ... they don't need to know any R to be able to use the app. – r2evans Feb 13 '23 at 14:34
  • @KonradRudolph That's a great idea, I hadn't thought of that. Do you have any literature to hand to help me understand how to do it? Can such values be pulled up at any time in the code or is it easier to define them all at the beginning? – hydroceanog Feb 13 '23 at 15:09
  • 1
    @r2evans I understand what you're saying, and usually, numbers used as column naming to not work in code. What I'm doing here is calling a column where every row has the same value, i.e. '-50' or '0', and it works as I then use this to create a matrix and then an array with `arrayInd(which(df_matrix %in% keep, arr.ind = TRUE), dim(df_matrix))` – hydroceanog Feb 13 '23 at 15:28
  • @MrFlick - Thank you! This resolves the 'offset' issue perfectly. Could you add it to an answer so I can give credit where it's due? :) – hydroceanog Feb 13 '23 at 15:59

2 Answers2

1

If it is on Windows you could use winDialogString

#Name of file here:
file <- winDialogString("Name of file here:", "mydata.csv")

#Select columns desired:
offsets <- 
  winDialogString("Select columns desired by indices: (eg: -50,-25,0,25,50)", "-50,-25,0,25,50") |>
  strsplit(",") |>
  unlist() |>
  as.numeric()

winDialogString returns a character vector of length 1, so you have to transform it before use and you also have to put conditions check to know that the code will work properly

M Aurélio
  • 830
  • 5
  • 13
1

Since R is a programming language, it has very particular rules for how input must be formatted for the R interpreter to run. If you want to be more tolerant for different types of input, it's often better to accept a string/charcter value and then parse it yourself into the format that R needs. So if you accept a string of number separated by commas,

offsets <- "-50,-25,0,25,50"

you can parse that with something like:

offsets <- as.numeric(strsplit(offsets, ",")[[1]])
MrFlick
  • 195,160
  • 17
  • 277
  • 295