2017-04-20 24 views
1

我試圖將我的應用中的滑動條值連接到表,從上傳的文件呈現,但我無法理解如何正確執行該操作。這是怎樣的代碼看起來現在:將sliderValues應用於輸入的文件

server.r

library(shiny) 

function(input, output) { 

    # upload the csv file 

    output$contents <- renderTable({ 

    inputFile <- input$file1 

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

    read.csv(inputFile$datapath, header=input$header, sep=',', 
      quote="'") 
    }) 

    # Create a frame with our values 

    sliderValues <- reactive({ 

    # Compose data frame 
    data.frame(
     Name = c("cost", 
       "range"), 
       Value = as.character(c(input$cost, 
             paste(input$range, collapse=' ') 
       ), 
       stringsAsFactors=FALSE) 
    ) 
    }) 

    # Show the values 
    output$values <- renderTable({ 
    sliderValues() 
    }) 
} 

ui.r

library(shiny) 

fluidPage(

    titlePanel("Cars plot"), 

    sidebarLayout(
    sidebarPanel(
     fileInput('file1', 'Choose CSV File', 
       accept=c('text/csv', 
       'text/comma-separated-values,text/plain', 
       '.csv')), 
     helpText("Note: svc file should be separated by comas, and contain ", 
       "single quotes only."), 
     checkboxInput('header', 'Header', TRUE), 
     tags$hr(), 
     sliderInput("cost", "Max cost:", 
        min=0, max=1000000, value=500000, pre = "$"), 
     sliderInput("range", "Range of MaxSpeed:", 
        min = 50, max = 500, value = c(100,450)) 
     ), 

    mainPanel(
     tableOutput('contents'), 
     tableOutput('values') 
    ) 
) 
) 

最後,我使用來測試它看起來像這樣的文件:

Car, Horsepower, MaxSpeed, Cost 
AlfaRomeo, 200, 300, 200000 
AstonMartin, 400, 310, 300000 

回答

1

幾乎完成。只是input值賦給你dataframesubset

mytable<-read.csv(inputFile$datapath, header=input$header, sep=',', 
     quote="'") 

subset(mytable,mytable$Cost<input$cost & mytable$MaxSpeed>=input$range[1] & mytable$MaxSpeed<=input$range[2]) 
+0

謝謝!正是我在找什麼。我不知道「子集」,今天是我學習的第一天。 –