【问题标题】:Running R in Scala using rscala causing issues使用 rscala 在 Scala 中运行 R 会导致问题
【发布时间】:2017-09-28 13:39:20
【问题描述】:

我最近遇到了一个名为 rscala 的 R 包。 https://darrenjw.wordpress.com/tag/rscala/

我尝试执行示例,但程序从未完成运行。我不确定可能出了什么问题。每当我尝试实例化 RClient 时,它似乎永远运行。请帮忙。

【问题讨论】:

    标签: r scala


    【解决方案1】:

    对我来说,运行以下代码:

    import breeze.stats.distributions._
    import breeze.linalg._
    import org.ddahl.rscala.RClient
    
    object ScalaToRTest {
    
      def main(args: Array[String]): Unit = {
    
        // first simulate some data consistent with a Poisson regression model
        val x = Uniform(50,60).sample(1000)
        val eta = x map { xi => (xi * 0.1) - 3 }
        val mu = eta map { math.exp }
        val y = mu map { Poisson(_).draw }
    
        // call to R to fit the Poission regression model
        val R = RClient() // initialise an R interpreter
        R.x=x.toArray // send x to R
        R.y=y.toArray // send y to R
        R.eval("mod <- glm(y~x,family=poisson())") // fit the model in R
        // pull the fitted coefficents back into scala
        val beta = DenseVector[Double](R.evalD1("mod$coefficients"))
    
        // print the fitted coefficents
        println(beta)    
      }
    
    }
    

    输出:

    DenseVector(-3.1683714618415855, 0.1031332817387318)
    

    build.sbt

    name := "scalaRdemo"
    
    version := "0.1"
    
    scalaVersion := "2.12.3"
    
    scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")
    
    resolvers ++= Seq(
      "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
      "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
    )
    
    libraryDependencies += "org.scalanlp" %% "breeze-natives" % "0.13.2"
    libraryDependencies += "org.scalanlp" %% "breeze" % "0.13.2"
    libraryDependencies += "org.ddahl" %% "rscala" % "2.3.5"
    

    【讨论】:

    • 谢谢,会试试这个,让你知道
    • 它编译得很好但没有运行。这取决于R版本吗?我有 R 版本 3.1.3。
    • @user8654291 我从这里安装了R和RStudio:cran.r-project.orgR version 3.4.0 (2017-04-21)sudo nano /etc/apt/sources.listdeb https://cloud.r-project.org/bin/linux/ubuntu trusty/sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9sudo apt-get updatesudo apt-get updatesudo apt-get install r-basesudo apt-get install r-base-devsudo apt-get install r-base-devsudo apt-get install r-base-dev987654332@9876不运行什么是异常?
    • 也不例外。它只是没有给出输出。即使我运行它超过一个小时。我有一个旧的 R 版本会尝试这个。
    • 哦,其实我在 scala 2.10 中使用了 rscala jar 2.11。这给我带来了问题。一旦我开始使用 2.10 jar,它对我来说效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2016-09-08
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2018-01-31
    • 2011-02-19
    相关资源
    最近更新 更多