RStudio uses a non-interactive R session to compile R Markdown
documents, which means the functions that involve human interaction
will not work. The only way to make these functions work is to use an
interactive R session, which basically means you should not click the
button in RStudio to compile the documents, but should type
rmarkdown::render() in the R console instead. You cannot put the
numbers under scan() and expect they will be read by scan() as if they
were typed by a human, even though they look similar.

If the function in your case only allows file input for scan(), I
guess the only way is to make a connection, such as textConnection()
as you mentioned.

Regards,
Yihui
--
Yihui Xie <xieyi...@gmail.com>
Web: http://yihui.name


On Sat, Oct 25, 2014 at 10:58 AM, John Fox <j...@mcmaster.ca> wrote:
> Dear Peter,
>
> Thanks for the suggestion -- it's a bit better for the example I provided 
> than my approach of using textConnection() of putting the lines in a file.
>
> I'd still be interested in whether I've missed something like a text-chunk 
> option that would allow the R Markdown document to behave like a script 
> executed at the console. For example, your approach doesn't work for the 
> application where I encountered the problem, which is in a function that 
> calls scan() and that passes the file argument to it, but doesn't use the 
> text argument.
>
> Best,
>  John
>
> On Sat, 25 Oct 2014 17:11:14 +0200
>  peter dalgaard <pda...@gmail.com> wrote:
>>
>> > On 25 Oct 2014, at 16:43 , John Fox <j...@mcmaster.ca> wrote:
>> >
>> > Dear JJ and list members,
>> >
>> > I wonder whether it's possible to get scan() to read in-line data in an R 
>> > Markdown document. The following code, for example, works (of course) when 
>> > entered in the R console but fails in an R block in an R Markdown document 
>> > (using an up-to-date version of RStudio):
>> >
>> > ```{r}
>> > x <- scan()
>> > 1 2 3
>> > 4 5 6
>> >
>> > x
>> > ````
>> >
>> > I'm aware of a couple of work-arounds, such as putting the data in a file 
>> > or a character string (as below), but am interested in whether it's 
>> > possible to get this to work directly.
>> >
>> > ```{r}
>> > text <- "
>> > 1 2 3
>> > 4 5 6
>> > "
>> > (x <- scan(file=textConnection(text)))
>> > ````
>> >
>> > Any help would be appreciated.
>>
>> This generally isn't easy. You can't source() a file like that either, it 
>> only works on std.input, and even there it is tricky it you have tabs in the 
>> (pasted) data. Notice that
>>
>> scan(text="
>> 1 2 3
>> 4 5 6
>> ")
>>
>> was designed for the purpose of reading inline data.
>>
>>
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Email: pd....@cbs.dk  Priv: pda...@gmail.com

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to