Is there a way to see a 'live' calculation for the code in RStudio? - r

I have a code that runs a monte carlo simulation between A/B/C, and I modified it so now it will run a MC sim between A/B/C/D. However, the code works fine with A/B/C, but if I add the D category the code just dies. It brings up the UI that it is supposed to but when I click 'compute' (action button) the program stops doing anything. it doesn't close, but it just doesn't do anything.
I am wondering if the issue isn't necessarily the code but what I am asking the code to do. An MCMC with 100k samples, 4 conditions becomes 100000^4 and things get computationally heavy very quickly.
I am hoping for a recommendation that someone may have where I can see what the R code is doing in sort of a 'live stream'. It doesn't return any errors, but I am hoping to be able to see when I add the D category the code will say 'I'm working on it, but it's taking a long time.' But I don't even know where to begin to look for something like that.

Related

Trying to automate an R script that always runs against one dataset and conditionally against another

Very new to R and trying to modify a script to help my end users.
Every week a group of files are produced and my modified script, reaches out to the network, makes the necessary changes and puts the files back, all nice and tidy. However, every quarter, there is a second set of files, that needs the EXACT same transformation completed. My thoughts were to check if the files exist on the network with a file.exists statement and then run through script and then continue with the normal weekly one, but my limited experience can only think of writing it this way (lots of stuff is a couple hundred lines)and I'm sure there's something I can do other than double the size of the program:
if file.exists("quarterly.txt"){
do lots of stuff}
else{
do lots of stuff}
Both starja and lemonlin were correct, my solution was to basically turn my program into a function and just create a program that calls the function with each dataset. I also skipped the 'else' portion of my if statement, which works perfectly (for me).

My looping code in prolog won't work with the rest of my code

I am working on a school assignment where I have to find the smallest hitting set of a list of lists. Right now I am working on finding all the hitting sets before I narrow it down to the smallest one. I implemented a way for my code to find out if two lists have at least one number in common. To my knowledge it works as intended by itself but whenever I try to connect it to the main part of my code it won't work or runs indefinitely. Any help would be appreciated.
Minimal, Reproducible Example:
This code has been made in a way that it will return true anytime a two arrays have at least one number that intersects between the two. I have tested this code and to my knowledge it works as intended. The first result back is always correct when I tested it.
checkForIntersection([],[]):- false.
checkForIntersection([Head|Tail],[Head2|Tail2]):-
Head = Head2;
checkForIntersection(Tail,[Head2|Tail2]);
checkForIntersection([Head|Tail],Tail2).
This part of the code is where I believe an error occurs. I have an AnswerSet as the list that I want to check for intersections. The [Check|NextCheck] is a list of lists and I want to check each of them against the AnswerSet. I loop through it until the [Check|NextCheck] is empty. The issue is that when I call it the results I get is an infinite amount of trues even if the answer should be false.
loopThroughListOfLists(CheckListsAgenstThisList,[]).
loopThroughListOfLists(CheckListsAgenstThisList,[ListToCheck|NextListToCheck]):-
checkForIntersection(ListToCheck,CheckListsAgenstThisList),
loopThroughListOfLists(CheckListsAgenstThisList,NextListToCheck).
loopCheck([3],[[1], [1], [3], [4], [4]]). This is one of the cases I used to test my code with. Instead of returning false it returns an infinite amount of trues whenever I test it.
Thank you so much for reading, I am sorry if this is a really stupid question, I am really struggling with Prolog.

Executing Wolframscript Code in Rstudio Through Terminal

I am interested in using Wolframscript to perform certain operations in R but am a bit new to programming at a level beyond data analytics.
It is rather easy to start a terminal running wolframscript:
rstudioapi::terminalExecute("wolframscript")
will open it in a terminal tab. Indeed, one can also run lines of code via CTRL+ALT+ENTER. My question, then, is how would one attempt to run commands toward the mathematica terminal and retrieve results thereafter?
My main goal is to create some loop to send code and receive output- here's an example of the general idea:
X <- rweibull(100,1.5)
A <- vector(length=100)
for (a in 1:length(A)){
send_code_to_Wolfram(Integrate[(E^(TX[a]u))*(E^(Bu)),{u,0,X[a]}])
A[a]<-Output_from_wolfram }
Where T and B are matrices, send_code_to_Wolfram and Output_from_wolfram are undefined functions. Here, the obvious use of wolframscript would be to utilize its numerical integration and matrix exponentiation capabilities which are mostly unavailable in R.
Would anyone know how this might be possible to implement?
EDIT:
It seems that I can send code via naming the terminal and then using the TerminalSend command, though it does not submit (it merely enters it into the input line)

for Loop, replacement has length zero, Shiny

This is a question that has been answered in context to R, so I should have a similar solution. The problem is, my code works in R but not in Shiny ?
error source
for(i in 1:N)
{
rank_free_choice<- rank_free_choice_fn(signal_agent[i], M, gamma, omega, K,m)
website_choice<- website_choice_fn(rank_data_today,alpha,rank_free_choice)
t1<- ranking_algo_fn(rank_data_today, website_choice, kappa)
rank_data_today<- t1
df_website_choice[i,]<- website_choice
df_rank_data[i,]<- rank_data_today
}
Both matrices are initialized before the loop begins, and rank_data_today was also created before.
The function continues further, and multiple outputs are put together in a list before returning it outside the function.
Curiously I have another app that runs this code similarly, and that works fine!! In that one the initial rank data is passed to df_rank_data[i,] and the updated are passed to df_rank_data[i+1,]
Anybody with a solution? Or perhaps could explain this answer in my context?
I figured it out, and since the problem was so bizarre, I'm posting it here in case anyone else runs into a similar problem.
The reason the code wasn't working was because one of the inputs to the function was missing in Shiny!!!!!
So basically it was a plain and simple typo/carelessness but the error didn't really help.
The Shiny app is just a wrapper around a simulation I wrote in R that used functions, taking inputs from other functions. The error only showed up in the penultimate function [No real way to trace it]
It was working in R because I didn't have to separately input any values as I'd already saved the code.

Get Scilab to calculate without printing result

This sounds like a silly question, but I really can't find an answer around.
I'm using Scilab to evaluate two methods in terms of performace. However, every time I tell Scilab to calculate anything, it will print the results. Since I'm using large matrices, it spends much more time printing the results than doing the calculations, so I'm having a hard time telling how long is each method actually taking.
Can I get Scilab to compute something without printing the result?
That is, instead of
-->B = A'*A
A =
1. 2. 3.
2. 4. 6.
3. 6. 9.
-->
I'd like it to do
-->B = A'*A
-->
Also simply adding a semicolon works
-->B = A'*A;
-->
Well, I finally found the right query. When I searched for 'scilab silent', one of the results (not the first) was this:
http://help.scilab.org/docs/5.3.3/en_US/mode.html
Function mode(k) lets you choose how Scilab will behave in terms of variable display. The following call will temporarily hide results:
mode(-1)
Whereas this will get you back to the default option:
mode(2)
The documentation is confusing, though.
Please notices that mode does not used at prompt, only in an exec-file or a scilab function.
Aside from the awful English, this notice seems to be outdated. This function worked perfectly for me on the prompt.

Resources