rstudio控制台和脚本区的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rstudio控制台和脚本区的区别相关的知识,希望对你有一定的参考价值。

参考技术A 1.安装R:自行百度☺ 2.R控制台(R Console)和R程序脚本: 打开R软件,就会直接打开控制台,控制台可以显示程序运行的结果.错误提示...
术之多
参考技术B R控制台与R脚本环境的区别;(Differences of R console vs. R script environment; error with installed.packages())
我是R的新手,我花了一段时间在网上加入示例脚本,我遇到了一个有一堆require(<package>)行的脚本。 我没有编写install.packages(<package>) ,而是修改了脚本:
package_names <- c('caret',
'readr',
'xgboost',
'ggplot2',
'R.utils',
'gridExtra',
'lubridate',
'data.table',
'Matrix',
'plyr',
'Hmisc',
'maps',
'maptools',
'sp',
'corrplot')
for (package_name in package_names)
if (!package_name %in% rownames(installed.packages()))
install.packages(package_name)

eval(parse(text=sprintf("require(%s)",package_name)))


因此,在需要之前,它会尝试安装包,如果它没有安装。 但是,在R studio中将其作为脚本执行会导致以下错误:
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: <Anonymous> ... withVisible -> eval -> eval -> install.packages -> contrib.url

我没有明确地调用contrib.url所以我真的不知道从哪里开始。
但后来我尝试复制并粘贴那些确切的行并在命令行R Studio解释器会话(repl)中运行它们,然后运行并安装/更新所有这些包完美无缺。
这让我想到了一个问题:命令行会话和导致此错误的脚本之间有什么区别?

I am new to R, and I've spent a while getting ramped up on example scripts on the web, I came across a script that had a bunch of require(<package>) lines. Rather than writing install.packages(<package>), I modified the script as such:
package_names <- c('caret',
'readr',
'xgboost',
'ggplot2',
'R.utils',
'gridExtra',
'lubridate',
'data.table',
'Matrix',
'plyr',
'Hmisc',
'maps',
'maptools',
'sp',
'corrplot')
for (package_name in package_names)
if (!package_name %in% rownames(installed.packages()))
install.packages(package_name)

eval(parse(text=sprintf("require(%s)",package_name)))


So that it would attempt to install the package if it wasn't installed, before requiring it. However executing this as a script in R studio resulting in the following error:
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: <Anonymous> ... withVisible -> eval -> eval -> install.packages -> contrib.url

I am not explicitly calling contrib.url so I didn't really know where to begin.
But then I tried copying and pasting those exact lines and running them in a command line R Studio interpreter session (repl), and voila, it ran and installed/updated all those packages flawlessly.
This brings me to the question: What's the difference between the command line session and the script that caused this error?

原文:https://stackoverflow.com/questions/32427315
2022-07-10 08:07
满意答案
在getCRANmirror()返回的选项中,在脚本中设置CRAN镜像,例如,
chooseCRANmirror(ind=1)

正如@KonradRudolph所建议的那样,一种更惯用的方法可能是安装任何缺少的需求,然后是require()所有包。
chooseCRANmirror(ind=1)
needed = package_names[!package_names %in% rownames(installed.packages())]
install.packages(needed)
ok = sapply(package_names, require, character.only=TRUE)
if (!all(ok))
bad = paste(package_names[!ok], collapse=", ")
stop("failed to 'require' packages: ", bad)


Set the CRAN mirror in your script, from amongst the options returned by getCRANmirror(), e.g.,
chooseCRANmirror(ind=1)

As suggested by @KonradRudolph, a more idiomatic way might be to install any missing requirements and then to require() all packages.
chooseCRANmirror(ind=1)
needed = package_names[!package_names %in% rownames(installed.packages())]
install.packages(needed)
ok = sapply(package_names, require, character.only=TRUE)
if (!all(ok))
bad = paste(package_names[!ok], collapse=", ")
stop("failed to 'require' packages: ", bad)

Rstudio怎么生成html文件 Rstudio生成html报告方法介绍

参考技术A 1、先在R中建一份R-Markdown(.md)文件,可直接在其中写Markdown脚本。

2、可以通过以下方式插入R脚本,并可以通过调参,控制R程序的输出包括表和图的各种属性控制。

3、最后通过Knitr来运行这份.md文件可直接生产一份html文档,也可通过latex进一步产生一份pdf报告。

以上是关于rstudio控制台和脚本区的区别的主要内容,如果未能解决你的问题,请参考以下文章

R.003 Rstudio使用

脚本从 R 运行,但不是通过命令行

在 R 和 RStudio 中清除控制台的功能

R:在新的 Rstudio 预览版中安装 matplotlib

Rstudio怎么生成html文件 Rstudio生成html报告方法介绍

有没有办法以颜色将文本输出到 R 控制台