How to create a loop with R function specgram(signal) - r

I am working with many signals; each one in a time series but is too many and, I need to make more than 1000 but, I am not sure how to implement it because I not only need the plots but the values of output for each spectrogram stored in a file or an R object. I am sorry I don't have an approach. Can anyone help out, please?

Related

(r) turning data (DNAbin) into a matrix

I am trying to run stamppFst() and stamppConvert() with haplotype data. The data I have is a squence of nucleotides in a DNAbin. I have tried to find ways to turn it into a matrix but what I have read goes way over my head since this is the first time I have ever used R.
data
This is an example of one of the data sets I want to use.
I apologize if this is a very basic question. Thanks for any help!

Updating existing plot instead of creating new in a for loop

I am attempting a homework problem where I am tasked to plot the histogram that results from a Galton board experiment, essentially creating normal distribution by adding one value at at a time and updating the histogram after each trial (ball). I would like to find a way to update the histogram after each addition of a new value to the distribution; instead of that my code currently makes a whole ton of plots.
So far I've set up a vector with length=1000 (though theoretically I should be able to apply my final code to a vector of anything length?) and created a loop to add values to it using rbinom() with 200 "pegs" with a probability of 50% (falling left or right).
x<-numeric(1000) #create vector length of 1000 values of 0
for (i in 1:1000) {
x[i]<-sum(rbinom(200,1,0.5))
hist(x,freq=FALSE)
}
I have the hist() call within the for loop (this may be a cardinal sin in R...), which as you can imagine produces 1000 graphs! Definitely not the right way to go about this. Is there any way to just essentially update on top of the previous plot? I'm thinking of things like abline(), lines(), etc, which (as far as I can tell) just add lines on top of an already existing plot in R without creating a new one. This is probably because the data associated with those functions isn't the same as the data in a vector? Anyways, I haven't been able to figure this out wth google. I haven't tried using ggplot2 or the animate packages yet, though I'm only vaguely familiar with the former and I imagine there's a learning curve.
A final note: I'm fairly new to R, so I'd appreciate unrelated advice on the above code, but I also think it's very productive to work things out on your own, so I would prefer hints and/or general advice instead of pasting working code.
Thank you very much in advance for your help!

How do I display variable number of ggplots in R shiny, depending on input?

this is my first question on StackOverflow. I’ve tried to make it as clear as possible, but I am also very open to feedback!
I am creating an app with R shiny to analyze two dimensional data (Time and Value) for multiple samples.
I would like the application to:
Import the sample files.
Recognize the number of samples in the uploaded files.
Create a selectInput bar for each sample.
Create a ggplot object for each sample.
Huge thank you to Pork Chop for pointing out the similarities to this question - that solved my multiple selectInput bar issue. Also thank you to camille for suggesting purr's map function, that helps me create a list of ggplot objects without fuss.
However, I am still struggling to get all of the ggplot objects to display in Shiny. I have used this approach for inspiration but the author uses a for loop with static length. I tried their approach, just to see if it works, but it also only gives me the first plot of my list of plots.
Here is a very basic example of my current approach. Maybe something with map/lapply with renderPlot? i.e. map(plot_list, renderPlot({})) ?
Sincerest thanks again for your help and patience.
EDIT: finally managed to solve my issue with a lot of help from this post! Instead of using max_plots I created a reactive value for number of samples, and was able to get the plots to display properly once I added observe({}).
As described in the edit I made, this post was extremely helpful. Wrapping the actual creation of the plots in observe({}) and creating a reactive element for number of imported samples was crucial.
Pork Chop's reference to this post solved my issue with multiple dynamic inputs.

R loop from a txt or csv file

I am learning to use R and I am working with the for loop
Here is an example:
for (loopvalues in c(1,5,8,10,19)){
print(paste("The number is", loopvalues))
}
I was wondering what can be done if the list of values is as big as 100 or 1000 different values and they follow no patterns.
I imagined I can have the values saved in a csv or a txt file beforehand, but how could I tell the loop command to read the values from that file?
I am sure the question is very basic, so I thank you beforehand for your help!
For loops can be used for extremely long lists however you will often find that they become slow and you will want to use other commands such as the apply family.
You do not have to name all the values in the loop. One way to accomplish this is using the in. Here is an example using the mtcars data set that is preloaded into R.
for(c in unique(mtcars$carb)){
print(c)
}
By using the unique function, I don't even have to know what all the possible values of mtcars$carb are but I can still loop through them.
Additionally, you probably want to practice your googling skills instead of asking StackOverflow. Most of the questions you're going to ask when learning R are already out there.

Can I use genetic distances in form of a .csv file to create a tree in R using ape ?

It is my first time using R for phylogenetics work and I was wondering if I could do that. It seems a rather trivial job and I think there must be a very small code for this, but I am unable to get it done. Any help appreciated!

Resources