Is there a way to make the choices asked for in the package always yes?

Is there a way to make the choices asked for in the package always yes?

when I use

get_sentiments("loughran")

While text mining in kaggle, some packages will ask me yes or no questions.
Since kaggle notebook does not allow interactive input, you will get an error.

Error in menu(choices = c("Yes", "No"), title = title): menu() cannot be used non-interactively

Is there an option setting to always select yes?

1 Like

The best way to go about this is submit a pull request to the textdata package on GitHub. That way, other people won't have the same problem. Also, because my next solution is what's called "monkey patching," and that's not an endearing term. It's unreliable, so don't use it for code you'll keep around.

Here's the code to include, with an explanation afterwards:

library(tidytext)

tns <- getNamespace("textdata")
assignInNamespace(x = "printer", value = function(...) 1, ns = tns)
get_sentiments("loughran")

First, thank you for sharing the error message. It let me know to look for a call to menu() in the code. You didn't say which package get_sentiments came from, but I found it using RSiteSearch:

RSiteSearch("get_sentiment", restrict = "functions")

It's from the tidytext package. So I looked up the get_sentiments function there, and it's a basic wrapper around functions from textdata. So then I continued tracing the calls in textdata until it led to a call to menu in the printer function. This function's only purpose is to ask whether the user wants to download the file.

If you make a pull request, give the load_dataset function a parameter users can set to avoid any prompts. It'd need to be passed from the different lexicon_* functions.

Anyway, because printer just prints a message and then asks for a yes/no with menu, it's "safe" to monkey patch (which is never really safe). So I replaced the printer function in the textdata namespace with a function that accepts any arguments and always returns 1 (because "Yes" was the 1st option in the menu call).

A namespace is a special kind of environment. A package's namespace bundles up all the functions and data it uses. This means, even if you create a new function named printer in the global environment, textdata will keep using the printer function from its own namespace. Normally, this is good. It would suck to break a package by creating a variable that has the same name as an undocumented function. But R is very dynamic when needed, which is also good. By using the "forbidden" functions getNamespace and assignInNamespace, we can replace objects in a namespace. Again, this is a hack. The documentation for these functions says they're internal and not promised to work forever.

And again, the best option is to make a pull request to the textdata package.

1 Like

@nwerth
Thanks for the detailed explanation.
It was helpful that you even reported the steps to find out the cause.
By learning the path you followed, I too can solve the problem alone or help others.

I will try to make a pull request for the package.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.