I have a text file and I want to append some string to it without writing over the existing data. How can I accomplish this in Julia?
Julia provides a bunch of different options to accomplish this same goal. One possible option is to do:
# Open file in append mode and then write to it
exampleFileIOStream = open("example.txt","a")
write(exampleFileIOStream, "Hello world!");
You can read the full docs for the open function and the corresponding functionality in the Julia docs.
Related
I have seen that there is the nice option to enter an input through a pop-up window which has been created in R.
Is it possible to write a code that creates a pop-up window (or maybe any other nice interface) where one can enter a csv file? (or excel file.)
Would it also be possible to then get an output as a csv or excel file?
I'm thankful for any idea since I'm not sure what is possible in which language. If you think things would be easier in python - let me know. I just thought to work with data frames is very easy in R.
I already explored a bit the package svDialogs
There is for example the function dlgInput() which can take as an input some integer and there are also functions such as dlg_form() etc which do similar things. But I don't find a function that would take a csv file or so as an input.
You can use rstudioapi:
rstudioapi::selectFile("Select File") returns the file path of your select file, to open a csv interactively, you can use following code:
read.csv(rstudioapi::selectFile("Select File"))
You can read more at: https://rstudio.github.io/rstudioapi/reference/file-dialogs.html
I'm trying to read all the bytes of a file using Julia into an array. So far I have:
s = open(file_path,"r")
I'm not sure how to tell how big the file is. I'm also not sure that I need to. Perhaps I can just pass an empty array into readbytes!
The simplest way do do it is to use read function.
You can either pass an open stream to it like data = read(s) if s was opened with the code you have provided above.
Alternatively you can simply write data = read(file_path). In this way you do not have to close the stream yourself.
You can find out the details by reading help of read by executing ?read in Julia REPL.
To get the size of the file in bytes you can use filesize(file_path) function.
After a bit of testing this seems to work ...
s = open(file_path,"r")
data = UInt8[]
readbytes!(s,data,Inf)
close(s)
I'm trying to write out an R output to a text file that is not saved as .txt but as some other unique identifier (for example .prt). I know that's possible with matlab, but I don't know how to get that to work with R.
I can't find any package to do that, and when I try to specify the extension in the file name it give me an error and doesn't save.
Any idea would be greatly welcome! Thank you.
Unless you are using some specialized package, a lot of standard R functions for writing data to files have a file= parameter (or similar) to let you specify whatever the filename (and extension) you want. For example:
dummy.data <- matrix(rnorm(25),ncol=5)
### in reality you could just write file="dummyfile.prt" as one string
### but for demonstration purposes, you can use paste0() or paste(,sep='')
### to create a new file name using some variable prefix and your extension
### ".prt"
### sep='\t' makes the output tab-delimited
write.table(dummy.data,file=paste0("dummyfile",".prt"),sep='\t')
I'm testing out using a simple piece of code to create directories that don't exist for my monthly SAS analysis so I don't have to manually do the pre-prep so to speak:
options dlcreatedir;
libname newdir "C:\Folder\&YYYYMM.\Inputs";
However I've read that using that options command essentially leaves it on for all subsequent pieces of code and I am worried that it might perform some messy writes on my disk if I make a mistake and leave it on.
Is there a way to turn it off once I am done creating directories?
If you review the documentation you'll notice it lists two options, DLCREATEDIR and NODLCREATEDIR. Use the second to turn off the option.
option nodlcreatedir;
http://support.sas.com/documentation/cdl/en/lesysoptsref/64892/HTML/default/viewer.htm#n1pihdnfpj4b32n1t62lx0zdsmdn.htm
Another option is to use the DCREATE() function without changing the default options to create a folder explicitly rather than rely on the libname statement to create the folder.
data _null_;
folder = dcreate("Inputs", "C:\Folder\&YYYYMM.\");
run;
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002986745.htm
I want to save a part of my r script output including the commands into a text file. I know sink() but it does not include the commands or I could not find a specific option to do that.
Is there any possibility to capture the commands and its ouput within an r session. Simply write an Rmd or capture the output within the console is not the solution at the moment.
You are probably looking for the TeachingDemos package. Documentation can be found here.
Example:
library(TeachingDemos)
txtStart("test.txt")
# Your code
txtStop()
This should write both your command input and output to a file called test.txt.
If you're working interactively, here's one idea. It was this specific problem for which I created the sinkstart() function in the rite package. Basically, this creates a pop-up tcl/tk widget that you can write commands and output to. Here's a screenshot to give you a feel:
There are just two relevant functions: sinkstart() starts the sink; sinkstop() turns it off. You can toggle back and forth to selectively write to the widget. Then you can just save the contents with a right-click or a key shortcut.