官术网_书友最值得收藏!

The server.R of the minimal example

Let's now look at server.R, where it should all become clear. Look at the following code:

function(input, output) { # server is define    d here output$textDisplay = renderText({ # assign to output$textDisplay

paste0("You said '", input$comment, # from text input
"'.There are ", nchar(input$comment),
" characters in this.")
})
}

We define the reactive components of the application within function(input, output) {...}. On the whole, two types of things go in here. Reactive objects (for example, data) are defined, which are then passed around as needed (for example, to different output instructions), and outputs are defined, such as graphs. This simple example contains only the latter. We'll see an example of the first type in the next example.

An output element is defined next with output$textDsiplay = renderText({..}). This instruction does two basic things. First, it gives the output a name (textDisplay) so that it can be referenced in ui.R (you can see it in the last part of ui.R). Second, it tells Shiny that the content contained within is reactive (that is, it will be updated when its inputs change) and it takes the form of text. We will cover advanced concepts in reactive programming with Shiny in a later chapter. There are many excellent illustrations of reactive programming at the Shiny tutorial pages, available at rstudio.github.io/shiny/tutorial/#reactivity-overview.

The actual processing is very simple in this example. Inputs are read from ui.R by the use of input$..., so the element named in ui.R as comment (go and have a look at ui.R now to find it) is referenced with input$comment.

The whole command uses paste0() to link strings with no spaces (equivalent to paste(..., sep = "")), picks up the text the user inputted with input$comment, and prints it, along with the number of characters within it (nchar()) and some explanatory text.

That's it! Your first Shiny application is ready. The full code can be found here, https://gist.github.com/ChrisBeeley/4202605cf2e64b4f609e. Using these very simple building blocks, you can actually make some really useful and engaging applications.

主站蜘蛛池模板: 乌拉特后旗| 墨江| 大港区| 平顺县| 铅山县| 息烽县| 鹿邑县| 大田县| 栖霞市| 镶黄旗| 木里| 米易县| 张家口市| 勃利县| 二手房| 龙里县| 滨州市| 民乐县| 淮安市| 眉山市| 缙云县| 从江县| 平凉市| 辽阳市| 武定县| 大庆市| 屯昌县| 晋城| 定南县| 马龙县| 临漳县| 霍山县| 肃北| 航空| 海安县| 石景山区| 教育| 策勒县| 江达县| 娱乐| 鄯善县|