天天看点

如何用 R 语言的 Shiny 库编写 web 程序

这是一个用 shiny 写的简单的小 web 程序:

<code>library(shiny)</code>

<code></code>

<code>server &lt;- function(input, output, session) {</code>

<code>observe({</code>

<code>mytext &lt;- paste("value above is: ", input$textin)</code>

<code>updatetextinput(session, "textout", value=mytext)</code>

<code>})</code>

<code>}</code>

<code>ui &lt;- basicpage(</code>

<code>h3("my very own sample application!"),</code>

<code>textinput("textin", "input goes here, please."),</code>

<code>textinput("textout", "results will be printed in this box")</code>

<code>)</code>

<code>shinyapp(ui = ui, server = server)</code>

当你在输入框中输入文字时,它会被复制到输出框中提示语后。这并没有什么奇特的,但它向你展示了一个 shiny 程序的基本结构。“server”部分允许你处理所有后端工作,如计算、数据库检索或程序需要发生的任何其他操作。“ui”部分定义了接口,它可以根据需要变得简单或复杂。

经验丰富的 r 大牛可能已经知道 shiny 了;它已经存在大约几年了。对于像我这样来自一个完全不同的编程语言,并且希望学习一点 r 的人来说,它是相当有帮助的。

原文发布时间为:2017-03-04

本文来自云栖社区合作伙伴“linux中国”