r - Downloadhandler() 下载一个文件,其中只有实际数据帧 inRShiny 的列名
问题描述
我使用以下数据:
Database<-c("Composite","DB","TC","RH","DGI","DCH","DCH","DCH","LDP")
Organism<-c("Human","Human","Human","Human","Human","Human","Mouse","Rat","Human")
Unique_Drugs<-c(12672,5130,1425,3090,6100,2019,250,736,1182)
Unique_Targets<-c(3987,2175,842,2308,2413,1441,198,327,702)
Mean_S.D.Targets_per_Drug<-c("5.87 ± 6.72","2.60 ± 6.87","2.28 ± 3.76","3.29 ± 5.03","3.60 ± 5.21","6.28 ± 14.29"
,"1.92 ± 1.83"
,"4.11 ± 5.32"
,"4.27 ± 8.25"
)
Mean_S.D.Drugs_per_Target<-c("11.63 ± 15.59",
"12.52 ± 23.93",
"10.71 ± 8.37",
"12.98 ± 17.57",
"23.44 ± 25.65",
"13.87 ± 34.23",
"8.20 ± 18.44",
"14.82 ± 9.36",
"17.43 ± 9.34"
)
Unique_Drug_Target_Associations<-c(
45276,
14598,
3599,
12439,
23048,
13872,
594,
2876,
3915)
db<-data.frame(Database,Organism,Unique_Drugs,Unique_Targets,Mean_S.D.Drugs_per_Target,Mean_S.D.Targets_per_Drug, Unique_Drug_Target_Associations)
创建这个工作闪亮的应用程序。问题是,虽然(在浏览器中打开)文件被正确创建和下载,但它们是空的,或者准确地说,它们只包含列名。正如您从数据表中看到的那样,动态数据框得到了很好的更新。可能是响应式数据帧与downloadhandler()
内部的错误通信或错误downloadhandler()
?
#ui.r
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(
title = "Stats Table"
),
dashboardSidebar(
uiOutput("dbase"),
tags$hr(),
uiOutput("stats"),
tags$hr(),
h4(strong("Download Data in Table")),
textInput("filename","Specify Filename"),
#Added .json in the list
selectInput("extension","File Format", choices = c("txt","csv","tsv","json")),
column(1, align="center",
# Style font family as well in addition to background and font color
tags$style(type="text/css", "#downloadData {background-color:white;color: black;font-family: Arial}"),
downloadButton("downloadData", "Download",class = "butt1"),
tags$style(type='text/css', "#downloadData { vertical-align: middle;}")
)
),
dashboardBody(
DTOutput('tbl')
)
)
#server.r
library(shiny)
library(shinydashboard)
library(DT)
server <- function(input, output,session) {
#Create the static dataframe below
Database<-c("Composite","DB","TC","RH","DGI","DCH","DCH","DCH","LDP")
Organism<-c("Human","Human","Human","Human","Human","Human","Mouse","Rat","Human")
Unique_Drugs<-c(12672,5130,1425,3090,6100,2019,250,736,1182)
Unique_Targets<-c(3987,2175,842,2308,2413,1441,198,327,702)
Mean_S.D.Targets_per_Drug<-c("5.87 ± 6.72","2.60 ± 6.87","2.28 ± 3.76","3.29 ± 5.03","3.60 ± 5.21","6.28 ± 14.29"
,"1.92 ± 1.83"
,"4.11 ± 5.32"
,"4.27 ± 8.25"
)
Mean_S.D.Drugs_per_Target<-c("11.63 ± 15.59",
"12.52 ± 23.93",
"10.71 ± 8.37",
"12.98 ± 17.57",
"23.44 ± 25.65",
"13.87 ± 34.23",
"8.20 ± 18.44",
"14.82 ± 9.36",
"17.43 ± 9.34"
)
Unique_Drug_Target_Associations<-c(
45276,
14598,
3599,
12439,
23048,
13872,
594,
2876,
3915)
db<-data.frame(Database,Organism,Unique_Drugs,Unique_Targets,Mean_S.D.Drugs_per_Target,Mean_S.D.Targets_per_Drug, Unique_Drug_Target_Associations)
#Create the databases checkbox group
output$dbase<-renderUI({
checkboxGroupInput("base", label = "Specify dataset(s)",
choices = list("Composite","DB","TC","RH","DGI","DCH","LDP"),
selected = c("Composite","DB","TC","RH","DGI","DCH","LDP")
)
})
#Create the stats check box group
output$stats<-renderUI({
checkboxGroupInput("sta", label = "Specify statistic(s)",
choices = list("# Unique Drugs"="Unique_Drugs",
"# Unique Targets"="Unique_Targets",
"# of Drugs per Target"="Mean_S.D.Drugs_per_Target",
"# of Targets per Drug"="Mean_S.D.Targets_per_Drug",
"# Unique Drug-Target Associations"="Unique_Drug_Target_Associations"
),
selected = c("Unique_Drugs","Unique_Targets","Mean_S.D.Drugs_per_Target","Mean_S.D.Targets_per_Drug","Unique_Drug_Target_Associations")
)
})
#Subset the data based on user choices
df_subset <- reactive({
#By database
a <- subset(db, Database %in% input$base)
#By stats
keeps <- c("Database","Organism",input$sta)
# assigning the subset of db to tmp
tmp <- a[ , which(names(a) %in% keeps)]
# returning tmp
return(tmp)
})
#Download files with quotes or not depending on the quote=input$quotes which has value TRUE or FALSE.
output$downloadData <- downloadHandler(
filename = function() {
paste(input$filename, input$extension, sep = ".")
},
# This function should write data to a file given to it by
# the argument 'file'.
content = function(file) {
sep <- switch(input$extension,"txt"=",", "csv" = ",", "tsv" = "\t","json"=",")
# Write to a file specified by the 'file' argument
write.table(data.frame(df_subset())[input[["table_rows_all"]],], file, sep = sep,
row.names = FALSE)
}
)
output$tbl = renderDT(
df_subset(), options = list(lengthChange = FALSE),selection = list(target = 'row+column')
)
}
解决方案
我不知道为什么,但我不得不删除[input[["table_rows_all"]],]
推荐阅读
- git - Git如何撤消最近的合并提交然后更改为变基
- php - 如何对所有元素值求和并将该数字附加到数组的末尾?
- angular-material - 如何从角度材料转换日期格式
- java - 移动迭代器语句使代码编译
- javascript - 如何在这个 Chrome 扩展代码中有多个指令?
- prolog - 从列表 Prolog 中挑选元素
- swift - 为什么 serviceSubscriberCellularProviders 返回 nil?(在 iOS 12 中)
- python-3.x - TypeError:需要一个整数(得到类型 str)?
- c# - 获取每个 URL 目录
- c# - 带有 EF Core 的沙盒 Sqlite