2017-09-27 10 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

相關問題