I have tried following code to generate a password protected zip file in R, but it didn't work and didn't give any error either.
zip('file.zip',
files = 'file.pdf',
flags = paste("--password", 'pwd123'))
Can anyone say what's wrong in this code or how to make it work?
It looks like you were close, but need to add your commands to the extras argument as a list:
pdf("file.pdf")
plot(1)
dev.off()
zip('file.zip',
files = 'file.pdf',
extras = list(paste("--password", 'pwd123'))
)
Related
I have this weird problem. I was able to get here() to work but it stopped working and I can't figure out how to fix it.
So basically the structure of my file is like:
C:/First/Second/Third/Analysis/Scripts
C:/First/Second/Third/Analysis/Data
I want to easily bounce between data and scripts in the code
If I type here("C:/First/")
and then follow up with here(), R says that I'm at
C:/First/Documents
I'm not able to type here("Second", "Third", "Analysis", "Data", "todayscode.R")
because it puts me in the folder:
C:/First/Documents/Second/Third/Analysis/Data/
Which obviously does not exist.
It's unclear why you say that C:/First/Documents/Second/Third/Analysis/Data/ "does not exist". You will need to create a subdirectory if it doesn't exist, but at the beginning of your problem presentation you implied that was an existing directory.
Sounds like you want something like this possibilities:
setwd("C:/First/Second/Third/Analysis/")
You should first clarify where your version of the here function is coming from by typing here at the console input line and noting which namespace it comes from. If you then wanted to refer to a directory with that starting point you could use this (using the example set up in the here package at the here help page):
library(here); library(readr)
readr::read_csv(
here("data", "penguins.csv"),
col_types = list(.default = readr::col_guess()),
n_max = 3
)
I am using a code like this:
writexl::write_xlsx(
x = list(
"Sheet" = df
),
path = paste0("User/", name,"_", date, ".xlsx")
)
The code should write in the folder User that is in the wd the file.xlsx
I need that if the command not success in the writing operation the console display a message.
For example if in the object name there is a "/" windows not allow to write the file but any message appear to me. Also if for permission problem windows not allow to write the file.
I just tried with TryCatch without success.
Thanks
I'm creating a simple little time-saver wrapper function that pre-fills out some standard file locations, etc. for importing an Excel file using readxl::read_xlsx. It works exactly as expected with the defaults, however, when I try to use it at the console with a different file location I get the following error.
Error in read_space_program(path = "inst/extdata/space_program.xlsx") :
unused argument (path = "inst/extdata/space_program.xlsx")
I've tried adding , ..., as suggested on StackOverflow by those with similar error messages, to extend the arguments but it does not fix the problem. This is the code I am running:
read_space_program <-
function(file_location = "inst/extdata/space_program.xlsx",
sheet_name = "Program",
skip_rows = 5, ...) {
readxl::read_xlsx(
path = file_location,
sheet = sheet_name,
col_names = TRUE,
skip = skip_rows
) # first five rows skipped to allow for project information
}
Without uploading the entire .xlsx file, suffice to say that I use this particular file all the time and it is not the source of the problem. It loads fine with this exact code when I run it like this: read_space_program(), however when I test it by feeding the exact same file location into it at the console with this: read_space_program(file_location = "inst/extdata/space_program.xlsx"), I get the error above. This error probably has something to do with something basic, I'm pretty sure, but cannot figure it out. Any help is appreciated.
This was caused by an artifact of development in my environment. Cleaning the environment allowed the code to run.
I can create a report object, but when I export() it, there is a generic file name containing a random number. I want to be able to specify the file name.
for (report in c("file1", "file2", "file3")) {
myReport <- Pandoc$new(author="Jerubaal",title="What's my file name?", format="docx")
myReport$add.paragraph(paste("This is", report))
myReport$export(open = FALSE)
}
This gives me files with names such as
pander-1c246b334f70.docx
pander-1c246b334f70.md
I have tried adding these to Pandoc$new() and also to myReport$export(), but to no avail:
output=report
file=report
filename=report
I will be looping through a lot of things, each of which needs its own report file, so I can't manually rename the files efficiently.
How do I specify the output file name?
Thanks!
Use the first or f argument based on the sources. E.g.:
myReport <- Pandoc$new(author="Jerubaal",title="What's my file name?", format="docx")
myReport$add.paragraph("This is it.")
myReport$export('this_is_the_file_name.docx', open = FALSE)
Please feel free to send a pull request or suggestion for an updated documentation.
These are the steps I am trying to achieve:
Upload a PDF document on the server.
Convert the PDF document to a set of images using GhostScript (every page is converted to an image).
Send the collection of images back to the client.
So far, I am interested in #2.
First, I downloaded both gswin32c.exe and gsdll32.dll and managed to manually convert a PDF to a collection of images(I opened cmd and run the command bellow):
gswin32c.exe -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -r150 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dMaxStripSize=8192 -sOutputFile=image_%d.jpg somepdf.pdf
Then I thought, I'll put gswin32c.exe and gsdll32.dll into ClientBin of my web project, and run the .exe via a Process.
System.Diagnostics.Process process1 = new System.Diagnostics.Process();
process1.StartInfo.WorkingDirectory = Request.MapPath("~/");
process1.StartInfo.FileName = Request.MapPath("ClientBin/gswin32c.exe");
process1.StartInfo.Arguments = "-dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -r150 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dMaxStripSize=8192 -sOutputFile=image_%d.jpg somepdf.pdf"
process1.Start();
Unfortunately, nothing was output in ClientBin. Anyone got an idea why? Any recommendation will be highly appreciated.
I've tried your code and it seem to be working fine. I would recommend checking following things:
verify if your somepdf.pdf is in the working folder of the gs process or specify the full path to the file in the command line. It would also be useful to see ghostscript's output by doing something like this:
....
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.UseShellExecute = false;
process1.Start();
// read output
string output = process1.StandardOutput.ReadToEnd();
...
process1.WaitForExit();
...
if gs can't find your file you would get an "Error: /undefinedfilename in (somepdf.pdf)" in the output stream.
another suggestion is that you proceed with your script without waiting for the gs process to finish and generate resulting image_N.jpg files. I guess adding process1.WaitForExit should solve the issue.