简体   繁体   中英

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

I have some .csv files in one of my directories and want to read them in ui.R to populate drop down list's choices parameter. But I am unable to do so. I have referred these links but they didn't not seem to help me. link 1 and link 2

Here is my code for ui.R

library(shiny)


ui <- fluidPage(
  #this is where I start with my normal rscript code
  #I don't know even if this is allowed but shiny does
  #not give me any error in the 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 your choice",choices=selection),

  verbatimTextOutput("text")
)

Here is my code for server.R

library(shiny)


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

}

When I run my server, the browser page say

ERROR: object 'selection' not found

I hope I have provided everything relevant. If you need any further information please feel free to ask. Thank you in advance.

I have found a solution for my problem after thousands of trial and error. What I did is, I wrote a new rscript file that contains a function "listfiles()" to return a vector containing all the names of the files. In the ui.R file I added the source(path) function to locate the new rscript file, where path is the path to the new script file. In the drop down function of selectInput(), I simply make parameter choices = listfiles() and it works like magic. See the code below for good understanding.

This is my 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 is my ui.R file

library(shiny)
source("listfiles.R")

ui <- fluidPage(

  selectInput("choice","Select your choice",choices = listfiles()),

  verbatimTextOutput("text")
)

No changes have been made to the server.R file. And this works. I think, now whenever I require some automated task to be included in the shiny scripts, I will just make external rscript files containing the required function that returns the value I want.

Changes after the edit

This is my ui.R file

library(shiny)
source("listfiles.R")

ui <- fluidPage(

  uiOutput("stocknames"),

  verbatimTextOutput("text")
)

This is my server.R file

library(shiny)
source("listfiles.R")

server <- function(input,output){

  stock_names <- reactive({
    return(listfiles())
  })
  output$stocknames <- renderUI({
    selectInput("choices","Select your choice",choices = as.vector(stock_names()))
  })


  output$text <- renderPrint({
    paste("you selected",input$choices,sep=" ")
  })

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM