Create an SDMX file in R - r

I do not have any code to post, but just a question.
There are several tools I am aware of to read SDMX files in R (an SDMX is an XML file for exchanging statistical data) like for instance
https://github.com/opensdmx/rsdmx
https://github.com/amattioc/SDMX
but does anyone know a way to export some data to an SDMX format for dissemination?
Any suggestion is welcome!

This is not a ‘pure’ R solution, but the Python sdmx1 package is fully usable through reticulate, and allows to programmatically generate SDMX objects and then serialize them as SDMX-ML (XML). For example:
# Use reticulate to import the Python package
> library(reticulate)
> sdmx <- import("sdmx")
# Create an (empty) DataMessage object
> msg <- sdmx$message$DataMessage()
# Convert to XML
> xml <- sdmx$to_xml(msg, pretty_print = TRUE)
# Write to file using the built-in R method
# The Python 'bytes' object must be decoded to a string
> write(xml$decode(), file = "message.xml")
This gives output like:
<mes:GenericData xmlns:com="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" xmlns:data="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/structurespecific" xmlns:str="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure" xmlns:mes="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" xmlns:gen="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic" xmlns:footer="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message/footer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<mes:Header>
<mes:Test>false</mes:Test>
</mes:Header>
</mes:GenericData>
For more information on authoring more complex messages using sdmx1, there is a page “HOWTO Generate SDMX-ML from Python objects” in the documentation.

Related

Reading a .mat file using R

I'm trying to read a .mat file using R.
library(R.matlab)
data <- readMat('e-060RAW.mat')
It gives me this error.
Error in readMat5(con, firstFourBytes = firstFourBytes, maxLength = maxLength) :
Reading of MAT v7.3 files is not supported. If possible, save the data in MATLAB using 'save -V6'.
How am I supposed to get this sorted out.
Is there any other way to read a .mat file using R.
From https://www.rdocumentation.org/packages/R.matlab/versions/3.6.2/topics/readMat:
MAT v7.3 files, saved using for instance save('foo.mat', '-v7.3'),
stores the data in the Hierarchical Data Format (HDF5) [6, 7], which
is a format not supported by this function/package. However, there
exist other R packages that can parse HDF5, e.g. CRAN package h5 and
Bioconductor package rhdf5.

How can i write as a csv file for S4 class?

I am using DEP proteomics package to analyse my mass spectometry data. I want to remove the batch effect from the my data. So after the preprocessing of my data i want to download as a CSV file file so that i can upload into batch server. but I am not able to write as CSV. Whenever i try i am getting error (no method for coercing this S4 class to a vector)? I am new to R. i read something about this and still i don't have clear idea?
can someone help me with this?
> data_se <- make_se(data_unique, LFQ_columns, experimental_design)
> LFQ_columns <- grep("LFQ.", colnames(data_unique))
> data_se_parsed <- make_se_parse(data_unique, LFQ_columns)
> is(data_se)
[1] "SummarizedExperiment" "RectangularData" "Vector" "Annotated"
[5] "vector_OR_Vector"
> data_se#metadata
list()
> View(data_se)
> colnames(data_se)
[1] "Ubi4_1" "Ubi4_2" "Ubi4_3" "Ubi6_1" "Ubi6_2" "Ubi6_3" "Ctrl_1" "Ctrl_2" "Ctrl_3" "Ubi1_1" "Ubi1_2" "Ubi1_3"
> write.csv2(data_se, "/home/dell/Desktop/Preoteomics_TMT_data/ubi_data_se.csv")
Error in as.vector(x) : no method for coercing this S4 class to a vector
Sorry, the question is not too clear (not sure what you want to upload for example), but you can access and write to file what looks like mass spec readings using the following:
my_data <- data_se#assays#data#listData
write.csv2(my_data, "my_upload.csv")
It probably won't be in the format needed for your batch server, but that is a separate question.
Not sure if this is the data you want! If you have no luck here, try biostars, where this type of question is more common.

R-Studio IDE on Databricks

