Reading RDS files from a select list

434 views
Skip to first unread message

Aurelio García

unread,
Feb 23, 2016, 8:35:11 AM2/23/16
to Shiny - Web Framework for R
Hi,

I am trying to use Shiny to do this:

I have 10 rds files in the /app/data/ directory.
I want to let the user choose one of this files and then read it into R to make some data processing.

I have tried this in ui.R:

       
selectInput("user_file", "Usuario", list.files("data/"), selected = list.files("data/")[1])


Then I am trying to read the file in server.R:


shinyServer(function(input, output, session) { 
  
    data_plot <- reactive({
      readRDS(file.path("data", input$user_file))
    })

}

or 


shinyServer(function(input, output, session) {
  
 data_plot <- reactiveFileReader(1000, session, reactive(input$user_file), readRDS) 

}


None of them seems to work.

Any help please?

Thanks,
Aurelio
Message has been deleted

MySchizo Buddy

unread,
Feb 24, 2016, 1:41:36 PM2/24/16
to Shiny - Web Framework for R
print out input$user_file to see if it contains the correct path or not

Joe Cheng

unread,
Feb 24, 2016, 2:04:50 PM2/24/16
to MySchizo Buddy, Shiny - Web Framework for R
That should work--what seems to be the problem?

I'd also be sure that, on the server side, you be very careful to validate `input$user_file %in% list.files("data/")` before you go reading anything, and stop("Access denied") if not. A malicious user could set input$user_file to "../../path-to-sensitive-data" or something, potentially.

On Wed, Feb 24, 2016 at 10:41 AM MySchizo Buddy <myschi...@gmail.com> wrote:
print out input$user_file to see if it contains the correct path or not

--
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/f363a4a4-caca-4f16-9447-2e9907e4eb5c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aurelio García

unread,
Feb 25, 2016, 5:51:18 AM2/25/16
to Shiny - Web Framework for R
Hi,

Yes, I printed and the path is OK

Aurelio García

unread,
Feb 25, 2016, 6:06:44 AM2/25/16
to Shiny - Web Framework for R, myschi...@gmail.com
Hi Joe,

Thanks for your suggestion on validating.

The problem is: when I try to do anything with the dataframe i get an error, as if the file hadn't been read.

For example:

shinyServer(function(input, output, session) { 
  
    data_plot <- reactive({
      readRDS(file.path("data", input$user_file))
    })

    t1 <- min(data_plot$datetime)
}



Gives an error when trying to set t1:

Error in data_plot$datetime : object of type 'closure' is not subsettable


Sigbert Klinke

unread,
Feb 25, 2016, 6:59:42 AM2/25/16
to Shiny - Web Framework for R, myschi...@gmail.com
Hi,

did you try:

mydata <- data_plot()
if (mydata$datetime) { ...}

Sigbert

Aurelio García

unread,
Feb 25, 2016, 9:33:41 AM2/25/16
to Shiny - Web Framework for R, myschi...@gmail.com
Hi Sigbert,
datetime does exists, but you gave me the clue,

in my previous example I forget to put () to the variable outside a reactive context.

So this works:

shinyServer(function(input, output, session) { 
  
    data_plot <- reactive({
      readRDS(file.path("data", input$user_file))
    })

    t1 <- min(data_plot()$datetime)
}
 
Thanks so much
Reply all
Reply to author
Forward
0 new messages