2015-01-09 57 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))