简体   繁体   English

读取文件并将其保存到对象中。 Shiny R中的[不显示加载的数据]

[英]read a file and save it into an object. [No display of loaded data ] in Shiny R

I know there are a few questions asked about this here is SO. 我知道这里有一些问题要问。

But I couldn't figure out the solution to my problem from them. 但是我无法从他们那里找到解决我问题的方法。

I want to Upload a file into R through shiny and assign it to an object say the object name "Object_name". 我想通过闪亮将文件上传到R中,并将其分配给对象,例如对象名称“ Object_name”。

I used fileInput in ui.R and did this 我在ui.R中使用了fileInput并做到了

fileInput('file1', h4('Choose Your Data File'),
                            accept=c('text/csv',
                                     'text/comma-separated-values,text/plain',
                                     '.OUT'))

Now I accesses the "file1" and tried to recover my uploaded file through file1$datapath. 现在,我访问“ file1”,并尝试通过file1 $ datapath恢复上传的文件。 I couldn't actually do this. 我实际上无法做到这一点。 I am getting console errors like.... 我收到类似控制台的错误。

1. Error in terms.formula(formula, data = data) : 
  'data' argument is of the wrong type

And in GUI in the plot plane its showing Error:'data' argument is of the wrong type 并且在绘图平面的GUI中,其显示的Error:'data' argument is of the wrong type

I have added this piece of code in shinyserver server.R 我已经在Shinyserver server.R中添加了这段代码

mydata <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    object_name <- read.table(inFile$datapath, header = TRUE)
  })

Is it allowed to access the file directly from the datapath as I am doing above. 是否可以像我在上面那样直接从数据路径访问文件。 Why doesn't it load the uploaded data into "a". 为什么不将上传的数据加载到“ a”中。

If somebody knows the answer please help me out. 如果有人知道答案,请帮助我。

Update: 更新:

My dataset is of header = False. 我的数据集的标题为False。 So once I upload the data I have to change the names(my dataset) and then further calculations can be performed. 因此,一旦我上传数据,我就必须更改名称(我的数据集),然后可以执行进一步的计算。

Considering this code 考虑这段代码

a <- reactive({
   inFile <- input$file1
   if (is.null(inFile)) return(NULL)
   read.table(inFile$datapath, header = TRUE)
 })

Still I cant access the a() [i,e the data I want] and use it into further calculations. 我仍然无法访问a()[即我想要的数据]并将其用于进一步的计算。 I cant assign like names(a) <- c(.......) outside reactive because it throws error if I do that ... 我无法在反应式外部分配类似names(a) <- c(.......)因为如果这样做我会抛出错误...

Error in names(a) <- c("Ei", "Mi", "hours", "Nphy", "Cphy", "CHLphy",  : 
  names() applied to a non-vector

And I cant do it inside reactive because I can only assign names once the data "a" or "a()" is ready. 而且我无法在React中做到这一点,因为我只能在数据“ a”或“ a()”准备好后才能分配名称。 so how can I achieve this. 所以我怎么能做到这一点。

In your server.r you need to do read.csv (or whatever corresponds to your input file format) to get at the data. server.r您需要执行read.csv (或与输入文件格式相对应的任何操作)来获取数据。 Something like: 就像是:

MyData<-reactive({ read.csv(input$file1$datapath, ...) })

Where ... is whatever other arguments that you need for read.csv() . 其中...是read.csv()所需的其他任何参数。 Then you can access your data by referring to it as MyData() . 然后,您可以通过将其称为MyData()来访问数据。

Edit If you want to add column names (that aren't in the original .csv) they you can use the col.names argument in read.csv() . 编辑如果要添加列名(不在原始.csv中),则可以在read.csv()使用col.names参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM