我是 RShiny 的新手(和一般的 R),不太清楚如何做以下事情。我有一個包含 50 個以 P2.csv、P3.csv 或 P5.csv 結尾的 CSV 檔案的串列。我想制作一個允許用戶選擇特定價格(P2、P3、P5)的應用程式,然后向他們展示具有該結束模式的 csv 檔案串列,然后用戶可以從中選擇最多 3 個需要加載到記憶體中。
我已經分別找到了如何列出具有特定結尾的所有檔案,并且我認為我需要使用 updateSelectizeInput 以便每次用戶選擇價格(例如 P2)時,他都會看到所有 csv 檔案 appleP2.csv、orangeP2 .csv 等例如,但想不出辦法使這項作業。
任何幫助是極大的贊賞!謝謝你。
library(shiny)
#listing the csv files ending with a same pattern
same_price_P2<-list.files(pattern="P2.csv")
same_price_P3<-list.files(pattern="P3.csv")
same_price_P5<-list.files(pattern="P5.csv")
shinyUI(fluidPage(
titlePanel("Price of fruit"),
#user can select the price of the fruit
selectInput("Price", "Price", choices = c("5 euros"="P5", "2 euros"="P2", "3 euros"="P3"), selected="P2", multiple=FALSE)),
#user must be presented only with the CSV files associated to the selected price and can select maximum of 3
#files to be read
selectizeInput("Fruit", "Fruit", choices = "", selected = NULL, multiple=TRUE, options=list(maxItems=3)),
tableOutput("dataset")
)
shinyServer(function(session, input, output){
#Populate this by filtering through and showing the csv files based on whether they end in P2.csv, P5.csv, P3.csv
#the selected files by the user should then be read
observeEvent(
input$Price,
updateSelectizeInput(session, "Fruit", "Fruit",
choices = ................[..........==input$Price]))
output$dataset <- renderTable({
data
})
})
uj5u.com熱心網友回復:
您已經找到了主要步驟。
最后的步驟是:
choice在函式的引數中插入所有可能的 csv 名稱(無論結尾如何)selectizeInput。(我們在第二步也需要這個向量,因此它是在selectizeInput函式之外指定的。->all_price)- 過濾 中
choices的input$Price變數updateSelectizeInput。 - 將表加載到記憶體中。
下面的代碼將幫助您完成第一步。因為這似乎是您目前正在處理的問題。
library(shiny)
#listing the csv files ending with a same pattern
same_price_P2 <- c("appleP2.csv", "bananaP2.csv", "cranberryP2.csv", "dateP2.csv")
same_price_P3 <- c("appleP3.csv", "bananaP3.csv", "cranberryP3.csv", "dateP3.csv")
same_price_P5 <- c("appleP5.csv", "bananaP5.csv", "cranberryP5.csv", "dateP5.csv")
all_price <- c(same_price_P2, same_price_P3, same_price_P5)
ui <- fluidPage(
titlePanel("Price of fruit"),
#user can select the price of the fruit
selectInput("Price",
"Price",
choices = c("5 euros" = "P5",
"2 euros" = "P2",
"3 euros" = "P3"),
selected = "P2",
multiple = FALSE),
#user must be presented only with the CSV files associated to the selected price and can select maximum of 3
#files to be read
selectizeInput("Fruit",
"Fruit",
choices = all_price,
selected = NULL,
multiple = TRUE,
options = list(maxItems = 3)),
textOutput("dataset")
)
server <- function(input, output, session){
#Populate this by filtering through and showing the csv files based on whether they end in P2.csv, P5.csv, P3.csv
#the selected files by the user should then be read
observe({
updateSelectizeInput(session,
"Fruit",
"Fruit",
choices = grep(paste0(input$Price,
".csv$"),
all_price,
value = T))
})
output$dataset <- renderText({
input$Fruit
})
}
shinyApp(ui, server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417054.html
標籤:
上一篇:DaskParserError:讀取CSV時錯誤標記資料
下一篇:如何在PHP中合并多個CSV檔案
