【问题标题】:How to populate drop down menu in shiny's ui.R using rscript code?如何使用 rscript 代码填充闪亮的 ui.R 中的下拉菜单?
【发布时间】:2015-07-16 09:32:20
【问题描述】:

我的一个目录中有一些 .csv 文件,并希望在 ui.R 中读取它们以填充下拉列表的选择参数。但我无法这样做。我已经提到了这些链接,但它们似乎对我没有帮助。 link 1link 2

这是我的 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")
)

这是我的 server.R 代码

library(shiny)


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

}

当我运行我的服务器时,浏览器页面说

ERROR: object 'selection' not found

我希望我已经提供了所有相关的信息。如果您需要任何进一步的信息,请随时询问。提前谢谢你。

【问题讨论】:

  • 您需要将文件调用放在 server.r 中,并让 ui.r 保留应用程序的外观 - 所有数据调用和动态内容(如您所要求的)都应该在服务器中。河。最好通过shiny.rstudio.com 上的一些示例来工作 - 这篇文章最相关shiny.rstudio.com/articles/dynamic-ui.html
  • 查看了您提到的链接。但我不想要任何动态的东西。我只想列出我的所有文件并在下拉列表中显示它们@MarkeD

标签: r csv shiny


【解决方案1】:

经过数千次反复试验,我找到了解决问题的方法。我所做的是,我编写了一个新的 rscript 文件,其中包含一个函数“listfiles()”以返回一个包含所有文件名称的向量。在 ui.R 文件中,我添加了 source(path) 函数来定位新的 rscript 文件,其中 path 是新脚本文件的路径。在 selectInput() 的下拉函数中,我简单地使参数选择 = listfiles(),它就像魔术一样工作。请参阅下面的代码以获得更好的理解。

这是我的 listfiles.R 脚本

    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)
  }

这是我的 ui.R 文件

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

ui <- fluidPage(

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

  verbatimTextOutput("text")
)

没有对 server.R 文件进行任何更改。这有效。我想,现在每当我需要在闪亮的脚本中包含一些自动化任务时,我只会制作包含返回我想要的值的所需函数的外部 rscript 文件。

修改后的变化

这是我的 ui.R 文件

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

ui <- fluidPage(

  uiOutput("stocknames"),

  verbatimTextOutput("text")
)

这是我的 server.R 文件

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=" ")
  })

}

【讨论】:

  • 这是完成一些相对基本的事情的一种不必要的hacky方式......您不想使用ui.r调用函数,UI应该只引用已经在server.r中计算的值。尝试使 listfiles 成为一个响应函数,然后在应该存在于 server.r 中的 renderUI 中引用它,然后使用 renderUI 的 ui.r 引用
  • @hedgedandlevered 你能解释一下为什么我们不想用 ui.r 调用函数吗?
  • @hedgedandlevered 我一直在尝试按照您建议的方式做事,但我遇到了问题。您会看到我的 listfiles() 函数返回一个向量。如何在 reactive() 编写该函数?请帮忙。谢谢。
  • 我在上班,没时间,今晚或者明晚我会回复的
猜你喜欢
  • 2017-08-25
  • 2013-05-24
  • 2021-05-01
  • 2019-11-26
  • 2015-05-08
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 2016-08-15
相关资源
最近更新 更多