2017-09-27 9 views
0

我需要显示选择输入选项,这些选项是从可能更改的csv文件列表中呈现的。Shiny UI:为csv文件的长列表中的选择输入渲染名称 - 如何显示?

第一步用户通过单选按钮决定他想检查哪个详细视图。在这里,我只需将路径添加到选定的选项即可指向正确的文件夹。 在第二步中,用户应该能够选择“水果”细节视图。因此,我用gsub过滤了一大堆csv文件,这个工作正常,但名称并未显示在选择输入字段中,此外我只是收到一条错误消息。

为了使它更简单,代码:

ui.R

 tabItem(tabName = "mainTab", 

    # control boxes   

    fluidRow(
     box(
     title = "Please select a fruit representation", 
     width = 12, 
     status = "info", 
     color = "maroon", 
     collapsible = FALSE, 
     radioButtons(
      inputId = "fruit_view", 
      label = "", 
      choices = c("Top Fruit View", 
      "Current Fruit Split", 
      "Top Fruit & Value Class"), 
      selected = "Top Fruit View", 
      inline = TRUE) 
    ) 
    ), 
    fluidRow(

     box(
     title = "Selected Fruit:", 
     width = 4, 
     collapsible = FALSE, 
     selectInput(
     inputId = "fruit_selection", 
     label = "", 
     choices = "") 

server.R

get_fruitView <- reactive({ 
    switch(input$fruit_view, 
    "Top Fruit View" = dir(
     path = "data/fruits/", full.names = TRUE), 
    "Current Fruit Split" = dir(
     path = "data/splits/", full.names = TRUE), 
    "Top Fruit & Value Class" = dir 
    (path = "data/classes/", full.names = TRUE)) 
}) 


# loading the modeled data by fruit 

     visible_fruits <- reactive({ 

     fruit_choiced <- get(input$fruit_view) 

     fruit_choice <- view_choiced()[c(gsub(view_choiced(), 
     pattern = "[^_]+_[^_]+_([^_]+)_[^_]+", replacement = "\\1"))] 

     basename(unique(fruit_choice)) 
    }) 

    observe({ 
     updateSelectInput(session, "", 
     choices = visible_fruits()) 
    }) 

正则表达式工作正常,隔离我想的名字显示,但它们在用户输入中不可见。

在此先感谢。

顺便说一句,我得到这个错误信息:

Warning: Error in serverFuncSource: server.R returned an object of unexpected type: environment 
Stack trace (innermost first): 
    1: shiny::runApp 
Error in serverFuncSource() : 
    server.R returned an object of unexpected type: environment 
Warning: Error in get: object 'input' not found 
Stack trace (innermost first): 
    58: get 
    57: <reactive:visible_channels> [C:\Users\m.nierhoff\Desktop\AnalyseR\RAnalysen\channel-forecasting/server.R#60] 
    46: visible_channels 
    45: updateSelectInput 
    44: observerFunc [C:\Users\m.nierhoff\Desktop\AnalyseR\RAnalysen\channel-forecasting/server.R#69] 
    1: shiny::runApp 
+0

我无法验证我的意见,因为我没有什么数据没有做,但它看起来就像你没有在'updateSelectInput'中放入'inputId'变量一样,'shiny'不知道要更新哪个'selectInput'。 –

+0

使用'''get'''输入$''fruit_view'''将不起作用; '''view_choiced()'''在哪里? – amrrs

回答

0

这里是动态变化的输入选项的另一种方式。在这里,我使用uiOutputrenderUI功能对在我server.R创建UI元件,这样做的优点是,server.R可以执行任意R代码里面:

library(shiny) 

ui <- fluidPage(

    # Application title 
    titlePanel("Select CSV demo"), 

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
     sidebarPanel(
     radioButtons(
      inputId = "fruit_view", 
      label = "Select a fruit", 
      choices = c("Top Fruit View"="1", 
         "Current Fruit Split"="2", 
         "Top Fruit & Value Class"="3"), 
      selected = "1", 
      inline = TRUE) 
    ), 

     # Show a plot of the generated distribution 
     mainPanel(

     uiOutput("ChoicesUI") 
    ) 
    ) 
) 


server <- function(input, output) { 

    output$ChoicesUI <- renderUI({ 
    a <- read.table(paste0(input$fruit_view,".csv")) 
    selectInput("Choices2", 
       label="select from file", 
       choices = a$V1) 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 

我已手动创建3周的CSV称为1.csv2.csv,和3.csv每个行上有三个不同的元素来完成这项工作。

enter image description here

让我知道如果你需要进一步的解释或者这就是你想要它

+0

非常感谢您的意见@ michael-bird。你的例子工作正常,但它似乎不适合我的问题。我将编辑我的问题以使问题更加明显。 – MHN

相关问题