cmd from R with wkhtmltopdf - r

I am trying to use wkhtmltopdf to turn website content into pdf and then read it into my R. So I write in my COMMAND PROMPT line to download as html front page of yahoo finance (just for fun). So i create "TemporaryFolder" on my C and write in cmd:
C:\Program Files\wkhtmltopdf\bin>wkhtmltopdf https://finance.yahoo.com/ "C:/TemporaryFolder/myhtml.pdf"
And it downloads yahoo finance website as pdf. Now I want to do the same thing but using R script. I know there is system function however I have very little experience with it (and with cmd to be honest).
So now i try use this command in my Rstudio so I can later create R script which downloads website as html and converts it to pdf.
URL="https://finance.yahoo.com/"
wkhtmltopdf_dir="C:/Program Files/wkhtmltopdf/bin"
save_as="C:/TemporaryFolder/myhtml.pdf"
x=paste0(wkhtmltopdf_dir,">","wkhtmltopdf"," ",URL," ",'\"',save_as,'\"')
system(x)
I also tried shell(x) but I got "permission denied".
But it does nothing... Could someone elaborate how system works and what should be added here?
BTW: can I harm my computer by using system? For example writing some "bad" command? This question might sound silly, but I am really to new to this.

What you're trying to paste as a command ("C:/Program Files/wkhtmltopdf/bin>wkhtmltopdf https://finance.yahoo.com/ \"C:/TemporaryFolder/myhtml.pdf\"") doesn't quite work. The first part ("C:/Program Files/wkhtmltopdf/bin>) is actually the prompt when you run it in commander. It's not a part of the command, but instead shows in which directory you are running that command.
If you replace wkhtmltopdf with C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe, it should work just fine:
URL="https://finance.yahoo.com/"
wkhtmltopdf_exe="C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe"
save_as="C:/TemporaryFolder/myhtml.pdf"
x=paste0(wkhtmltopdf_exe," ",URL," ",'\"',save_as,'\"')
system(x)
To answer your second question, a call to system() runs the command through CMD. So basically anything you could mess up through CMD.exe, you can mess up through system().

I figured out what was wrong. As i posted in comment, after using shell(x) instead of system(x) it returned 'C:/Program' is not recognized as an internal or external command, operable program or batch file.. So I reinstalled my wkhtmltopdf to folder which name contains no spaces. So wkhtmltopdf_exe is now:
wkhtmltopdf_exe="C:/Programs/wkhtmltopdf/bin/wkhtmltopdf.exe"
Rest of the code is the same. Followup here would be nice, is there any way to workaround spaces in folder names? Or should I always avoid spaces? Putting wkhtmltopdf path into quotation marks didnt help.
Thank for user JAD for fixing my first code

Related

Error that says Rscript is not recognized as an internal or external command, operable program or batch file [duplicate]

shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
This is the call to script.On calling the above script, the error occurs.
I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.
Add the Rscript path to your environment variables in Windows:
Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"
Restart everything. Should be good to go. Then you should be able to do
exec('Rscript PATH/TO/my_code.R')
instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.
You need to set the proper path where your RScript.exe program is located.
exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
C:\\My_work\\R_scripts\\my_code.R my_args";
#my_args only needed if you script take `args`as input to run
other way is you declare header in your r script (my_code.r)
#!/usr/bin/Rscript
and call it from command line
./my_code.r
If you are running it in Git Bash terminal, you could follow a revised version of the idea suggested by #user5249203: in the first line of your file my_code.R, type the following
#!/c/R/R-3.2.2/bin/Rscript.exe
I assumed that your path to Rscript.exe is the one listed above C:\R\R-3.2.2\bin. For anyone having a different path to Rscript.exe in Windows, just modify the path-to-Rscript accordingly. After this modification of your R code, you could run it in the Git Bash terminal using path-to-the-code/mycode.R. I have tested it on my pc.
I faced the same problem while using r the first time in VS Code, just after installing the language package (CRAN).
I restart the application and everything worked perfectly. I think restarting would work for you as well.

Can not call "excel.link" package using Rscript.exe

i am new to R and recently i am using 'excel.link' to read excels with macros.
everything is good when i am using R studio.
However, when i call the Rscript using Rscript.exe, it gave me this error.
enter image description here
i have tried to check the library directory and i copy it to every path i can find. But it still doenst work.
Can anyone help on that ?
library(excel.link)

Where does RStudio store the temporary R script files before saved?

I've just had RStudio crash on me unexpectedly, and on re-starting, contrary to what I've come to expect, the R script I had been tinkering around with was nowhere to be found.
I've managed to track down the Rhistory file so I'll be able to piece together all the commands, which is reassuring.
However, I am curious if there's somewhere I might try looking to find the temporary unsaved file on the off chance that might be cached somewhere (after all, it is usually cached somewhere that RStudio apparently knows to look). Is there a particular file extension/format I should be searching for?
Currently running R 3.3.1 through RStudio 0.99.903 on Linux Mint 17.3 (over Ubuntu 14.04.3 LTS).
I've tried running grep on the command line to find some of the more recently updated lines of code; I may be out of luck. I found two files:
~/.rstudio-desktop/history_database
Which appears to basically be a more centralized .Rhistory for RStudio
and
~/.rstudio-desktop/sdb/s-9CD2C698/D7986B2A
This looks JSON-like and also appears to basically be an Rhistory. Please correct me if I'm wrong.
As indicated by #KevinUshey from RStudio:
RStudio stores autosave data as part of the JSON 'blobs' within the sdb folder. You should see the document serialized as a long 'string', with newlines embedded.
Use packages such as jsonlite to parse this and best of luck.
if you used Rstudio on linux, the temporary R script files were stored in .rstudio/sources folder, and you can open all of the script files directly.
Good luck
In Rstudio if you saved or not a script but you ran this code, you can check the history off the app, this is the "telemetric data" that Rstudio has about you.
In windows, this is the path,
C:\Users\ANALISTA\AppData\Local\RStudio\history_database
you should use "visual studio code" or something similar to see it.

Running Plink from within R

I'm trying to use a Windows computer to SSH into a Mac server, run a program, and transfer the output data back to my Windows. I've been able to successfully do this manually using Putty.
Now, I'm attempting to automate the process using Plink. I've added Plink to my Windows Path, so if I open cmd and type in a command, I can successfully log in and pass commands to the server:
However, I'd like to automate this using R, to streamline the data analysis process. Based on some searching, the internet seems to think that the shell command is best suited to this task. Unfortunately, it doesn't seem to find Plink, though passing commands through shell to the terminal is working:
If I try the same thing but manually setting the path to Plink using shell, no output is returned, but the commands do not seem to run (e.g. TESTFOLDER is not created):
Does anyone have any ideas for why Plink is unavailable when I try to call it from R? Alternately, if there are other ideas for how this could be accomplished in R, that would also be appreciated.
Thanks in advance,
-sam
I came here looking for an answer to this question, so I only have so much to offer, but I think I managed to get PLINK's initial steps to work in R using the shell function...
This is what worked for me:
NOT in R:
Install PLINK and add its location to your PATH.
Download the example files from PLINK's tutorial (http://pngu.mgh.harvard.edu/~purcell/plink/tutorial.shtml) and put them in a folder whose path contains NO spaces (unless you know something I don't, in which case, space it up).
Then, in R:
## Set your working directory as the path to the PLINK program files: ##
setwd("C:/Program Files/plink-1.07-dos")
## Use shell to check that you are now in the right directory: ##
shell("cd")
## At this point, the command "plink" should be at least be recognized
# (though you may get a different error)
shell("plink")
## Open the PLINK example files ##
# FYI mine are in "C:/PLINK/", so replace that accordingly...
shell("plink --file C:\\PLINK\\hapmap1")
## Make a binary PED file ##
# (provide the full path, not just the file name)
shell("plink --file C:\\PLINK\\hapmap1 --make-bed --out C:\\PLINK\\hapmap1")
... and so on.
That's all I've done so far. But with any luck, mirroring the structure and general format of those lines of code should allow you to do what you like with PLINK from within R.
Hope that helps!
PS. The PLINK output should just print in your R console when you run the lines above.
All the best,
- CC.
Just saw Caitlin's response and it reminded me I hadn't ever updated with my solution. My approach was kind of a workaround, rather than solving my specific problem, but it may be useful to others.
After adding Plink to my PATH, I created a batch script in Windows which contained all my Plink commands, then called the batch script from R using the command shell:
So, in R:
shell('BatchScript.bat')
The batch script contained all my commands that I wanted to use in Plink:
:: transfer file to phosphorus
pscp C:\Users\Sam\...\file zipper#144.**.**.208:/home/zipper/
:: open connection to Dolphin using plink
plink -ssh zipper#144.**.**.208 Batch_Script_With_Remote_Machine_Commands.bat
:: transfer output back to local machine
pscp zipper#144.**.**.208:/home/zipper/output/ C:\Users\Sam\..\output\
Hope that helps someone!

Scilab issue with exec command

I am using Scinote 5.4.0 with OSX 10.7.4. I am unable to execute script files from the console using the exec("path") command; when I do so, only the first line of the script file is read.
Example:
-->exec("plot1.sce")
-->x=[0:.1:10]'; //(the first line of my code)
If however I "execute with echo" from the editor Scinote, the script will run just fine.
Does anybody know what is going on? (The script files I am trying to run are in my present working directory).
Thanks!
Update: I just installed Scilab on an identical machine and the same thing is happening.
Update: Per Scilab's bugtracker, it appears to be caused by Scinote defaulting to cr eol on a mac. I don't really know what this means or how to fix it, but the adventure continues!
Update: I found the solution!:http://comments.gmane.org/gmane.comp.mathematics.scilab.user/6184
In the preferences, I changed the eol to Unix, and the default file encoding to iso-8859-1. I restarted and exec is now working!
The link is not easy to follow so here's the answer (pasted)
The problem is actually scinote's, not scilab's. I don't have a Lion
machine to check if I'm correct, but it seems scinote's file encoding
is no longer compatible in ML. I discovered that when I opened scinote
generated files with a different text editor (vi) the new lines
weren't encoded right for my machine. The other give-away was that
executing scripts written prior to upgrading worked fine.
Go to preferences and in the scinotes tab, switch default file
encoding to iso-8859-1. I also switched the end-of-line to Unix.

Resources