csv - How to populate drop down menu in shiny's ui.R using rscript code? -


i have .csv files in 1 of directories , want read them in ui.r populate drop down list's choices parameter. unable so. have referred these links didn't not seem me. link 1 , link 2

here code ui.r

library(shiny)   ui <- fluidpage(   #this start normal rscript code   #i don't know if allowed shiny   #not give me error in source code   files <- sys.glob("/home/ubuntu/r-stockprediction-master/input/*.csv"),   selection = c(),   for(i in 1:length(files)){     selection[i] = strsplit(strsplit(files[i],"/")[[1]][6],".csv")   },   selectinput("choice","select choice",choices=selection),    verbatimtextoutput("text") ) 

here code server.r

library(shiny)   server <- function(input,output){   output$text <- renderprint({     paste("you selected",input$choice,sep=" ")   })  } 

when run server, browser page

error: object 'selection' not found 

i hope have provided relevant. if need further information please feel free ask. thank in advance.

i have found solution problem after thousands of trial , error. did is, wrote new rscript file contains function "listfiles()" return vector containing names of files. in ui.r file added source(path) function locate new rscript file, path path new script file. in drop down function of selectinput(), make parameter choices = listfiles() , works magic. see code below understanding.

this listfiles.r script

    listfiles <- function(){      files <- sys.glob("/home/ubuntu/r-stockprediction-master/input/*.csv")      l = list()     for(i in 1:length(files)){       l[[i]] = strsplit(strsplit(files[i],"/")[[1]][6],".csv")     }      vect = c()     for(i in 1:length(files)){       vect[i] = l[[i]][[1]][1]          }     return (vect)   } 

this ui.r file

library(shiny) source("listfiles.r")  ui <- fluidpage(    selectinput("choice","select choice",choices = listfiles()),    verbatimtextoutput("text") ) 

no changes have been made server.r file. , works. think, whenever require automated task included in shiny scripts, make external rscript files containing required function returns value want.

changes after edit

this ui.r file

library(shiny) source("listfiles.r")  ui <- fluidpage(    uioutput("stocknames"),    verbatimtextoutput("text") ) 

this server.r file

library(shiny) source("listfiles.r")  server <- function(input,output){    stock_names <- reactive({     return(listfiles())   })   output$stocknames <- renderui({     selectinput("choices","select choice",choices = as.vector(stock_names()))   })     output$text <- renderprint({     paste("you selected",input$choices,sep=" ")   })  } 

Comments