Need help assigning global settings for Rstudio's pandoc in VSCode to knit pdf_document in a .rmd output format - visual-studio-code

So I am migrating from RStudio to Visual Studio Code for my future R projects. I have successfully gotten my github aspect all setup and I am trying to write my code in (.rmd) format so that I can knit it to pdf, html and flex_dashboard outputs. When I have tried to knit the it, I get the following error:
rmarkdown::render("c:\Users\{user}\{folder}\{sub-folder}\{sub-folder}\Co$
Error: pandoc version 1.12.3 or higher is required and was not found (see the help page ?rmarkdown::pandoc_available).>
I found the following solution that once run in the terminal, allows it to knit into a pdf_document successfully.
Code: Sys.setenv(RSTUDIO_PANDOC="--- insert directory here ---")
Reference: pandoc version 1.12.3 or higher is required and was not found (R shiny)
This is great, but everytime I restart VSCode, this setting appears to be reset and I have to run it again. Is there away to set this globally so that I don't have to run it every time I use it? Or is there a better way to do this?

I had a similar problem before in VScode to render R markdown. Now, I found the solutions to it.
Following the steps below to properly set up the pandoc:
Step 1:
Go to your R Studio and go the console, type the following:
Sys.getenv("RSTUDIO_PANDOC")
If you are using MacOS, you will get the path for "pandoc": "/Applications/RStudio.app/Contents/MacOS/pandoc"
If you are using Windows, you probably will get the path like:
"/c/Program Files/RStudio/bin/pandoc/" as mentioned in pandoc version 1.12.3 or higher is required and was not found (R shiny)
Step 2:
Paste the path of "pandoc" and put it into your bash or zsh (depends on what command shell that you are using).
On MacOS, I'm using zsh. Therefore, I add the following path into ~/.zshrc:
export RSTUDIO_PANDOC="/Applications/RStudio.app/Contents/MacOS/pandoc"
As for how to edit ~/.zshrc, one way to do this is presented as follows:
step 2.1
nano ~/.zshrc
It will open your .zshrc file and navigate to the bottom of this file, paste the
export RSTUDIO_PANDOC="/Applications/RStudio.app/Contents/MacOS/pandoc". Then, control + X to exit. It will ask about "Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?", then type Y and type return bottom.
step 2.2
source ~/.zshrc
Now, it should work fine in VScode. By checking if it works, you could open your console by typing R to invoke the R environment and type Sys.getenv("RSTUDIO_PANDOC"). If it returns the path of the "pandoc", it would work fine in VScode as well.

Adding
"terminal.integrated.env.osx": {
"RSTUDIO_PANDOC":"/Applications/RStudio.app/Contents/MacOS/pandoc"
}
into settings.json worked for me.
Adding to .zshrc didn't work at all since the System environment wasn't being passed on to the integrated terminal.

Related

An error in the running of kotline in vscode

enter image description here
Can anyone say how to debug it
It seems like your kotlin directory is not added to your PATH variable thus the extension/terminal may not know what to execute, when trying to execute kotlinc.
The easiest way to resolve this issue is by adding the path to your kotlin directory (path/to/your/kotlin/bin) to your PATH variable
(see here), if you are on windows, or here, if you are on a linux system.
You can check, if it works by opening a command line window and trying to execute
kotlinc -version
However, as it seems like you are using Code-Runner extension in vscode, you can also update your settings.json file:
reference here.

How to allow VS Code to take input from users?

I have installed Visual Studio Code 1.23.1 and added extensions - Python, Code Runner.
With Code Runner, now I can see the Run Code symbol (triangle) and on highlighting it, I see the shortcut Ctrl + Alt + N. But when I try to use it to run the code that asks for user input, I can't find a way to provide the input. When I try to enter user input, I get error message "Cannot edit in read-only editor". I think this is because I am missing some configuration part for Code Runner like setting up PATH or some other Workspace settings.
Question: Please assist me in identifying what all configuration will I need to do and how?
I did select "Add Python 3.6 to PATH" while installing Python. I have attached screenshots for reference:
Note: Even now when I right click and select "Run Python File in Terminal" for the same program, I can enter user input fine and get the expected output.
Here's another alternative answer, I think more accurate.
Add following settings to your vscode user settings file:
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName",
},
Check out this reference for some useful variables:
Variables Reference
You can provide input by telling code runner to use the terminal. To do this, there is a setting called code-runner.runInTerminal, set to false by default, that you can set to true.
There is one more thing that you should watch out for if you are using a windows command line for the terminal like CMD or PowerShell. If your project directory has spaces in it (e.g. C:\Example Test) you will get an error. To fix this, you need to add escaped quotation marks (\") around the directory path variables (normally $dir or $workspaceRoot) found under the setting code-runner.executorMap and code-runner.executorMapByFileExtension in the user settings.
The main problem here is that the output window that the code runner extension uses by default is read only. If you use the terminal instead, your program will be able to accept input as normal.
You can configure Code Runner to use the integrated terminal instead of the output window by setting the code-runner.runInTerminal setting to true (the default is false). In the settings.json file it should look like: "code-runner.runInTerminal": true
If you want to use the GUI instead the setting should look like this once set to true.
If you are using a virtual environment instead of the system python install, you will also need to configure a second setting for it to work properly with installed modules.
The code-runner.executorMap setting will configure what code runner actually does once you press run or use the Ctrl + Alt + N shortcut. By default it seems to simply invoke the python interpreter added to the PATH.
If you change the setting in the settings.json file to:
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
}
then Code Runner will use whatever value is in the pythonPath variable instead. You can set this using the Python: Select Interpreter command from the command palette (Ctrl + Shift + P). This way you can select the interpreter in your virtual environment and use that instead of the one attached to the PATH by default.
The two settings above should allow you to A) Enter input inside of the integrated terminal and B) Select which python interpreter code-runner should execute easily using existing commands.

