天天看點

如何用 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中國”