我试图理解下面的代码res1
之间的区别:res2
#include <iostream>
int main()
{
int x = 1;
int y = 0;
double res1 = double(x)/y; // OK: evaluates to Inf
int res2 = x/y; // run-time error: Floating point exception
// 1/0; // g++ warning: division by zero [-Wdivision-by-zero]
std::cout << res1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
据我了解,除以零是 C++ 标准中未定义的行为,并且res1
和之间存在差异的原因res2
是由于我的机器为 实现了 IEEE 754 double
,它需要除以零才能返回Inf
或-Inf
。
但现在我想知道为什么标准必须首先对除以零做出任何声明。这个答案说它是为了适应实现 C++ 的各种不同的体系结构,但我不确定 -除以零不是更多的运行时问题吗?特别是如果编译器在大多数情况下不太可能在不评估分母的情况下检测到它(我认为这就是上面示例中发生的情况)。当然,如果我尝试类似 的操作 …
在清理列表上使用 rbindlist 之前,我尝试用 map() 内的 NA 替换下面列表的 NULL 元素:
m = list(min = list(id = "min", val = NULL),
max = list(id = "max", val = 7),
split = list(id = "split", val = "gini"))
str(m)
List of 3
$ min :List of 2
..$ id : chr "min"
..$ val: NULL
$ max :List of 2
..$ id : chr "max"
..$ val: num 7
$ split:List of 2
..$ id : chr "split"
..$ val: chr "gini"
Run Code Online (Sandbox Code Playgroud)
我已经尝试过: …
我正在尝试安装 的开发版本tidyr
。当我尝试devtools::install_github("tidyverse/tidyr")
或 时remotes::install_github("tidyverse/tidyr")
,出现以下错误:
> devtools::install_github("tidyverse/tidyr")\nDownloading GitHub repo tidyverse/tidyr@master\nDownloading GitHub repo r-lib/vctrs@master\nThese packages have more recent versions available.\nWhich would you like to update?\n\n1: All\n2: CRAN packages only\n3: None\n4: rlang (0.3.4 -> 18856f22a...) [GitHub]\n5: ellipsis (0.1.0 -> 9908b24a2...) [GitHub]\n\nEnter one or more numbers separated by spaces, or an empty line to cancel\n1: 1\nrlang (0.3.4 -> 18856f22a...) [GitHub]\nellipsis (0.1.0 -> 9908b24a2...) [GitHub]\nDownloading GitHub repo r-lib/rlang@master\n\xe2\x88\x9a checking for file \'C:\\Users\\owner\\AppData\\Local\\Temp\\RtmpYDrYNh\\remotes96bc1c295445\\r-lib-rlang-18856f2/DESCRIPTION\' ...\n- preparing \'rlang\': (2s)\n\xe2\x88\x9a checking DESCRIPTION meta-information …
Run Code Online (Sandbox Code Playgroud) 下面的应用程序有两个相互依赖的 numericInputsa
和b
。input$a
的值为,的1-input$b
值为。每当用户更改输入的值时,我想相应地更新另一个输入的值。下面的代码包含两种方法:input$b
1-input$a
b
更新 b a
。当我尝试 1) 时,输入陷入无限循环,但我不确定我是否理解原因。如果用户发生更改b
,则会触发观察者并将 的值a
更新为1-b
。由于两个输入的值现在都是最新的,因此不会对input$a
或进行进一步更改input$b
,并且在用户进行另一次更改之前不应再次触发观察者。但事实并非如此。
方法 1) 与方法 2) 有何不同?为什么 1) 会陷入循环而 2) 却不会?
library(shiny)
shinyApp(
ui = fluidPage(
numericInput('a','a', value = 0.5, max = 1, min = 0),
numericInput('b','b', value = 0.5, max = 1, min = 0)
),
server = function(input, output, session) { …
Run Code Online (Sandbox Code Playgroud) 下面的应用程序包含一个模块,每次Add
单击按钮时都会插入一个 UI 对象。UI 对象包含两个输入:
selectInput
带有选项A
和 的B
。textInput
如果用户选择,则输入 2 为 a ;如果用户选择 ,则输入A
2 为 a
。numericInput
B
但是,当我单击 时Add
,插入的 UI 仅包含输入 1(selectInput
) - 输入 2 未渲染,如下所示:
而所需的输出如下所示:
我不确定这是否是命名空间问题,或者模块的范围是否存在问题。将 ID 打印到控制台检查:
该应用程序如下:
library(shiny)
# module UI function
modUI <- function(id){
ns <- NS(id)
tagList(
actionButton(ns('add'), 'Add'),
div(id = ns('placeholder'))
)
}
# module server function
modServer <- function(input, output, session) {
ns = session$ns …
Run Code Online (Sandbox Code Playgroud) 我有一个小标题:
df = tibble(one = list('a', 'b'), two = list(c('p1', 'p2', 'p3'), NA_character_), three = list(NA_character_, c('z1', 'z2', 'z3')))
df
# A tibble: 2 x 3
one two three
<chr> <list> <list>
1 a <chr [3]> <chr [1]>
2 b <chr [1]> <chr [3]>
Run Code Online (Sandbox Code Playgroud)
我想替换丢失的值的列two
和three
与列的值one
使用coalesce()
,然后在折叠每个字符向量(横行)two
和three
成使用单个串toString()
。我的预期输出如下所示:
df = tibble(one = c('a', 'b'), two = list('p1, p2, p3', 'b'), three = list('a', 'z1, z2, z3'))
df
# A …
Run Code Online (Sandbox Code Playgroud) 我读到,实现文件包含相应的标头是一种很好的做法,因为它允许在编译时而不是链接时捕获某些类型错误。然而,在搜索此类错误的示例时,我最终得到了以下代码,它产生了我不太理解的结果:
a.h
:
char returnString(void);
int returnInt(void);
Run Code Online (Sandbox Code Playgroud)
a.cpp
:
// #include "a.h"
#include <string>
std::string returnString(void)
{
return "Hi";
}
void returnInt(void)
{
// return 1;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp
:
#include "a.h"
#include <iostream>
int main(void)
{
std::cout << returnString() << '\n';
std::cout << returnInt() << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
中的声明与 中的a.h
定义具有不同的返回类型a.cpp
。如果包含a.h
在其相应的实现文件a.cpp
. 但是,如果我注释掉包含内容(如上所示),然后尝试使用 进行编译和链接g++ main.cpp a.cpp
,则会出现以下链接时错误:
/usr/bin/ld: /tmp/ccgWiADF.o: in function 'main':
main.cpp:(.text+0x2c): undefined reference to 'returnString()'
collect2: error: ld returned 1 …
Run Code Online (Sandbox Code Playgroud) 我有以下字符串:
x = "marchTextIWantToDisplayWithSpacesmarch"
Run Code Online (Sandbox Code Playgroud)
我想删除字符串开头的“ march”部分,然后在其余的每个大写字母之前添加一个空格,以产生以下结果:
"Text I Want To Display With Spacesmarch"
Run Code Online (Sandbox Code Playgroud)
要插入whitepace,我使用了gsub("([a-z]?)([A-Z])", "\\1 \\2", x, perl= T)
但不知道如何修改模式,以便从返回的字符串中排除第一个“ march”。我正在努力做得更好,因此任何帮助将不胜感激。
我的应用程序有两个页面:Step1
和Step2
. Step1
有一个复选框,如果选中它会阻止导航,还有一个Step2
单击时导航到的下一步按钮。Step2
有一个“上一个”按钮,Step1
单击时会导航回。
根据本教程,如果选中复选框,我将使用对象的block
方法createBrowserHistory
来阻止路由更改Step1
:
const unblock = useRef();
useEffect(() => {
unblock.current = history.block((tx) => {
if (block.current.checked) {
const promptMessage = "Are you sure you want to leave?";
if (window.confirm(promptMessage)) {
unblock.current();
tx.retry();
}
} else {
console.log("tfgx");
unblock.current();
tx.retry();
}
});
}, []);
Run Code Online (Sandbox Code Playgroud)
我还必须将低级<Router>
(not <BrowserRouter>
) 中的 history 道具设置为createBrowserHistory
对象,如下所示:
<Router history={createBrowserHistory()}>
...
</Router>
Run Code Online (Sandbox Code Playgroud)
但这会阻止路由被正确呈现。我认为这可能与 …