0

I have been writing a program using shiny where I need to access the fileInput from the user. Here is a code exert.

for (machine in 1:machineCount)
{
  curFileID <- paste0("file", machine)
  print(paste0("Current File ID: ", curFileID))
  print(paste0("Current File ID input accessed through variable: ", input$curFileID))
  print(paste0("Current File ID input accessed directly through variable name:", input$file1))
}

and here is the output

*[1] "Current File ID: file1"

[1] "Current File ID input accessed through variable: "

[1] "Current File ID input accessed directly through file ID:COPYING.txt"
[2] "Current File ID input accessed directly through file ID:35727"
[3] "Current File ID input accessed directly through file ID:"
[4] "Current File ID input accessed directly through file ID:C:\Users\fabma\AppData\Local\Temp\RtmpYV0PCn"*

You can see that when I used a variable to store the ID it does not work and input$variable returns NULL. However when I directly access with input$fileID then it works. I don't understand why this happens and how to solve it.

jogo
  • 12,469
  • 11
  • 37
  • 42
Fabmaur
  • 123
  • 2
  • 12

1 Answers1

3

curFileID is character variable where you store the name of the column. You cannot directly access the column by using $.

See for example,

mtcars$cyl
#[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

temp <- "cyl"
mtcars$temp
#NULL

To access the column values as vector with temp you need to use double brackets [[

mtcars[[temp]]
#[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

You can read more about how [, [[ and $ works at ?Extract.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213