我正在关注一个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
}
}
我希望它能给我一个菜单界面,但是我得到的只是
source('~/Desktop/Quantitative Finance /R data scripts for statistics/QF_Probability_Distribution.R')