Saving a lattice plot - r

I am trying to open a device, but getting the following error:
> trellis.device(device="pdf", filename="runtime.pdf")
Error in device.call(...) : unused argument (filename = "runtime.pdf")
The same error does occur when I try to open a device with
pdf(filename="c:/R/FSM/runtime.pdf")
Is there a package that I need to load into the library?

The correct argument is file rather than filename, as in pdf(file = "myfile.pdf"). Other functions that open new devices do use the filename argument, such as jpeg(), so you need to check the help file. In general, the error that was returned Error in ...: unused argument indicates that an argument supplied in the function call is not part of the function.

Related

Warning message within R function seems to make the function not work?

Using the syuzhet package in R, the following works but returns a warning message:
object <- get_text_as_string("path/name.txt")
When I put this in a function, it returns the same warning error but does NOT change the value of object:
gen <- function(file){
object <- get_text_as_string(file)
}
gen("path/name.txt")
This is the warning message, if it matters:
Warning message:
In readLines(path_to_file) :
incomplete final line found on 'path/name.txt'
...but again, I get that from get_text_as_string() when used outside of the function, but it DOES change the value of object.
Anyone have any advice? There must be something I don't understand about functions?
(I've looked for similar questions/answers, if I've missed the right one I'd be happy to just be directed there.)

geojsonio::geojson_read error for a geojson file "conversion from feature type sfc_GEOMETRY to sp is not supported"

I downloaded a geojson file of Iraq's electrical grid from EnergyData. You can download the file yourself here:
https://development-data-hub-s3-public.s3.amazonaws.com/ddhfiles/145188/electric-network-iraq.geojson
and visit the webpage here:
https://energydata.info/dataset/iraq-electricity-transmission-network-2017/resource/4a302ef4-0d79-47db-b301-7b40293067b0
I tried to use the library geojsonio to read the file into R, but:
When I set the what argument to what = "sp" it returns the error:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'addAttrToGeom': conversion from feature type sfc_GEOMETRY to sp is not supported
When I set what = "list" I get the error:
Error in rcpp_sf_to_geojson(sf, digits, factors_as_string) :
Unknown R object type
When I set what = "json" I get the error:
Error in read_json(x, parse, what, stringsAsFactors, ...) :
what='json' not supported for file and url inputs yet
Here's my code:
library(geojsonio)
obj = geojson_read("Filepath/electric-network-iraq.geojson"),
what = "sp")
I tried alternate code, which just returned an empty string:
library(spData)
obj = system.file("Filepath/electric-network-iraq.geojson",package="spData")
I know the file isn't actually empty because when I open the file up in Notepad I can see the coordinates of the electrical grid.
This is my first time ever dealing with the geojsonio package, so please be detailed in your response. I just want to read in the powerlines into R as a lines spatial object, but I'm new to these data formats.

How do I use strings in functions for R ? (file,rt error)

I am trying to load data into a function to use for analysis later on and there seems to be issues inserting a string (i.e. a filename) into my function. Here is what I am working with.
hist_sep<- function(dex_file,etoh_file,sep_parameter) {
dex<-read.csv("dex_file")$sep_parameter
etoh<-read.csv("etoh_file")$sep_parameter
}
The code below outputs this error
hist_sep(RT_3h_Amp_A.csv , RT_3h_Amp_EtOH_A.csv , FL1.A)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
However, if I input the filenames myself (as below) then everything works great. So everything is in the correct directory.
dex<-read.csv("RT_3h_Amp_A.csv")$FL1.A
etoh<-read.csv("RT_3h_Amp_EtOH_A.csv")$FL1.A
Any ideas how I could get around this problem?
Remove the quotes around the filenames in your function (e.g., should be dex<-read.csv(dex_file)$sep_parameter, not dex<-read.csv("dex_file")$sep_parameter). Otherwise, it's trying to download a file CALLED "dex_file".

SparkR reading and writing dataframe issue

I have a Spark DataFrame which I want to write to my disc, I used the following code-
write.df(data_frame,"dataframe_temp.csv",source="csv",mode="overwrite",schema="true",header="true")
It got completed and I can see a new folder created with a _SUCCESS file in it.
Now when I am trying to read from the same file, using following code-
dataframe2<-read.df("dataframe_temp.csv",inferSchema="true",header="true")
I am getting following error:
ERROR RBackendHandler: loadDF on org.apache.spark.sql.api.r.SQLUtils
failed Error in invokeJava(isStatic = TRUE, className, methodName,
...) : org.apache.spark.sql.AnalysisException: Unable to infer
schema for ParquetFormat at dataframe.csv. It must be specified
manually;
I have even tried using repartition
data_frame<-repartition(data_frame,1)
Any help?
You also have to specify the source as "csv":
dataframe2<-read.df("dataframe_temp.csv", source="csv")
Regarding the header argument:
Currently there is also a bug in SparkR for Spark 2.0, where the variable arguments of the write.df function aren't passed to the options parameter (see https://issues.apache.org/jira/browse/SPARK-17442). That's why the header is not written to the csv even if you specify header="true" on write.df.
Got it solved using parquet file format, parquet file format stores the schema by default.

Correctly set R default graphic device to quartz?

I tried to add the following line in my .Rprofile file:
options(device = quartz)
It produced an error:
Error in options(device = quartz) : object 'quartz' not found
Then I tried:
options(device = "quartz")
And it works.
However, both work in the regular R session. Can anyone tell me what is the reason for the difference in behavior?
The erro message says it all. There is no data-object named 'quartz' and the options function is not expecting (nor can it find) a function name as an argument value for the 'device'-node.
You are seeing the effect of the environment where .Rprofile is being evaluated because some of the usual packages (such as stats or graphics) are not yet loaded. Read more about this at ?Startup. You could avoid this by starting .Rprofile with require(grDevices)

Resources