Я следил за видео на Youtube, в котором рассказывалось о том, как создать интерактивное меню в R. Я сделал некоторые изменения из оригинального видео, но ничего радикального, что могло бы создать или сломать код (по крайней мере, я так думаю).
Видео на YouTube, которое я смотрел:
https://www.youtube.com/watch?v=uaPBpJArh0M
Это мой код:
inputNumber <- function(prompt) {
# Usage: num = inputNumber(prompt)
#
# Displays prompt and asks for a number.
# Repeats until user inputs a valid number.
while(TRUE) {
num = suppressWarnings(as.numeric(readline(prompt)))
if(!is.na(num)) {
break
}
}
}
displayMenu <- function(options) {
# Usage: choice = displayMenu(options)
#
# Displays a menu of options,
# ask the user to choose an item,
# and return the number of the menu item chosen.
#
# Input options Menu options (cell array of strings)
# Output choice Chosen option (integer)
# Display menu options
for (i in 1:length(options)) {
cat(sprintf("%d. %s\n", i, options[i]))
}
# Gets a valid menu choice
choice <- 0
while (!any(choice == 1:length(options))) {
choice = inputNumber("Please choose a menu item: ")
}
return(choice)
}
# Define menu options
menuItems <- c("Binomial", "Poisson", "Geometric", "Negative Binomial", "Hypergeometric", "Uniform", "Normal", "Exponential")
while(TRUE) {
# Displays the menu
choice <- displayMenu(menuItems)
if(choice == 1) {
print("Good choice!")
break
} else {
print("Bad choice.")
break
}
}
Я ожидал, что оно даст мне интерфейс менюно вместо этого я получил только 1011
source('~/Desktop/Quantitative Finance /R data scripts for statistics/QF_Probability_Distribution.R')