楼主: davis987
6128 3

[问答] req()函数怎么用? [推广有奖]

  • 0关注
  • 0粉丝

初中生

47%

还不是VIP/贵宾

-

威望
0
论坛币
1 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
132 点
帖子
8
精华
0
在线时间
14 小时
注册时间
2016-7-26
最后登录
2018-7-30

楼主
davis987 发表于 2016-8-18 11:06:18 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
官方文档是这么写的,但还是不太明白是什么意思?哪位给翻译一下,或是给举下应用的例子,谢谢!

req {shiny}        R Documentation
Check for required values

Description

Ensure that values are available ("truthy"–see Details) before proceeding with a calculation or action. If any of the given values is not truthy, the operation is stopped by raising a "silent" exception (not logged by Shiny, nor displayed in the Shiny app's UI).

Usage

req(...)
Arguments

...       
Values to check for truthiness.
Details

The req function was designed to be used in one of two ways. The first is to call it like a statement (ignoring its return value) before attempting operations using the required values:

rv <- reactiveValues(state = FALSE)
r <- reactive({
  req(input$a, input$b, rv$state)
  # Code that uses input$a, input$b, and/or rv$state...
})
In this example, if r() is called and any of input$a, input$b, and rv$state are NULL, FALSE, "", etc., then the req call will trigger an error that propagates all the way up to whatever render block or observer is executing.

The second is to use it to wrap an expression that must be truthy:

output$plot <- renderPlot({
  if (req(input$plotType) == "histogram") {
    hist(dataset())
  } else if (input$plotType == "scatter") {
    qplot(dataset(), aes(x = x, y = y))
  }
})
In this example, req(input$plotType) first checks that input$plotType is truthy, and if so, returns it. This is a convenient way to check for a value "inline" with its first use.

Truthy and falsy values

The terms "truthy" and "falsy" generally indicate whether a value, when coerced to a logical, is TRUE or FALSE. We use the term a little loosely here; our usage tries to match the intuitive notions of "Is this value missing or available?", or "Has the user provided an answer?", or in the case of action buttons, "Has the button been clicked?".

For example, a textInput that has not been filled out by the user has a value of "", so that is considered a falsy value.

To be precise, req considers a value truthy unless it is one of:

FALSE

NULL

""

An empty atomic vector

An atomic vector that contains only missing values

A logical vector that contains all FALSE or missing values

An object of class "try-error"

A value that represents an unclicked actionButton

Note in particular that the value 0 is considered truthy, even though as.logical(0) is FALSE.

If the built-in rules for truthiness do not match your requirements, you can always work around them. Since FALSE is falsy, you can simply provide the results of your own checks to req:

req(input$a != 0)

Value

The first value that was passed in.

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:REQ Description Calculation Operations operation available operation required raising stopped

沙发
冰川断层 发表于 2016-8-20 11:30:46
> rep(1:4,2)
[1] 1 2 3 4 1 2 3 4
> rep(1:4,each=2)
[1] 1 1 2 2 3 3 4 4
> rep(1:4,c(1,2,2,3))
[1] 1 2 2 3 3 4 4 4

藤椅
davis987 发表于 2016-8-22 16:23:37
冰川断层 发表于 2016-8-20 11:30
> rep(1:4,2)
[1] 1 2 3 4 1 2 3 4
> rep(1:4,each=2)
看好了,是req,不是rep哦!

板凳
冰川断层 发表于 2016-8-25 15:07:56
davis987 发表于 2016-8-22 16:23
看好了,是req,不是rep哦!
不好意思,没有看清楚你的问题,这个我就不会啦。。。。。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 13:25