stdin() does not work properly in Rmarkdown (but does in Rstudio)

The following code does not work in Rmarkdown,
but should; it does work in Rstudio as an R script file.


x <- read.table(stdin(),header=T)
y z
1 2
3 4
2 5

x

Suppose that you create a pdf-file with RMarkdown?
You open the file; how do you expect the file to interact?

Thanks for your help.
I found a solution in R help files,
but it is clunkier than it should be:


x <- read.table(header = TRUE, text = "
a b
1 2
3 4
")

x

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

I am sure I do not understand what you are trying to achieve.
To clarify the conversation I have made a reprex of your case.
The following file was knitted with the knit button in RStudio.
If this is not what you did then let us know.

---
title: "Untitled"
author: "My name"
date: "6/5/2020"
output: html_document
---

```{r cars}
x <- read.table(stdin(),header=T)
```

with result

processing file: DavidR.Rmd
Quitting from lines 9-10 (DavidR.Rmd)
Error in read.table(stdin(), header = T) : no lines available in input
Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> read.table

Execution halted

Where do you expect that the stdin function get its data from?
I think that the stdin normally get its data only from an interactive session or maybe a script.
Knitting with the knit button normally does not set up an interactive session.
Are you trying to do the knit with a script? If so share a small 'prototype' of the components of the script:
bat-file (assuming Windows), R-file and Rmd-file.

When you expect help from us you should give more information than 'it does not work but it should'

Not sure what you mean. You can't create a pdf file, because the code does not compile and run. Just to be sure you understand, a complete Rmarkdown file would contain a header,
then the code segment I presented in the usual code block. The program claims to detect
an error, because it doesn't process the stdin() argument correctly.

Thanks for trying to help.
stdin() is the way to let your data be inline with the
rest of the program. So the line below that one
contains the header (y z) with the names of the
variables, and three lines of data follow. Then
there is a blank line (signaling end of data), and a command
x which should print the data. The data set is artificial
of course; this is an example based on a larger problem
I am dealing with.

David

An additional thought: To see what should happen,
copy my lines into an R script file and execute it.
(NOT an .Rmd file).

David, you are not answering my questions.
Below my final attempt to communicate with you.
Maybe it is helpful (at least it works for me).

---
title: "Untitled"
author: "My name"
date: "6/5/2020"
output: html_document
---

``` 

(input for) this file is DavidR.Rmd

run by "Rscript DavidR.R  < DavidR.txt" (file input) or "Rscript DavidR.R"  (manual input)

contents of DavidR.R is one line with "rmarkdown::render('DavidR.Rmd')"

```


```{r cars}
# next line will not work with manual standard input
#x <- read.table(stdin(),header=T)

# next line will take only the last line with manual standard input (EOF is 2* cntrl-Z) file input is okay
x <- read.table(file("stdin"),header=T)

# next line will read correctly manual standard input (EOF is 1* cntrl-Z) and file input
#x <- readLines(file("stdin"))
print(x)
```