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

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.

主站蜘蛛池模板: 江口县| 吴江市| 文水县| 桑植县| 镇平县| 大悟县| 海城市| 军事| 吴堡县| 光泽县| 玛沁县| 武穴市| 宜兰市| 阜新| 台南市| 威信县| 灯塔市| 城口县| 西乌| 扬州市| 澄城县| 潞城市| 揭西县| 岚皋县| 黄骅市| 沙洋县| 冕宁县| 兰州市| 和硕县| 健康| 阜南县| 崇左市| 云安县| 普安县| 丰台区| 青龙| 长武县| 加查县| 法库县| 石楼县| 墨玉县|