yihui / knitr-examples

A collection of knitr examples
488 stars 586 forks source link

Golang example #54

Closed prvst closed 7 years ago

prvst commented 7 years ago

Is there an example on how to use the GO language engine? If not, would it be possible to add one? The language engine itself is already available, I'm just not too sure how to load and run.

Thanks

yihui commented 7 years ago

The go engine was added from https://github.com/yihui/knitr/pull/1330 by @hodgesds. Perhaps we should add a minimal example to this repo.

prvst commented 7 years ago

Just a side note here; I tried configuring the block like this:

{r golang, engine='go', engine.opts='run', engine.path='/usr/local/go/bin/go'}

but I get the following:

go run: no go files listed

It's the same output when I run go run on my terminal without passing a source to execute. I'm not sure but it looks to me that there might be something wrong or missing with the engine configuration

hodgesds commented 7 years ago

Here is an example of using a go package (non executable).

Hello Go!
```{go eval=FALSE}
package hello 

func HelloWorld(){
   println("hello world!")
}```

And for a main (executable) package:

I can run Go!
```{go}
package main 

func main(){
   println("hello world!")
}```

Note that it will automatically call go fmt on the source.

hodgesds commented 7 years ago

One last thought... is go on your $PATH? I'm not sure if the code handles engine.path.

prvst commented 7 years ago

Sorry but that still doesn't work. I created a fresh notebook from Rstudio and copied the snippets above. The blocks are not recognized as 'runable' ( no play button on the right corner). I tried to execute all anyways but nothing happens.

Next I added the following to my file:

library(knitr)
knit_engines$get("go")

The blocks still are not recognized, but now when I execute the file, the block that contains the main function returns the following:

/bin/sh: 1: go: not found

I double checked and my Go installation is on path, I also can confirm that because its my default language for development and I use other tools like Atom.

prvst commented 7 years ago

Also, I had to change the snippet from

{go}

To

{r golang, engine='go'}

to get the error message

hodgesds commented 7 years ago

What happens when you run system("echo $PATH")? The error /bin/sh: 1: go: not found would indicate that go isn't on your $PATH

prvst commented 7 years ago

When I run the command from Rstudio I see this: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

But when I check the $PATH from my terminal I get this: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/bin:/home/felipevl/go/bin

Note how they are different. Am I missing something ?

hodgesds commented 7 years ago

This might be of help, perhaps try starting Rstudio from the terminal if you want to inherit the same path.

touch ~/.Renviron | R_PATH="PATH=$PATH" | echo $R_PATH >  ~/.Renviron
prvst commented 7 years ago

I added my system PATH to the file as described on that link you sent me, and now I started rstudio from terminal. When I run system("echo $PATH") I see the following:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/bin:/home/felipevl/go/bin

Now it seems to be present on the path inside Rstudio as well.

I typed the same block again

{r golang, engine='go'}
package main 

func main(){
   println("hello world!")
}

And now I'm getting the following :

go: unknown subcommand "/tmp/RtmpxYjuJJ/chunk-code31bd34e27ecb." Run 'go help' for usage.

The block is still unrecognized as 'runnable'

hodgesds commented 7 years ago

Can you try running the minimal example in this repo: ./k 118-engine-go.Rmd

prvst commented 7 years ago

I opened Rstudio via command line and checked the path, the Go installation is present.

I downloaded the file and created a new notebook. None of the blocks are recognized as executable and only the last one has the code colored.

I clicked the 'Run All ' command and only the last block produced the following output:

go: unknown subcommand "/tmp/RtmpNJF891/chunk-code220b54e7b85." Run 'go help' for usage.

prvst commented 7 years ago

It looks like that the go compiler is being found and is responding, but the block or the execution unit is not being passed to it.

I changed the last block to this {r engine='go', engine.opts='run'} and the output changed to:

go run: no go files listed

go run is what you run when trying a script like that, its just saying that there is nothing to execute.

hodgesds commented 7 years ago

I can't really help you debug your RStudio setup, please run the minimal example that is in this repo.

> require('knitr')
> knit('~/git/knitr-examples/118-engine-go.Rmd')

processing file: ~/git/knitr-examples/118-engine-go.Rmd
  |...........                                                      |  17%
  ordinary text without R code

  |......................                                           |  33%
label: unnamed-chunk-1 (with options) 
List of 2
 $ eval  : logi FALSE
 $ engine: chr "go"

  |................................                                 |  50%
  ordinary text without R code

  |...........................................                      |  67%
label: unnamed-chunk-2 (with options) 
List of 1
 $ engine: chr "go"

running: go run ./code77451bd44b11.go
  |......................................................           |  83%
  ordinary text without R code

  |.................................................................| 100%
label: unnamed-chunk-3 (with options) 
List of 1
 $ engine: chr "go"

running: go run ./code7745662a0cba.go

output file: 118-engine-go.md

[1] "118-engine-go.md"

The contents of 118-engine-go.md should be as follows:

Hello package!

package hello 

func HelloWorld(){
   println("hello world!")
}

Hello main!

package main 

func main(){
   println("hello world!")
}
## hello world!

Or specify an engine:

package main

func main(){
   println("hello world!")
}
## hello world!
prvst commented 7 years ago

yes I got the same thing.

prvst commented 7 years ago

When running on the terminal I see the hidden go file being created, but I think the same is not true when running on Rstudio. It looks to me that the code should be on the /tmp folder but the same has nothing there. Could this snippet be the problem ? f = tempfile("code", ".", fileext = ".go")

prvst commented 7 years ago

The code you have, when running on Rstudio output this:

go: unknown subcommand "/tmp/RtmprHeIZi/chunk-code2f28c15b829."

See how the path ends with a . instead of an full extension ?

hodgesds commented 7 years ago

interesting.... I'm guessing something here needs updated. If you trying using the knit command for a .Rmd file it should work.

prvst commented 7 years ago

Hey @hodgesds ; Is there any perspective to have this fixed in the near future ? Thanks