2015-01-09 86 views
2

我想弄清楚如何根據最終用戶所做的選擇來編寫.csv。所做的選擇將子集「geodata.csv」並在應用程序文件夾中編寫一個單獨的「solution.csv」文件。R /閃亮 - 如何寫一個.csv反應包括一個actionButton?

N.B. - 我創建了一個github repo以使問題更容易解決。它包含geodata.csv,ui.R & server.R但沒有solution.csv呢!

geodata.csv

Postcode,HC,BSL,Position Location 
10,1,A,C 
10,1,A,D 
10,1,A,D 
11,1,B,C 
11,1,B,C 

ui.R

shinyUI(
pageWithSidebar(
    headerPanel('Min. working example - write a csv based on user input'), 

    sidebarPanel(
    selectInput("filter1", "First selection:" 
       , choices = c(Choose='', "A", "B") 
       #, multiple=T 
      ), 

    selectInput("filter2", "Second selection:", 
       choices = c(Choose='', "C", "D") 
      ), 
    br(), 
    p("Include actionButton to prevent write occuring before user finalises selection"), 
    actionButton("generateButton","Write Data")  
), 
    mainPanel() 
) 
) 

server.R

# Load data 
setwd("/Users/lukesingham/SOtestApp") 
geodata <- read.csv("geodata.csv", na.string = "#N/A", row.names=NULL) 

# Reactivity to subset data #### 
shinyServer(function(input, output) { 
    geodatasetInput <- reactive({ 

    # BSL switch 
    selection <-switch(input$BSL 
         , A = "A" 
         , B = "B" 
         ) 
    # Location switch  
    selection2 <-switch(input$Location 
         , C = "C" 
         , D = "D" 
         ) 

    # subset based on selection 
    Subgeodata <- subset(geodata, BSL == selection & Position.Location == selection2) 

    # Execute selections on data upon button-press 
    input$generateButton 

    # aggregate by postcode 
    Subgeodata <- Subgeodata[1:2] #no longer need other columns 
    AggSubGdata <- aggregate(. ~ Postcode, data=Subgeodata, FUN=sum) 
    isolate(write.csv(AggSubGdata 
       , file = "/Users/lukesingham/SOtestApp/solution.csv" 
       , row.names=F 
    )) 
    }) 
}) 

solution.csv

例如,基於對AD用戶選擇的解決方案文件應該是這樣的:

Postcode,HC 
10,2 
+0

'downloadButton'可能是一個更好的選擇,而不是'actionButton'你的情況 –

+0

我真的不希望給選項,下載文件的最終用戶。這裏創建的.csv成爲一個更大的閃亮應用程序的依賴項,這裏沒有包含這個。 –

回答

5

這裏是工作示例:

# Load data 
setwd("/Users/lukesingham/SOtestApp") 
geodata <- read.csv("geodata.csv", na.string = "#N/A", row.names=NULL) 

# Reactivity to subset data #### 
shinyServer(function(input, output) { 
    geodatasetInput <- observe({ 

    # Execute selections on data upon button-press 
    if(input$generateButton == 0) return() 

    inp.BSL <- isolate(input$filter1) 
    inp.loc <- isolate(input$filter2) 
    if (inp.BSL=='' | inp.loc=='') return() 

    # BSL switch 
    selection <-switch(inp.BSL 
         , A = "A" 
         , B = "B" 
         ) 
    # Location switch  
    selection2 <-switch(inp.loc 
         , C = "C" 
         , D = "D" 
         ) 

    # subset based on selection 
    Subgeodata <- subset(geodata, BSL == selection & Position.Location == selection2) 

    # browser() 
    # aggregate by postcode 
    Subgeodata <- Subgeodata[1:2] #no longer need other columns 
    AggSubGdata <- aggregate(. ~ Postcode, data=Subgeodata, FUN=sum) 
    write.csv(AggSubGdata 
       , file = "solution.csv" 
       , row.names=F 
    ) 
    }) 
}) 

和你的代碼的簡短分析:

  1. 的主要原因geodatasetInput因爲它是一個reactive()不啓動就大家表達。 reactive()只有在@pops的回答中被其他人調用時才被評估,如renderTable()。如果你想讓它自己執行,它應該是observe()

  2. 在observe()表達式的開始處有input$generateButton可能是個好主意。

  3. ui.R,你叫一個數字輸入欄filter1,但你試圖獲取其值分別爲input$BSLserver.R; filter2也是如此。

  4. 當你想geodataSetInput僅在generateButton被觸發,withing geodataSetInput所有其他input$和無功表達式應該isolate()隔離。另一方面,不需要隔離write.csv,因爲這個特定的函數調用不涉及任何「動態」參數。

1

你需要渲染你的你子集劃分的表中,也許有人否則可以解釋爲什麼你需要這種共同依賴。下面是我去解決它的方式的工作示例。它會將solution.csv保存在工作目錄中。

rm(list = ls()) 
library(shiny) 
# You can change this directory 
setwd("/Users/lukesingham/SOtestApp") 
geodata <- read.csv("geodata.csv", header = TRUE, sep = ",",stringsAsFactors =FALSE) 

ui = pageWithSidebar(
    headerPanel('Min. working example - write a csv based on user input'), 
    sidebarPanel(
    selectInput("filter1", "First selection:", choices = c("A", "B"),selected = "A"),  
    selectInput("filter2", "Second selection:", choices = c("C", "D"),selected = "C"), 
    br(), 
    p("Include actionButton to prevent write occuring before user finalises selection"), 
    actionButton("generateButton","Write Data")),mainPanel(tableOutput("test1")) 
) 

server = function(input, output) { 

    geodatasetInput <- reactive({ 
    if(input$generateButton == 0){return()} 

    isolate({ 
     input$generateButton 
     test_data <- geodata[geodata$BSL %in% as.character(input$filter1),] 
     test_data <- geodata[geodata$Position.Location %in% as.character(input$filter2),] 
     test_data <- test_data[,1:2] 
     test_data <- aggregate(. ~ Postcode, data=test_data, FUN=sum) 
     test_data 
    }) 
    write.csv(test_data,"solution.csv",row.names=F) 
    }) 
    output$test1 <- renderTable({geodatasetInput()}) 
} 

runApp(list(ui = ui, server = server))