如何缩短运行时间?

huangapple go评论106阅读模式
英文:

How do I get a shorter Runtime?

问题

num <- -18.7
guess <- -1

print("你能猜到英国每1000名居民的日常剂量吗?")

while (guess != num) {
    guess <- readline(prompt = "输入整数:")
    if (guess == num)
        cat(num, "是正确的")
    if (guess < num)
        cat("它更大")
    if (guess > num)
        cat("它更小")
}
英文:
num&lt;-18.7
guess&lt;- -1

print(&quot;Can you guess the daily dose per 1000 inhabititants in the UK?&quot;)

while(guess !=num) {
    guess&lt;-readline(prompt = &quot;Enter integer:&quot;)
    if (guess== num)
        cat(num, &quot;is correct&quot;)
    if (guess&lt;num)
        cat(&quot;it is bigger&quot;)
    if (guess&gt;num)
        cat(&quot;It is smaller&quot;)
}

It works when I play it through r script but when I knit it into markdown it doesn't get an error or anything but its been running for about 30 minutes and still hasn't finished. Is there a way to change this?

答案1

得分: 1

以下是翻译好的部分:

正如评论中所提到的,R Markdown 文档中的 R 代码在编织(knitted)时执行,而不是在查看时执行。谈到 R 的交互性时,通常会考虑到 Shiny。对于某些情况,也可以直接将 Shiny 与 R Markdown 结合使用,详见 https://bookdown.org/yihui/rmarkdown/shiny-documents.html。将您的脚本直接转换为如下形式:

---
runtime: shiny
output: html_document
---

# 你能猜到每千名英国居民的日常剂量吗?

```{r setup, include=FALSE}
num &lt;- 18.7
numericInput(&quot;guess&quot;, label = &quot;输入数字:&quot;, value = 0)

renderText({
    if (input$guess == num)
        paste(num, &quot;正确&quot;)
    else if (input$guess&lt;num)
        &quot;数字较大&quot;
    else if (input$guess&gt;num)
        &quot;数字较小&quot;
})

这样的文档可以在本地运行,也可以进行部署。顺便说一句,对于比较浮点数,== 不是最佳选择,最好使用 isTRUE(all.equal())

&gt; 0.3 == 0.1 + 0.1 + 0.1
[1] FALSE
&gt; isTRUE(all.equal(0.3, 0.1 + 0.1 + 0.1))
[1] TRUE
英文:

As mentioned in the comments, the R code in a R Markdown document is executed when it is knitted, not when it is viewed. When talking about interactivity for R, one usually thinks about Shiny. For some cases it is also possible to combine Shiny directly with R Markdown, c.f. https://bookdown.org/yihui/rmarkdown/shiny-documents.html. A straight forward conversion of your script gives:

---
runtime: shiny
output: html_document
---

# Can you guess the daily dose per 1000 inhabititants in the UK?

```{r setup, include=FALSE}
num &lt;- 18.7
```


```{r guessing-game, echo=FALSE}
numericInput(&quot;guess&quot;, label = &quot;Enter number:&quot;, value = 0)

renderText({
    if (input$guess == num)
        paste(num, &quot;is correct&quot;)
    else if (input$guess&lt;num)
        &quot;it is bigger&quot;
    else if (input$guess&gt;num)
        &quot;It is smaller&quot;
})
```

Such a document can either be run locally or deployed. BTW, == is not the best choice for comparing floating point numbers. It is better to use isTRUE(all.equal()):

&gt; 0.3 == 0.1 + 0.1 + 0.1
[1] FALSE
&gt; isTRUE(all.equal(0.3, 0.1 + 0.1 + 0.1))
[1] TRUE

huangapple
  • 本文由 发表于 2020年1月7日 00:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定