Set global $PATH environment variable in VS Code

I'm defining a custom $PATH environment variable in my ~/.bash_profile (on a Mac), like so:
PATH="$HOME/.cargo/bin:$PATH:$HOME/bin"
However, VS Code of course does not run my .bash_profile, so it does not have my custom paths. In fact, if I Toggle Developer Tools and check process.env.PATH, it doesn't even seem to have /usr/local/bin.
How do I globally set the $PATH environment variable in VS Code?
(I want to set it globally, not per project or per task, since I'm maintaining a lot of small packages.)
If you only need the $PATH to be set in the integrated terminal, you can use VS Code's terminal.integrated.env.<platform> variable (added in version 1.15). Press Cmd+Shift+P (or Ctrl+Shift+P) and search for "Preferences: Open Settings (JSON)". Then add the following entry to the settings file:
"terminal.integrated.env.osx": {
"PATH": "...:/usr/bin:/bin:..."
}
(Replace .osx with .linux or .windows as needed.)
To see your system's $PATH, type echo "$PATH" in Terminal.app, and copy and paste it into the settings snippet above.
As for having the $PATH available everwhere in VS Code, so that it will
be used by extensions that call binaries, the only workaround I've found so far is this:
Configure your shell (bash by default) to have the $PATH you want. For example, my ~/.bash_profile has the following line:
PATH="$PATH:$HOME/bin"
In VS Code, press ⇧⌘P and type install 'code' command if you haven't done so before.
Quit VS Code.
Launch VS Code not by clicking the icon in the dock or in Launchpad, but by opening Terminal.app and typing code. Your newly set path will be active in VS Code until you quit it.
If VS Code restarts, for example due to an upgrade, the $PATH will reset to the system default. In that case, quit VS Code and re-launch it by typing code.
Update: VS Code on Mac and Linux now apparently tries to automatically resolve the shell environment when it is started by clicking the icon (rather than via code). It does this by temporarily starting a shell and reading the environment variables. I haven't tested this though.
In:
> Preferences: Open Settings (JSON)
add to the JSON file:
"terminal.integrated.env.windows": {
"PATH": "${env:PATH}"
},
-> terminal.integrated.env should end with .osx, .linux or .windows depending on your OS.
In order to check if it works execute in your VS Code Terminal:
# For PowerShell
echo $env:PATH
# For bash
echo "$PATH"
I am using vscode on macos for C/C++ development in conjunction with CMake.
The vscode extension CMake Tools allows to manipulate environment variables via the configuration properties cmake.configureEnvironment, cmake.buildEnvironment and cmake.environment (acting respectively on the CMake configuration phase, the build phase and both - see docs).
Then you can extend your system PATH with custom paths by adding the following snippet to your user or project settings.json:
"cmake.environment": {
"PATH": "~/.myTool/bin:${env:PATH}"
},
Visual Studio Code is the problem.
No matter how you set your PATH variable in the shell, there are cases where Visual Studio Code will not inherit your PATH setting. If you're using an application launcher like LaunchBar to start Visual Studio Code, your PATH variable will not be inherited.
Here is a system-wide fix:
In the /etc/paths.d directory, create a file with your Unix username. In that file, place the additional paths that Visual Studio Code needs to work. In my case, this is the contents of my /etc/paths.d file:
/usr/ucb
/opt/local/bin
/opt/local/sbin
~/go/bin
Note: Your /etc/paths.d file will be processed system-wide. Since most systems are single-user, this shouldn't be a problem for most developers.
Since this is the top Google search result for variants of "VS Code path", I will add my answer here.
I'm running Linux and my problem was that VS Code couldn't find some executable needed to build my project. I was running VS Code from the quick launcher (ALT+F2), and not from a Terminal. I tried modifying the PATH variable in many different places, but I couldn't seem to get it right.
In the end, placing the right PATH inside of ~/.zshenv is what worked. It's because .zshenv is the only file that gets sourced for non-interactive shell command execution like from inside of VS Code (more detailed explanation here https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout )
This was even easier to fix than the above answers suggested.
Open VSCode Settings (Ctrl + ,) and search for terminal.defaultProfile.
I updated my Terminal > Integrated > Default Profile: Windows.
It was set to null by default. As soon as I changed it to PowerShell and restarted the terminal, it picked up my system's path variables!
What did the trick in my case (Linux Mint 19.3 Cinnamon, VS code installed via snap) was to put my appended PATH in ~/.profile . Since this file is read at the beginning of a user session, don't forget to logout/login or reboot after editing this file.
I'm working with ubuntu 18.04. I had a similar problem, my enviroment variables were defined and the terminal knows the $PATH but when I tried to debug with golang, go libraries were not found in $PATH variable.
So, to solve it I uninstall the default version from ubuntu software and install manually using the following instructions:
https://code.visualstudio.com/docs/setup/linux
It works for me.
As of VS Code v1.63.2, you can proceed with Ctrl + Shift + P and then type Open Settings (JSON), and simply add the following line.
"terminal.integrated.inheritEnv": true
In my case the code was already there, but set to false. After changing it, everything was fine.
Getting Code to load your existing ~/.bash_profile would be best. I think the docs here are the relevant reference:
https://code.visualstudio.com/docs/editor/integrated-terminal#_linux-os-x
Typically $SHELL is your primary shell on Unix-like systems so you
probably won't want to change the shell. You can pass arguments to the
shell when it is launched.
For example, to enable running bash as a login shell (which runs
.bash_profile), pass in the -l argument (with double quotes):
// Linux "terminal.integrated.shellArgs.linux": ["-l"]
// OS X "terminal.integrated.shellArgs.osx": ["-l"]
Although, it looks like that setting is the default on my current VS Code (OS X) setup. Integrated terminal is running my ~/.bash_profile without any changes to the configuration. Perhaps try adding echo Executing .bash_profile... to test if it's running when a new terminal is opened in Code.
Add the following to your ~/.bash_profile:
launchctl setenv PATH $HOME/.cargo/bin:$PATH:$HOME/bin
Or run a Bash script when needed, e.g.:
#!/bin/bash
set -Eeuxo pipefail
proj_path=$( cd $( dirname ${BASH_SOURCE[0]} ) && pwd )
launchctl setenv PATH $proj_path/bin:${PATH:-}
For me it's resolved by editing the .desktop file.
Originally I have
Exec=/usr/bin/code-oss --unity-launch %F
. Just changed to
Exec=zsh -c "source ~/.zshrc && /usr/bin/code-oss --unity-launch %F"
since I use zsh, instead of bash. But if you have the same problem with bash, simply replace zsh with bash. And shortcuts from your desktop environment should be fixed.

How to open Visual Studio Code from the command line on linux?

I know I can use command "code" to open VS code or file, but I don't know what should I do to make it possible after I install VS code in Ubuntu.Thanks.
Launching from the Command Line
You can launch VS Code from the command line to quickly open a file, folder, or project. Typically, you open VS Code within the context of a folder. We find the best way to do this is to simply type:
code .
Tip: We have instructions for Mac users in our Setup topic that enable you to start VS Code from within a terminal. We add the VS Code executable to the PATH environment variable on Windows and Linux automatically during installation.
Sometimes you will want to open or create a file. If the specified files does not exist, VS Code will create them for you:
code index.html style.css readme.md
Tip: You can have as many file names as you want separated by spaces.
Source: https://code.visualstudio.com/Docs/editor/codebasics
So, there are a couple of solutions for this.
I've linked a video that shows you how to add vscode to $PATH
(which didn't work for me because I couldn't find the "shell:install path" command)
I uninstalled the vscode from my ubuntu and re-installed using sudo snap install --classic code
(This method worked for me)
Tell me which one works for you... and if you have extensions installed to your vscode then i guess you ought to make a backup or something.
Link to the video: https://youtu.be/iP5FKZXtDBs

Convert ipynb to pdf in Jupyter

I am new to ipython notebook, and I would like to convert my ipynb to pdf. But I get the following error when I try to Download as PDF via LaTex.
nbconvert failed: pdflatex not found on PATH
There is no documentation anywhere how to add pdflatex to my PATH. I use windows. Thank you!
A simple and surprisingly good solution is to print the notebook to pdf through the browser with ctrl+p. Just make sure your plots and figures are not on interactive mode otherwise they will not be displayed (set them to %matplotlib inline).
Exporting jupyter notebooks through latex is quite troublesome and takes a lot of tinkering to get something remotely close to publish ready. When I absolutely need publication quality I do it on a latex editor, but this tutorial goes in great length about doing it on jupyter.
A few useful tips to get better results:
Higher resolution plots
Hide your code-cells from the pdf
Take a look at these extensions to improve your jupyter documents
For Mac OS X, the solution for me was to install MacTex first and then export the path to find it:
### TeX
export PATH="/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:$PATH"
You can add this to your .bash_profile or similar config file to load it every time.
See more here https://github.com/jupyter/nbconvert/issues/406
As said by Thomas K in the comments, you need to have Latex installed, and after add the path to the directory containing pdflatex.exe file to the PATH variable of your system.
I have looked for a lightweight distribution and tried installing TeXworks, but I didn't find any pdflatex.exe file.
So I have tried TeX Live, which worked fine creating the pdflatex.exe file under the target installation directory. This path should be like C:\...\texlive\2016\bin\win32.
Finally, you should just add this path to the PATH environment variable of your system (you can use the link shared by Thomas K).
As said here, you need to quit jupyter notebook and open a new command prompt after making any path changes, in order for jupyter to find the newly added item to the PATH.
Then, in Jupyter, you can check your environment variables by running the following (refer to this link for details):
import os
os.environ['PATH'].split(';')
and check if it contains the path to pdflatex.exe file.
If you get some trouble when exporting your notebook to pdf due to missing files/packages (this happened to me), refer to this link to search and install them under TeX Live.
For Linux, the reported error is due to the lack of XeLatex, part of the texlive-xetex package.
Installation in ubuntu will be:
sudo apt install texlive-xetex
Instead of using nbconvert what you can do is :
Download your ipynb file as HTML from File option.
Right-click and select print or use Ctrl+P.
Save as PDF
Easy.
Here is the full solution that worked for me (for Mac).
brew cask install mactex
$ cd ~/
$ touch .bash_profile
This will open the bash profile on TextEditor
$ open -e .bash_profile
Paste the following to the top and save
export PATH="/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:$PATH"
Close any notebook you have and reopen it
Here is the notebook explaining it step by step:
https://github.com/ybaktir/notes/blob/master/Convert%20Jupyter%20Notebook%20to%20Pdf.ipynb
I agree that latex installation (at least on windows) is painful and the result in my case was not a great looking document. The ctrl-p method alone doesn't work great if you're running in JupyterLab, but if you export the notebook to HTML, then print from the browser, choosing PDF, the result is quite good.
I know my solution is not at a level. But it works !!
in your browser of notebook tab, simply do "ctrl + p" to get download in pdf
First export the notebook file to HTML (available through File> Download as..).
If you are using JupyterLab, then this is available under File > Export Notebook As....
Use (any) free online converters to convert html file to a pdf file. (One such free online converter is sejda (https://www.sejda.com/html-to-pdf)
Note, there are many such converters are available online.