How to populate csv files of a subdirectory inside a shiny app in a dropdown menu?
Possible duplicate
Unable to populate dropdown menu dynamically in R shiny
I have a small shiny app with some dropdown option for the user. I created a subdirectory data
inside a shiny app containing the csv files to appear in the dropdown menu. I have used the below code, but I cannot access the files in the subdirectory data
.
In ui.r:
filenames <- list.files(pattern="\\.csv")
selectInput(inputId="dataset",label= "Choose platform annotation file",filenames)
server.r:
dataInput <- reactive({
if (grepl("[/\\\\]", input$dataset)) {
stop("Invalid dataset")
}
read.csv(file.path("data", input$dataset))
})
output$annotation <- renderDataTable({
withProgress(message = 'Loading Data . . .', {
dataInput()
})
})
The above code allows me to access the csv files, if they are within the application, as my server.r
and ui.r
there, and not in a separate subdirectory inside brilliant application.
I also want to know that this is the correct way to access the data of the above code as I cannot access the data further in my below code.
inputdata <- reactive({
df1 <- data.frame()
df1 <- dataInput()
if (is.null(df1))
return(NULL)
df1[] <- lapply(df1, as.character)
df1 <- df1[df1[,3]!='',]
df1 <- df1[!grepl('[/]', df1$Gene.Title),]
})
I tried this also
filenames <- list.files(pattern="\\.csv$data") ## data is my folder inside shiny app.
to access a subdirectory data
inside a brilliant app with csv files but fail to do.
Edited: Sample input file
ID Gene Title Gene Symbol
1007_s_at discoidin domain receptor tyrosine kinase 1 DDR1
1053_at replication factor C (activator 1) 2, 40kDa 7-Mar
117_at heat shock 70kDa protein 6 (HSP70B') HSPA6
121_at paired box 8 PAX8
1255_g_at guanylate cyclase activator 1A (retina) GUCA1A
1294_at ubiquitin-like modifier activating enzyme 7 UBA7
1320_at protein tyrosine phosphatase, non-receptor PTPN21
1405_i_at chemokine (C-C motif) ligand 5 CCL5/CCL6
1431_at
1438_at EPH receptor B3 EPHB3
1487_at estrogen-related receptor alpha ESRRA
1494_f_at cytochrome P450, family 2 CYP2A6/CYP2
source to share
When you call list.files
, you need to provide the path where you want to list the files, the default path is the current directory which will be the directory that contains the file ui.R
and server.R
.
Try:
filenames <- list.files(path="data",pattern="\\.csv")
You can put this line in front shinyUI(...)
of your ui.R
file.
For the second part of your code, it looks like you want to clear the data, you could do (I changed inputdata
to clean_data
to make it clearer):
clean_data <- reactive({
#get the data
df1 <- dataInput()
if (is.null(df1))
return(NULL)
df1[] <- lapply(df1, as.character)
#looks for lines that have only zero or more blanks or a slash in column 3 and removes them
df1 <- df1[!grepl("$ *^|/",df1[,3]),]
df1
})
#do what you want with the data, for expample render it as another table
output$annotation <- renderDataTable({
withProgress(message = 'Loading Data . . .', {
clean_data()
})
})
Also, looking at examples of your input files, you have a lot of Gene title
commas, this can cause problems if your delimiter in your file csv
is comma.
source to share