Reputation: 221
So I have been having a hard time trying to get the data in my renderDataTable
function to work with the downloadHandler
function. So the problem that I am having is when I run the shiny application, hit the checkbox for the filter option, select the variable sex 0(female), and download the data, I get all of the observations as supposed to the filtered data.
Sample data:
"","age","sex","bmi","children","smoker","region","expenses"
"1",19,"0",27.9,0,"1","0",16884.92
"2",18,"1",33.8,1,"0","1",1725.55
"3",28,"1",33,3,"0","1",4449.46
"4",33,"1",22.7,0,"0","2",21984.47
"5",32,"1",28.9,0,"0","2",3866.86
"6",31,"0",25.7,0,"0","1",3756.62
"7",46,"0",33.4,1,"0","1",8240.59
"8",37,"0",27.7,3,"0","2",7281.51
"9",37,"1",29.8,2,"0","3",6406.41
"10",60,"0",25.8,0,"0","2",28923.14
"11",25,"1",26.2,0,"0","3",2721.32
"12",62,"0",26.3,0,"1","1",27808.73
"13",23,"1",34.4,0,"0","0",1826.84
"14",56,"0",39.8,0,"0","1",11090.72
"15",27,"1",42.1,0,"1","1",39611.76
"16",19,"1",24.6,1,"0","0",1837.24
"17",52,"0",30.8,1,"0","3",10797.34
"18",23,"1",23.8,0,"0","3",2395.17
"19",56,"1",40.3,0,"0","0",10602.39
"20",30,"1",35.3,0,"1","0",36837.47
ui.R:
tabItem(tabName = "data",
h2("Insurance Data Page"),
checkboxInput("dataset","Filter option:"),
conditionalPanel(
condition = "input.dataset==true",
selectInput("sex", "Variable sex:", levels(insurance_data_update$sex))
),
dataTableOutput("data_read"),
downloadButton("data_saved","Download this data")
),
server.R:
#Filtering factor columns
getData <- reactive({
newData <- insurance_data_update #%>% filter(sex == input$sex)#, smoker==input$smoker, region==input$region)
})
# Reading in the data
output$data_read = renderDataTable({
#newData <- getData()
if(input$dataset==TRUE){
if(input$sex==0){
#newData <- newData %>% filter(sex==input$sex)
getData() %>% filter(sex==input$sex)
getData()
#newData
}else{
#newData <- newData %>% filter(sex==input$sex)
#newData
getData() %>% filter(sex==input$sex)
getData()
}
}else{
#newData
getData()
}
output$data_saved = downloadHandler(
filename="insurance.csv",
content = function(file){
write.csv(getData(), file)
}
)
I am not entirely sure what I am doing wrong.
Any help is greatly appreciated.
Upvotes: 0
Views: 454
Reputation: 1928
I think this should work.
Edit: There are different inputs that can be taken from datatables, and this will export the data (getData()
) as it appears in the datatable - meaning any filtering you do on the table will be reflected as well as filtering performed on the data fed to DT
. The output for the datatable, output$data_read
, is what feeds into the input value data_read_rows_all
, which is basically DT ouput name+ _rows_all
.
output$data_saved = downloadHandler(
filename="insurance.csv",
content = function(file){
write.csv(getData()[input[["data_read_rows_all"]], ], file)
}
Check here for more info!
Upvotes: 1
Reputation: 206197
Create a new reactive element that just shows your table data and then use that for the table and for the download. For example
#Filtering factor columns
getData <- reactive({
insurance_data_update
})
tableData <- reactive({
if(input$dataset==TRUE){
if(input$sex==0){
getData() %>% filter(sex==input$sex)
} else {
getData() %>% filter(sex==input$sex)
}
}else{
getData()
}
})
output$data_read = renderDataTable({
tableData()
})
output$data_saved = downloadHandler(
filename="insurance.csv",
content = function(file){
write.csv(tableData(), file)
}
)
Upvotes: 3