2017-04-14 112 views
0

我想從我上傳到應用程序的「汽車」數據集中獲取速度變量。基本上在選擇速度:我想所有的數字出現在數據集$速度。在selecInput下,選擇應該取決於我使用fileInput上傳的數據集。我怎樣才能完成這項任務。現在我已經將這些選項添加爲1,2,3。理論上應該有汽車數據集的速度變量的所有值。FileInput按鈕與selectInput閃亮..!

library(shiny) 
library(datasets) 
##the file I am uploading 
data(cars) 
dataset=write.csv(cars, "dataset.csv") 


ui=fluidPage( 

actionButton("upload", "Upload File"), 
bsModal("uploadFile", " ", "upload", 
sidebarLayout(
sidebarPanel(
fileInput("file","Choose file to upload") 

), 
mainPanel(
tableOutput("contents") 
) 
) 
), 



sidebarLayout(

sidebarPanel(

column(3, selectInput("selectElement", "Select speed:", c(1,2,3),multiple = 
T, selectize = F) 

) 
), 

mainPanel(
) 
) 
) 

server=function(input,output,session){ 

output$contents <- renderTable({ 

inFile <- input$file 

if (is.null(inFile)) 
return(NULL) 

read.csv(inFile$datapath) 
}) 
} 

shinyApp(ui,server) 

回答

1

我提前一個不太完整的響應道歉:見下文。

首先回答您的查詢:

如果你有一個像汽車的數據集,以確定「速度」標籤,你可以這樣做:

labls <- unique(cars$speed) 
... 
selectInput("selectElement", "Select speed:", labls, multiple = 
          T, selectize = F) 

我希望發佈完整的例子,但當前的邏輯(可能是因爲發佈的代碼有限?)似乎不正確:應用程序a)如何離開用戶來選擇要使用的文件;並在同一時間b)已經過濾的速度? 當然,您可能計劃顯示具有所有名爲「速度」的列的數據集,那麼它將有意義:)

此外,但這不是你的問題的一部分,你似乎使用模態對話通過包裏的shinyBS。

由於版本0.14的閃亮(約2016年10月)閃亮有一個非常好的模式功能,我個人認爲它會更好地使用本地功能。

我希望發佈一個從您的代碼派生的簡單示例(但selectInput的速度已被註釋掉,因爲如前所述,它不會在發佈的示例的上下文中正確顯示)。

library(shiny) 
library(datasets) 
data(cars) 
dataset = write.csv(cars, "dataset.csv") 

labls <- unique(cars$speed) # I left this in the code 

ui=fluidPage( 
    sidebarLayout(
    sidebarPanel(
     actionButton("upload", "Upload File") 
    ), 

    mainPanel(tableOutput("contents")) 
)) 
server=function(input,output,session){ 
# Show modal when button is clicked. 
observeEvent(input$upload, { 
    showModal(dataModal()) 
    }) 
dataModal <- function(failed = FALSE) { 
    modalDialog(
    fileInput('inputId', label=NULL, multiple = FALSE, accept = NULL, width =  NULL, buttonLabel = "Browse...", placeholder = "No file selected") 
# , selectInput("selectElement", "Select speed:", labls, multiple = 
#        T, selectize = F) 
) 
} 
output$contents <- renderTable({ 
if (length(input$inputId)== 0) return(NULL) 
    inFile <- input$inputId 
# if (is.null(input$selectElement)) return(NULL)  
input$inputId 
}) 
} 

shinyApp(ui,server) 
+0

此代碼不起作用 – Sumeda

+0

它適用於我。我正在使用Chrome。從sessionInfo()中提取。請檢查你是否有相同的版本(特別是有光澤)。 > sessionInfo()R 3.3.3版(2017年3月6日) (...) 其他附軟件包: [1] shiny_1.0.1 經由一個命名空間加載(和未附): [1 ] R6_2.2.0 htmltools_0.3.5 tools_3.3.3 Rcpp_0.12.10 [5] jsonlite_1.4 digest_0.6.12 xtable_1.8-2 httpuv_1.3.3 [9] mime_0.5 – Enzo