Customize existing function in R - r

I want to change a condition within the function psych::polychoric in R.
Specifically, I want to increase the limit of different realizations of a a variable from 8 to 10 on line 77 of the code.
I can manually increase the limit by calling
trace(polychoric, edit=TRUE)
Since the script is meant for reproduction purposes for a paper of mine, I want to make handling as smooth as possible by avoiding manual editing.
Is there a way to edit the function by a piece code,
e.g. by replacing if (nvalues > 8) by if (nvalues > 10) in the code by another function?
Any suggestions would be much appreciated.

find the location in the function that you want to change
as.list(body(psych::polychoric))
Change the function
trace(psych::polychoric, quote(nvalues > 10), at=11)
Check to see that you changed what you want to change
trace(psych::polychoric, edit=TRUE)
Set the function back to original
untrace(psych::polychoric)
-----
Seems like fix may be easier for you to implement for this task
fix(polychoric)
opens a pane that you can change the code in - change and hit save.
This will make the function local to your global environment you can check this by looking at the original function trace(polychoric, edit = T) will show nvalues > 10, and trace(psych::polychoric, edit = T) will show nvalues > 8. The next time you reload psych you will be using the original function. Bit of a manual hack - but hopefully works for this one off situation.

Related

RStudio not showing output in Console when using a variable?

Currently learning RStudio but for some reason my Console isn't outputting my code? Not sure if there was a setting I may have toggled but it seems to only affect me when I have a variable in my code?
For example:
when working the ToothGrowth dataset
data("ToothGrowth")
View(ToothGrowth)
This executes and I can view the table of the data in a separate tab.
However, when I try to filter it
data("ToothGrowth")
View(ToothGrowth)
filtered_tg = filter(ToothGrowth, dose=0.5)
View(filtered_tg)
Nothing Returns, a formatted table doesn't open and an empty line is returned in the console window.
This is just one example, even when i try something as simple as
number = 10
number
I would expect, in return;
[1] 10
But console is empty.
Looking for solutions online, I've seen maybe i didn't close a bracket or parenthesis and to include CloseAllConnections() if there was a '+' showing in the console (which there isn't).
Working on rstudio cloud so is there a reset somewhere that I could possibly try?
Thanks for any and all help!
I changed the function as follows:
filtered_tg = filter(ToothGrowth, dose=="0.5")
View(filtered_tg)
After running these commands it was viewed as the table of the data in a separate tab 👇
Output

Is pars() also need to be reset in r & how to check whether my examples and demos need reset?

The rule I want to follow is
reset to user's options(), working directory or par() after you changed it in examples and vignettes and demos
I checked all code in R package documents, there is no function called options() or par()
However, there do have pars()
For example my code
fitness_ts <-
function(pars, features, seasonal,
n = 120, freq = 12, target, nComp, selected.features)
{pars <- pars2list(pars, seasonal, nComp)
I wonder under such situation, do I still need to reset pars()?
Also, anyone know how can I check whether my working directory need to reset or not?
Is that means if I did not use setwd(), I do not need to worry about reset working directory
And if I donot have a demo file, I donot need to worry about reset problem?

Problem Getting Data from bsrch Using Rblpapi

I can use bsrch in excel to get values given of "COMDTY:WEATHER" however when I try it using Rblpapi it shows empty data. Also below I have the Excel formulas so you can see how I am trying to get it to work in R below
Observations Basic Query
=BSRCH("comdty:weather","provider=wsi","location=KNYC","model=ACTUALS","cols=15;rows=354")
Observations Specify Parameters
=BSRCH("comdty:weather","provider=wsi","location=KNYC","model=ACTUALS","fields=WIND_SPEED|TEMPERATURE","cols=3;rows=358")
I tried to look for examples online and only found the below, and this code works.
#this code works
library(Rblpapi)
blpConnect()
head(bsrch("COMDTY:NGFLOW"), 20)
head(bsrch("COMDTY:VESSEL"), 20)
#this is my code and it doesn't work
head(bsrch("COMDTY:WEATHER"), 20)
In order to pass in the overrides, requests need to be modified. In this particular request type, the name and value element of values of Overrides array element needs to be modified. In their source code this is not implemented yet, they have a TODO item below.
// TODO - implement limit and other overrides
Hopefully this will be introduced in future versions. Or you can implement yourself.

R scripting in SPSS Modeler 16: change default "rowCount=1000" for modelerData

When applying R transform Field operation node in SPSS Modeler, for every script, the system will automatically add the following code on the top of my own script to interface with the R Add-on:
while(ibmspsscfdata.HasMoreData()){
modelerDataModel <- ibmspsscfdatamodel.GetDataModel()
modelerData <- ibmspsscfdata.GetData(rowCount=1000,missing=NA,rDate="None",logicalFields=FALSE)
Please note "rowCount=1000". When I process a table with >1000 rows (which is very normal), errors occur.
Looking for a way to change the default setting or any way to help to process table >1000 rows!
I've tried to add this at the beggining of my code and it works just fine:
while(ibmspsscfdata.HasMoreData())
{
modelerData <-rbind(modelerData,ibmspsscfdata.GetData(rowCount=1000,missing=NA,rDate="None",logicalFields=FALSE))
}
Note that you will consume a lot of memory with "big data" and parameters of .GetData() function should be set accordingly to "Read Data Options" in node setting.

Viewing more than 1000 rows in RStudio

In RStudio when you use the View() function, it only allows you to see up to 1000 rows. Is there any way to see more than that. I know it is possible to subset the viewing and see rows 1000-2000 for example, but I would want to be able to see 1-2000. The best I could find was a comment about a year ago saying that it wasn't possible at the time but they were planning on fixing this.
Here's an example (note: I'm guessing you will have to run this in RStudio).
rstudio <- (1:2000)
View(rstudio)
The View command is specifically for the little helper window. You can easily view the full value in the actual console window. If you want the same layout, use cbind.
cbind(rstudio)
which in fact will even give you the same nice row-numbering setup
And if that's too cumbersome
pview <- function(x, rows=100) {
if (length(x) > rows)
print(cbind(x))
else
print(cbind(head(x, rows/2)))
print(cbind(tail(x, rows/2)))
}
pview(rstudio, 1998)
you will need to clean that up to get the row names to lineup
You can change this setting, for instance:
options(max.print=5000)

Resources