我试图从用户提示读取一个整数。但它正在阅读作为角色。如何将其读作整数?
myfunt <- function(){
cat("Enter an integer or whole number : \n")
enter <- readline(prompt = "")
cat("You sumitted : \n"); str(enter)
}
运行此功能会产生: myfunt()
Enter an integer or whole number:
17
You sumitted :
chr "17"
任何解决方案?
答案 0 :(得分:1)
myfunt <- function(){
cat("Enter an integer or whole number : \n")
enter <- as.integer(readline(prompt = ""))
cat("You sumitted : \n"); str(enter)
}
并运行你的函数.. myfunt()
Enter an integer or whole number:
17
You sumitted :
int 17
答案 1 :(得分:0)
通常,“ readline”功能会将数据转换为字符值。
为了将这些字符转换为数值,可以在函数中使用以下代码,
如果只有一个数字可以转换为数字值,
num <- readline("Enter the sequence of numbers separated by comma : ")
num <- strsplit(num,',')
num <- as.numeric(unlist(num))
如果要将多个数字转换为数字值,
import {ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy} from '@angular/router';
export class SystemRouteReuseStrategy implements RouteReuseStrategy {
handlers: {[key: string]: DetachedRouteHandle} = {};
shouldDetach(route: ActivatedRouteSnapshot): boolean {
if (route.data.reuse) {
return true;
}
return false;
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
this.handlers[route.routeConfig.path] = handle;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
return this.handlers[route.routeConfig.path];
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return future.routeConfig === curr.routeConfig;
}
}