[英]How to upload a file and name the columns in Shiny in R?
To upload a file in a Shiny app that reads the data, name the variables (columns) and do some data analysis before presenting plot output要在读取数据的 Shiny 应用程序中上传文件,请在呈现绘图输出之前命名变量(列)并进行一些数据分析
I am using this app from Shiny gallery as a reference: enter link description here我正在使用来自 Shiny 画廊的这个应用程序作为参考:在此处输入链接描述
I want to use the uploaded data in many outputs after doing different analyses.在进行不同的分析后,我想在许多输出中使用上传的数据。 So, instead of reading file inside renderTable
or renderPlot
, I read it in server
function:因此,我没有在renderTable
或renderPlot
中读取文件,而是在server
函数中读取它:
server <- function(input, output) {
inFile <- reactive({input$file1})
sdf <- reactive({read.csv(inFile()$datapath, header=F)})
colnames(sdf()) <- c('Vehicle.ID', 'Time', 'Vehicle.class.no', 'Vehicle.type2',
'Vehicle.Length', 'Lane', 'Preceding.Vehicle.ID', 'Spacing','Spacing2', 'State',
'svel.mps', 'deltaV.mps', 'sacc', 'lane.change') }
But when I run this app I get:但是当我运行这个应用程序时,我得到:
shiny::runApp('app-CC')闪亮::runApp('app-CC')
Listening on http://127.0.0.1:7484
Error in colnames(sdf()) <- c("Vehicle.ID", "Time", "Vehicle.class.no", :
invalid (NULL) left side of assignment
How can I fix this error?我该如何解决这个错误? I don't want to read the same file again in every render*
function.我不想在每个render*
函数中再次读取同一个文件。 Are there any online examples of shiny apps where a new file is read, column names are defined and then some analysis is done before using the render*
functions?是否有任何在线示例读取新文件,定义列名,然后在使用render*
函数之前进行一些分析?
You'd be better off assigning the column names during the read.csv
你最好在read.csv
期间分配列名
server <- function(input, output) {
inFile <- reactive({input$file1})
sdf <- reactive({
read.csv(inFile()$datapath, header=F, col.names = c('Vehicle.ID', 'Time', 'Vehicle.class.no', 'Vehicle.type2',
'Vehicle.Length', 'Lane', 'Preceding.Vehicle.ID', 'Spacing','Spacing2', 'State',
'svel.mps', 'deltaV.mps', 'sacc', 'lane.change')
)
})
}
Alternatively I believe you can perform multiple operations in the reactive block as long as you return the final object或者我相信你可以在反应块中执行多个操作,只要你返回最终对象
server <- function(input, output) {
inFile <- reactive({input$file1})
sdf <- reactive({
dd<-read.csv(inFile()$datapath, header=F)
colnames(dd) <- c('Vehicle.ID', 'Time', 'Vehicle.class.no', 'Vehicle.type2',
'Vehicle.Length', 'Lane', 'Preceding.Vehicle.ID', 'Spacing','Spacing2', 'State',
'svel.mps', 'deltaV.mps', 'sacc', 'lane.change')
)
dd
})
}
An alternative is to use an actionButton
and then to check for the validity of the files when you upload them.另一种方法是使用actionButton
,然后在上传文件时检查文件的有效性。 Lots of observeEvent
is probably appropriate for firing off when something is "triggered," like a file being uploaded or a button being pressed.大量的observeEvent
可能适合在某些“触发”时触发,例如上传文件或按下按钮。 In addition, to make a chain of changes, it's probably best to have an "update" reactiveValue()
thing to fire based on flags (which, I admit, is kind of messy, but works with Shiny since it doesn't have a proper callback framework like JS.)此外,要进行一系列更改,最好根据标志来触发“更新” reactiveValue()
事物(我承认这有点混乱,但可以与 Shiny 一起使用,因为它没有适当的回调框架,如 JS。)
There is a tutorial on RStudio that teaches how to solve problems such as these. RStudio上有一个教程,教您如何解决此类问题。 Hope it helps. 希望能帮助到你。
https://shiny.rstudio.com/tutorial/written-tutorial/lesson6/ https://shiny.rstudio.com/tutorial/write-tutorial/lesson6/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.