问题
Possible duplicate
Cannot populate drop down menu dynamically in R shiny
I have a small shiny app with some drop-down option for user. I created a subdirectory say data inside shiny app containing csv files to appear in the drop-down menu. I used the below code, but i am not able to access the files in the data subdirectory.
On 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 inside the app as my server.r and ui.r is , not in the separate subdirectory inside shiny app.
I also want to know is this the correct way to access the data of above code, as i am not able to 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 thedata subdirectory inside shiny app with csv files, but not able to do.
Edited: Input file example
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
回答1:
When you call list.files, you need to specify the path you want to list the files of, the default path is the current directory, which would be the directory where you have the ui.R and server.R file.
Try:
filenames <- list.files(path="data",pattern="\\.csv")
You can put this line before the shinyUI(...) in your ui.R file.
For the second part of your code, it looks like you want to clean up 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 your input file examples you have a lot of Gene title that have commas, this might cause issues if your the separator in your csv file is a comma.
来源:https://stackoverflow.com/questions/28914411/how-to-populate-csv-files-of-a-subdirectory-inside-shiny-app-in-drop-down-menu