在一个闪亮的应用程序的数据表中,我有大约2万张图像。我想删除不喜欢的图像,然后下载生成的数据表(仅包含我想要的图像的路径名)。
我设法看到一个数据框,每一行都显示一张图像。我可以删除不喜欢的行。我现在希望能够将数据框(仅包含文件路径而不是实际图像)下载到csv中。我似乎无法使用downloadHandler做到这一点。我想念什么?
这是我的代码:
server.R
library(shiny)
library(shinydashboard)
library(data.table)
library(DT)
server<-shinyServer(function(input, output) {
vals<-reactiveValues()
vals$Data<-data.table(
df
)
output$MainBody<-renderUI({
fluidPage(
box(width=12,
hr(),
column(12,dataTableOutput("Main_table")),
tags$script(HTML('$(document).on("click", "input", function () {
var checkboxes = document.getElementsByName("row_selected");
var checkboxesChecked = [];
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i].value);
}
}
Shiny.onInputChange("checked_rows",checkboxesChecked);
})')),
tags$script("$(document).on('click', '#Main_table button', function () {
Shiny.onInputChange('lastClickId',this.id);
Shiny.onInputChange('lastClick', Math.random())
});")
)
)
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$Main_table, ".csv", sep = "")
},
content = function(file) {
write.csv(vals$Data, file, row.names = FALSE)
}
)
output$Main_table<-renderDataTable({
DT=vals$Data
datatable(DT,
escape=F)}
)
})
ui.R
ui<-fluidPage(dashboardHeader(disable = T),
dashboardSidebar(disable = T),
downloadLink("downloadData", "Download"),
dashboardBody(uiOutput("MainBody")
)
)
# Run the application
shinyApp(ui = ui, server = server)
答案 0 :(得分:1)
问题在于def parse_file(filepath):
data = []
with open(filepath, 'r') as file_object:
row = {} # prepare an empty row
for line in file_object:
key, match = _parse_line(line)
# search for keys in the line
if key == 'value_one':
value_one = match.group('value_one')
value_one = int(value_one)
if 'value one' in row: # we always have a full row
data.append(row) # append it to the data liest
row = {} # and reset it
row['value one'] = value_one # we have a match: store the value in row
if key == 'value_two':
value_two = match.group('value_two')
value_two = int(value_two)
if 'value two' in row:
data.append(row)
row = {}
row['value two'] = value_two
if key == 'value_three':
value_three = match.group('value_three')
value_three = int(value_three)
if 'value three' in row:
data.append(row)
row = {}
row['value three'] = value_three
if row != {}: # do not forget the last row
data.append(row)
data = pd.DataFrame(data)
return data
是df
中的函数。请参阅library(stats)
。请不要将?df
用作变量名-这只会引起混乱;取df
代替。您将此函数包装到DF
中,并将其传递到data.table()
中(请参阅我添加的reactiveValue
)。
此构造被print()
视为list()
,并且不知道如何处理write.csv()
并导致以下错误:
write.table中的错误:'EncodeElement'中未实现的类型'list'
因此,您可能想通过以下方法修复list()
初始化传递data.table
而不是NULL
:
df
此外,您应该修复文件名(library(shiny)
library(shinydashboard)
library(data.table)
library(DT)
server <- shinyServer(function(input, output) {
vals <- reactiveValues(myTabData = data.table(NULL))
vals$Data <- data.table(df)
print(paste("myData:", isolate(vals$Data)))
output$MainBody <- renderUI({
fluidPage(box(
width = 12,
hr(),
column(12, dataTableOutput("Main_table")),
tags$script(
HTML(
'$(document).on("click", "input", function () {
var checkboxes = document.getElementsByName("row_selected");
var checkboxesChecked = [];
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i].value);
}
}
Shiny.onInputChange("checked_rows",checkboxesChecked);})'
)
),
tags$script(
"$(document).on('click', '#Main_table button', function () {
Shiny.onInputChange('lastClickId',this.id);
Shiny.onInputChange('lastClick', Math.random())});"
)
))
})
output$downloadData <- downloadHandler(
filename = function() {
"Main_table.csv"
},
content = function(file) {
write.csv(vals$myTabData, file, row.names = FALSE)
# Warning: Error in write.table: unimplemented type 'list' in 'EncodeElement'
# write.csv(vals$Data, file, row.names = FALSE)
}
)
output$Main_table <- renderDataTable({
DT = vals$Data
datatable(DT, escape = FALSE)
})
})
ui <- fluidPage(
dashboardHeader(disable = T),
dashboardSidebar(disable = T),
downloadLink("downloadData", "Download"),
dashboardBody(uiOutput("MainBody"))
)
shinyApp(ui = ui, server = server)
不存在)