我想弄清楚如何根據最終用戶所做的選擇來編寫.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
例如,基於對A
和D
用戶選擇的解決方案文件應該是這樣的:
Postcode,HC
10,2
'downloadButton'可能是一個更好的選擇,而不是'actionButton'你的情況 –
我真的不希望給選項,下載文件的最終用戶。這裏創建的.csv成爲一個更大的閃亮應用程序的依賴項,這裏沒有包含這個。 –