I'm trying to create the Spectra object (.RData), just like suggested on ChemoSpec.pdf (see the code below), and I get an error message like this:
"could not find getManyCsv function"
How can I fix it? Any suggestion is appreciated. I have R version 3.1.0 and ChemoSpec version 2.0-2.
Here is the code:
getManyCsv(gr.crit = c("ICLPS", "PEPS"), gr.cols = c("red3", "dodgerblue4"),freq.unit = "ppm", int.unit = "peak intensity", descrip = "PS Study",out.file = "PS")
Cheers,
Awel
I had the same problem and found on:
http://cran.r-project.org/web/packages/ChemoSpec/NEWS
following:
##### Version 2.0.0
Date: 2013-12-07
>> normSpectra modified to avoid integer overflow.
>> Modified plotScree & plotScree2 so that data sets with less than 10 components plot.
>> Freshened up the README.md file.
>> To facilitate using expressions in plot titles, argument 'title' has been removed from all functions that use base or lattice graphics and now one should use 'main'. The 'title' argument remains for the rgl graphics functions. THIS WILL BREAK EARLIER CODE, hence the move to a new major version.
>> Semantic versioning is now in effect. See semver.org for details.
>> getManyCsv has been removed (deprecated for one year now). Use files2SpectraObject instead.
So it might be necessary to use files2SpectraObject instead of getManyCsv.
Related
I am currently learning Julia and have been practicing in the Jupyter notebook environment. However, the ProgressBar package (similar to TQDM in python) is updated every new line instead of updating on the same line (picture attached). Is there any way to fix this? Thanks.
UPDATE : Here is the full function that I wrote.
function spike_rate(raw_dat, width)
N = size(raw_dat)[1]
domain = collect(1:N);
spike_rat = zeros(N);
for i in ProgressBar(1:N)
dx = i .- domain;
window = gaussian.(dx, width);
spike_rat[i] = sum(window .* raw_dat) ./ width;
end
return spike_rat;
end
This seems to be a known issue with ProgressBars.jl, unfortunately. It's not clear what changed to make these progress bars not work properly anymore, but the maintainer's comment says that tqdm uses "a custom ipywidget" to make this work for the Python library, and that hasn't been implemented for the Julia package yet.
To expand on #Zitzero's mention of ] up, that calls Pkg.update() which also prints a progress bar - so the suggestion is to use the mechanism Pkg uses for it. Pkg has an internal module called MiniProgressBars which handles this output.
Edit: Tim Holy's ProgressMeter package seems well-maintained, and is a much better option than relying on an internal non-exported Pkg submodule with no docs. So I'd recommend ProgressMeter over the below.
The Readme mentions a caveat regarding printing additional information with the progress bar when in Jupyter, which likely applies to MiniProgressBar as well. So, using ProgressMeter, and separating the progress output vs other relevant output to different cells, seems like the best option.
(not recommended)
using Pkg.MiniProgressBars
bar = MiniProgressBar(; indent=2, header = "Progress", color = Base.info_color(),
percentage=false, always_reprint=true)
bar.max = 100
# start_progress(stdout, bar)
for i in 1:100
sleep(0.05) # replace this with your code
# print_progress_bottom(stdout)
bar.current = i
show_progress(stdout, bar)
end
# end_progress(stdout, bar)
This is based on how Pkg uses it, from this file. The commented out lines (with start_progress, print_progress_bottom, and end_progress) are in the original usage in Pkg, but it's not clear what they do and here they just seem to mess up the output - maybe I'm using them wrongly, or maybe Jupyter notebooks only support a subset of the ANSI codes that MiniProgressBars uses.
There is a way, the package module does that as far as I know when you do:
] up
Could you share a little of your code?
For example, where you define what is written to the console.
One guess is that
print("text and progressbar")
instead of
println("text and progressbar")
could help, because println() always creates a new line, while print() should just overwrite you current line.
I'm actually evaluate different solution to enhance/explore reproductibility in my R/Python scientific workflow : data with reproductible analysis (plot, analysis) and paper.
There is, as you know, two big linux flavours offer some solutions : Nix and Guix
In nix, the way commonly described to develop with R is, for example, using rWrapper and rPackages :
pkgs.rWrapper.override{ packages = with pkgs.rPackages; [tidyverse rmarkdown]; };
My problem is (not so...) simple, like Python, R is well known to be a nightmare in term of reproducibility, even at middle term. For fun, you could try to run a ggplot2 code from 2 years with a recent version of R...
In order to propose a flake that produce the same result from the same data for a scientific paper, i'm interested to fix in derivation the version of R and the version of R packages used to compute analysis or plot.
{
description = "Generate R result from simulation";
inputs = {
nixpkgs.url = "nixpkgs/nixos-20.09";
utils.url = "github:numtide/flake-utils";
};
outputs = {self, nixpkgs, utils, mach-nix } : (utils.lib.eachDefaultSystem
(system :
let
pkgs = nixpkgs.legacyPackages.${system};
REnv = pkgs.rWrapper.override{ packages = with pkgs.rPackages; [tidyverse rmarkdown]; };
buildRScripts = { stdenv, fetch,... }: stdenv.mkDerivation {
name = "myscript";
src = self;
nativeBuildInputs = [ REnv ];
dontBuild = true;
buildInputs = [ pkgs.pandoc pkgs.unzip ];
installPhase=''
mkdir $out
cd $out
${REnv}/bin/Rscript -e 'rmarkdown::render("test.Rmd")
'';
in {
defaultPackage = self.packages.${system}.buildRScripts;
}
));}
For example, how could i define more precisly that i want to use, to compile my test.Rmd, only the tidyverse 1.3.1 with R 4.1.O ? Even in 5 years ?
I found that Guix show different available packages/versions of R and tidyverse :
http://data.guix.gnu.org/repository/1/branch/master/package/r-tidyverse
Version needed by tidyverse.1.3.1 are clearly presented :
http://data.guix.gnu.org/gnu/store/31rwnjq3map2njx1518vsxpwbvy7xx2j-r-tidyverse-1.3.1.drv
With rPackages in Nix i search a way to achieve something similar, ie. a way to refer explicitly to version of R or R packages into derivation, but i didn't found it.
With rPackages here nix developper already offering a great fundation, but perhaps we need more ...
How we could, collectively achieve a better reproducibility on R packages with Nix ? I'm interested by any ideas ?
Perhaps we could fetch sources of packages directly from the cran archive and compile it ? For example with tidyverse :
https://cran.r-project.org/web/packages/tidyverse/index.html,
https://cran.r-project.org/src/contrib/Archive/tidyverse/ ?
Ps : i know that Nix and Guix are each partners with https://archive.softwareheritage.org/, a great way to archive and call cran package :
https://guix.gnu.org/fr/blog/2019/connecting-reproducible-deployment-to-a-long-term-source-code-archive/
https://www.tweag.io/blog/2020-06-18-software-heritage/
Ps : answer could also be added to https://nixos.wiki/wiki/R
Update 1
After discussion with some great people on nix discord, i understand that nix doesn't need version because flake.nix + flake.lock store hash (see nix flake metadata) that link my build and download with a very specific commit on nixpkgs.
But that don't solve :
the problem of the tar.gz sources linked/needed by this packages declared at this very specific commit by RPackages ? I suppose software heritage will help on this point ?
the common problem of incompatibility between some R version, and R version of packages. For example, you write a code with R 3.0.0 and tidyverse 1.2.3, you update your R version because some other packages need an update and only works with dependency available with R 3.2.0, but ahum, tidyverse 1.2.3 don't exist for R 3.2.0 ... Fixing version and access to old tar.gz resolve part of this problem i suppose.
How we define something like this using nix ?
Update 2
It seems someone build an unofficial index to help people searching old version of package Ex with tidyverse : https://lazamar.co.uk/nix-versions/?channel=nixpkgs-unstable&package=r-tidyverse
Thanks #dram for link and discussion on this.
The exercise tutorial to the learnr package refers to the checkr packages:
The checkr package currently provides code checking functions that are compatible with learnr.
(Be aware that this reference does not point to the CRAN package checkr. Unfortunately, there is a duplicate package name.)
But in the learnr tutorial there is no advice how to use it together with checkr. On the other hand, the vignette in checkr is still a draft version, discusses advanced issues and is lacking an easy example in relation to learnr.
What I am looking for is a concrete procedure of a model example. For instance: How to check student input with the two-plus-twoexample, provided by the learnrR Markdown template "Interactive Tutorials".
So my question is: How to check with learnr the R code required to add two plus two using the checkr package?
Maybe this questions should use new tags (learnr and checkr) but I do not have the privileges to do so.
After many experiments the following procedure worked for me:
I have started a new project and loaded the R Markdown template
"Interactive Tutorial" and named it "01-Exercises". RStudio
generated a folder this name and put my "01-Exercises.Rmd" in this
folder.
I have added the line tutorial_options(exercise.checker =
checkr::check_for_learnr) to the setup chunk of my
"01-Exercises.Rmd" file.
I have added a new R chunk with the label two-and-two-check. (= The same name as the chunk name of the student task but added -check.)
In this R chunk I have added just one line check_two_and_two(USER_CODE) which will become my test function.
I have written a test function check_two_and_two in an extra script file "check_test.R" and saved under a folder "www".
I have sourced this script to the function into the memory. I ran rmarkdown::run("01-Exercises/01-Exercises.Rmd") from the console.
And here is my test function:
check_two_and_two <- function(USER_CODE) {
code <- for_checkr(USER_CODE)
t1 <- line_where(code, insist(all(F == "+"), "Your operator is {{F}}. This is not the assigned task."))
if (failed(t1)) return(t1)
t2 <- line_where(code, insist(all(V == 4), "Your solution is {{V}}. This is not the result (= 4) asked for."))
if (failed(t2)) return(t2)
line_binding(code, 2 + 2, failif(FALSE, "The pattern did not match."), message = "The result is correct, but I was looking for 2 + 2.")
}
Even if this worked for me: Maybe there is a better (more effective) solution?
I'm unable to save a plot generated by the plot.Node function in data.tree. I've tried the following:
### Create tree object and plot it
data(acme);
plot(acme);
This works fine, showing the plot, as one would expect.
### Try saving it as png
png(filename='file.png', type='cairo-png');
plot(acme);
dev.off();
This creates an empty file. ggsave does the same. Apparantly, plot.Node uses DiagrammeR under the hood, so I looked into that package. It has a function to export graphs:
export_graph(acme, file_name="file.png");
This gives the error:
Error in file.exists(diagram) : invalid 'file' argument
When I transform to GraphViz first, I get a different error:
export_graph(ToGraphViz(acme), file_name="file.png");
Error in graph$dot_code : $ operator is invalid for atomic vectors
Clearly, exporting to GraphViz doesn't quite export to what DiagrammeR expects.
I'm in RStudio and so could in theory just save the plot using the GUI, but I need this for in a script.
Apparently, plot.Node doesn't actually plot anything - instead it seems to generate html/js. Does this mean that that result cannot be stored as a graphic? Or is there some export/conversion function somewhere that I'm completely missing? it certainly feels like I'm missing something obvious - I assume the need to store plotted data.trees as images is quite common. But I have no idea which potential solutions I can still explore.
I'd be very grateful for any pointers anybody has!
As sebastian-c suggested, things work now a bit differently than suggested by Matherion, as of R 3.3.3 with data.tree 0.7.0 and DiagrammeR 0.9.0
Pre-requisite: DiagrmmeRsvg and dependencies need to be installed. Depending on your OS, for this to work you might have to install V8. For example on ubuntu:
apt-get install libv8-3.14-delibv8-3.14-dev
And then in R:
install.packages("DiagrammeRsvg")
On Windows, I didn't have to install anything (maybe because Chrome is installed?).
Once DiagrammeRsvg is available, run:
library(data.tree)
data(acme)
library(DiagrammeR)
export_graph(ToDiagrammeRGraph(acme), "export.pdf")
I've found the answer, at least, partially. There exists a package dedicated to exporting GraphViz diagrams to SVG: DiagrammeRsvg.
So this works:
treeAsSVG <- export_svg(grViz(ToGraphViz(acme)));
writeLines(treeAsSVG, "filename.svg"));
The grViz is necessary to actually convert the ToGRaphViz output to something that can be interpreted by export_svg. I'm still not really sure (yet) what all goes on under the hood - for example, this does not work:
export_graph(grViz(ToGraphViz(acme)), file_name="filename.svg");
But, if anybody else has a similar problem and stumbles upon this question, perhaps this partial answer can help them to at least export something that can then be integrated in e.g. html pages.
By converting acme with as.phylo() it works, but it looks a little boring:
plot(as.phylo(acme),
show.node.label=TRUE,
node.pos=2,
no.margin=TRUE
)
# adding edge labels
edgelabels(as.vector(acme$Get("cost"))[-1],
adj = c(0,-0.5),
frame = "none")
solution with as.phylo()
I also tried as.igraph. However, the nodes overlap and it looks even less pretty:
plot(0, type="n", ann=FALSE, axes=FALSE,
xlim=extendrange(ig[,1]),
ylim=extendrange(ig[,2]))
plot(ig,
layout=layout_as_tree(ig,root=1),
vertex.shape="rectangle",
vertex.size=(strwidth(V(ig)$name) + strwidth("oo")) * 100,
#vertex.size2=strheight("I") * 2 * 100,
edge.label=acme$Get("p",traversal = "level")[-1]
)
solution with as.igraph()
I would like to be able to edit the Fortran code that is referred to in the fGarch package.
More specifically I would like to edit the available conditional distributions that can be used by fGarch::garchFit, i.e. including the stable distribution and the generalised hyperbolic distribution.
So having looked into the garchFit() function, I have delved (deepish) into the code, and .aparchLLH.internal() is referred to from the garchFit() function and there is a line in there that refers to Fortran written code.
The specific line that I am referring to is the following bit of code:
fit <- .Fortran("garchllh", N = as.integer(N), Y = as.double(.series$x),
Z = as.double(.series$z), H = as.double(.series$h),
NF = as.integer(NF), X = as.double(params), DPARM = as.double(DPARM),
MDIST = as.integer(MDIST), MYPAR = as.integer(MYPAR),
F = as.double(0), PACKAGE = "fGarch")
I believe that the Fortran function garchllh is what I would like to edit, but do not know how to go about editing it so that I can introduce new distributions into the garchFit() function.
N.B. Just as a note, I do not have much experience in Fortran code, but would like to have a look at it to see if it can be edited and altered to fit for my purpose, so any help on the Fortran editing of code section would be much appreciated...
As mentioned in comments, you need to download the source -- a good place would be to start with install.packages("fGarch",type="source") and see that everything compiles properly. Then, look at the package source -- seems like you would need to do a pretty straightforward adjustment to dist.f, and probably add more changes to various places where MDIST is set -- start with grep MDIST *.R in the R directory of the extracted source. After you're done and tested, you could also talk to the package maintainers -- perhaps they would include your additions in the next version :)