I am trying to import a dataset from my Databricks File System (DBFS) to R-Studio- which is running on Databricks Cluster; and I am facing this issue below.
> sparkDF <- read.df(source = "parquet", path = "/tmp/lrs.parquet", header="true", inferSchema = "true")`
Error: Error in load : java.lang.SecurityException: No token to
authorize principal
at com.databricks.sql.acl.ReflectionBackedAclClient$$anonfun$com$databricks$sql$acl$ReflectionBackedAclClient$$token$2.apply(ReflectionBackedAclClient.scala:137)
at com.databricks.sql.acl.ReflectionBackedAclClient$$anonfun$com$databricks$sql$acl$ReflectionBackedAclClient$$token$2.apply(ReflectionBackedAclClient.scala:137)
at scala.Option.getOrElse(Option.scala:121)
at com.databricks.sql.acl.ReflectionBackedAclClient.com$databricks$sql$acl$ReflectionBackedAclClient$$token(ReflectionBackedAclClient.scala:137)
at com.databricks.sql.acl.ReflectionBackedAclClient$$anonfun$getValidPermissions$1.apply(ReflectionBackedAclClient.scala:86)
at com.databricks.sql.acl.ReflectionBackedAclClient$$anonfun$getValidPermissions$1.apply(ReflectionBackedAclClient.scala:81)
at com.databricks.sql.acl.ReflectionBackedAclClient.stripReflectionException(ReflectionBackedAclClient.scala:73)
at com.databricks.sql.acl.Refle
The DBFS Location is correct, any suggestions or blogs are welcomed for this!
The syntax for reading data with R on Databricks depends on whether you are reading into Spark or into R on the driver. See below:
# reading into Spark
sparkDF <- read.df(source = "parquet",
path = "dbfs:/tmp/lrs.parquet")
# reading into R
r_df <- read.csv("/dbfs/tmp/lrs.csv")
When reading into Spark, use the dbfs:/ prefix, when reading into R directly use /dbfs/.
We should use dbfs before the directory path.
For Example: /dbfs/tmp/lrs.parquet

What's the most simple approach to name-spacing R files with `file::function`

Criteria for answer to this question
Given the following function (within its own script)
# something.R
hello <- function(x){
paste0("hello ", x)
}
What is the most minimal amount of setup which will enable the following
library(something)
x <- something::hello('Sue')
# x now has value: "hello Sue"
Context
In python it's very simple to have a directory containing some code, and utilise it as
# here foo is a directory
from foo import bar
bar( ... )
I'm not sure how to do something similar in R though.
I'm aware there's source(file.R), but this puts everything into the global namespace. I'm also aware that there's library(package) which provides package::function. What I'm not sure about is whether there's a simple approach to using this namespacing within R. The packaging tutorials that I've searched for seem to be quite involved (in comparison to Python).
I don't know if there is a real benefit in creating a namespace just for one quick function. It is just not the way it is supposed to be (I think).
But anyway here is a rather minimalistic solution:
First install once: install.packages("namespace")
The function you wanted to call in the namespace:
hello <- function(x){
paste0("hello ", x)
}
Creating your namespace, assigning the function and exporting
ns <- namespace::makeNamespace("newspace")
assign("hello",hello ,env = ns)
base::namespaceExport(ns, ls(ns))
Now you can call your function with your new namespace
newspace::hello("you")
Here's the quickest workflow I know to produce a package, using RStudio. The default package already contains a hello function, that I overwrote with your code.
Notice there was also a box "create package based on source files", which I didn't use but you might.
A package done this way will contain exported undocumented untested functions.
If you want to learn how to document, export or not, write tests and run checks, include other objects than functions, include compiled code, share on github, share on CRAN.. This book describes the workflow used by thousands of users, and is designed so you can usually read sections independently.
If you don't want to do it from GUI you can useutils::package.skeleton() to build a package folder, and remotes::install_local() to install it :
Reproducible setup
# create a file containing function definition
# where your current function is located
function_path <- tempfile(fileext = ".R")
cat('
hello <- function(x){
paste0("hello ", x)
}
', file = function_path)
# where you store your package code
package_path <- tempdir()
Solution :
# create package directory at given location
package.skeleton("something", code_file = file_path, path = package_path)
# remove sample doc to make remotes::install_local happy
unlink(file.path(package_path, "something", "man/"), TRUE)
# install package
remotes::install_local(file.path(package_path, "something"))

import R forecast library JAR files into java

I am trying to import the R package 'forecast; in netbeans to use its functions. I have managed to make the JRI connection and also to import the javaGD library and experimented with it with a certain success. The problem about the forecasting package is that I cannot find the corresponding JAR files so to include them as a library in my project. I am loading it normally : re.eval(library(forecast)), but when I implement one of the library's function, a null value is returned. Although I am quite sure that the code is correct I am posting it just in case.
tnx in advance
Rengine re = new Rengine(Rargs, false, null);
System.out.println("rengine created, waiting for R!");
if(!re.waitForR())
{
System.out.println("cannot load R");
return;
}
re.eval("library(forecast)");
re.eval("library(tseries)");
re.eval("myData <- read.csv('C:/.../I-35E-NB_1.csv', header=F, dec='.', sep=',')");
System.out.println(re.eval("myData"));
re.eval("timeSeries <- ts(myData,start=1,frequency=24)");
System.out.println("this is time series object : " + re.eval("timeSeries"));
re.eval("fitModel <- auto.arima(timeSeries)");
REXP fc = re.eval("forecast(fitModel, n=20)");
System.out.println("this is the forecast output values: " + fc);
You did not convert values from R into java, you should first create a numerical vector of auto.arima output in R, and then use the method .asDoubleArray() to read it into java.
I gave a complete example in [here] How I can load add-on R libraries into JRI and execute from Java? , that shows exactly How to use the auto.arima function in Java using JRI.

Resources