r - R Shiny - 在服务器加载时加载 csv 文件
问题描述
我想让我的 server.R 文件在启动时加载二进制矩阵的 csv 文件。
library(shiny)
server <- function(input, output) {
#this aint loading
df <- read.csv("starGraphAdjMatrix.csv",
header = TRUE,
sep = ",",
quote='"')
#output$loadedMat -> output$loadedMat
output$loadedMat <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
#falsy value if empty
req(input$file1)
# when reading semicolon separated files,
# having a comma separator causes `read.csv` to error
tryCatch(
{
df <- read.csv(input$file1$datapath,
header = TRUE,
sep = ",",
quote='"')
df$X <- NULL
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
return(df)
},
rownames = FALSE, colnames = FALSE)
}
包含 ui.R 和 starGraphAdjMatrix 的完整代码在这里: https ://github.com/andandandand/fixCSVLoad
解决方案
不确定这是否是您所追求的:
library(shiny)
server <- function(input, output) {
output$contents <- renderTable({
if (is.null(input$file1$datapath)) {
dpath <- "starGraphAdjMatrix.csv"
} else {
dpath <- input$file1$datapath
}
read.csv(dpath)
}, rownames = FALSE, colnames = FALSE)
}
推荐阅读
- typescript - 为什么 {a: never} 不等于从不?
- css - 当包裹在 React Native 中的视图中时,将文本居中对齐不起作用
- python - 为什么我的 pygame 代码因“RecursionError:调用 Python 对象时超出最大递归深度”而崩溃?
- javascript - 当我将两个相同(由 IntelliJ 确认)的 JS 脚本加载到我的 Web 应用程序中时,一个会产生与另一个不同的结果
- flutter - Flutter:解析 JSON 数组
- c# - 我应该使用什么算法来最大化我可以做的任务数量(有截止日期)?
- css - 文本下框阴影的 CSS
- vb.net - 在VB.net中自动删除文件时避免文件正在使用
- debugging - x64dbg 无法打开 exe 文件(但可以附加),为什么?
- scala - Scala:在编译时验证类参数不是 instanceOf 特征