Display Images from file in R Jupyter notebook - r

I have an image on disk and want to display it in a markdown cell in an R Jupyter Notebook. How can I go about this?
I know with Python this as simple as importing the Image class from display.

IRdisplay has functions to rich display "stuff", which includes images:
library("IRdisplay")
display_png(file="plot.png)

In a markdown cell as you usually would in a Jupyter Python notebook:
<img src="../relative/path/to/img.png">
or
![image](../relative/path/to/img.png)

Here is code for the Jupyter R Kernel Notebook User to select choose an image file (.PNG) from the file system, strip off the full pathname, and then insert the image into the cell below this code.
image_chosen = choose.files(
default = "",
caption = "Select The Image File (in PNG format) to Insert",
multi = TRUE, filters = Filters,
index = nrow(Filters)
)
chosen_image_name = basename(image_chosen)
#chosen_image_name # uncomment this line to show the location of the image.
IRdisplay::display_png(file = chosen_image_name) # This line inserts the image.
Then use the standard Jupyter Notebook extension "Hide Input" to hide this codecell, and the "Freeze" NBextension to freeze the codecell so the image stays frozen and the notebook does not try to select and insert a new image everytime the notebook code runs.

Related

iPyWidgets FileUpload not working as I expect it

Im currently trying to load a file in my jupyter notebook, to read it's content.
My Code:
file = widgets.FileUpload(
description= 'Title',
accept='.csv',
multiple=False,
)
box = widgets.HBox([file])
display(box)
# New Cell
print(str(file))
I run the first cell, after that i use the FileUploader to Upload a .csv File before running the second cell.
I expect the output to be a dict containing the files Context like "content" etc.
A Screenshot of a simpler version:
Does anyone know that i'm doing wrong?
I also tried every other idea that came to my mind.
Unfortunately, at the moment file upload widget only retrieves file value if you are using classic Jupyter lab through your browser, and IDEs such as Pycharm and VScode do not support the feature.
Open the notebook on classic Jupyter lab and try to upload the file there, and you will see your code works perfectly.
Try accessing file.value instead.
More info here: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html#file-upload

R officer: Is it possible to print slide as image (jpg, png or other formats)

I have a set of files all with 1 slide created with a custom template created with R officer package
Would like to print these individual slides as jpg or png. Current workflow is to print individual pptx files using the inbuilt print function and converting to image outside of R. The process to convert to image using office "Save As" function or a script is cumbersome.
Is there a plan to add this feature?
The package {doconv} can help you.
See https://cran.r-project.org/web/packages/doconv/readme/README.html
Function to_miniature create an image with your slides. (you will need LibreOffice).
pptx_file <- system.file(package = "doconv", "doc-examples/example.pptx")
to_miniature(pptx_file, width = 1000)

How to copy multiple input cells in Jupyter Notebook

Basically I want to copy (Ctrl+C) only the code portions from multiple cells without also copying the output or the In[1]: and Out[1]:
What is the easiest way to do so?
When you are on a cell in Command mode(blue color mode), simply press Shift + DownArrow or Shift + UpArrow to select multiple cells. Press ctrl + C. And that's it. You have copied your entire selected code at once. It doesn't affect whether you have cell outputs.
Command mode: The Jupyter Notebook has two different keyboard input modes. Edit mode allows you to type code or text into a cell and is indicated by a green cell border. Command mode binds the keyboard to notebook level commands and is indicated by a grey cell border with a blue left margin.
In jupyter you can copy several cells or the content of one cell. If you follow #BenWS comment you can copy several cells, and if you do kernel > restart & clear outputs beforehand you woult not get the [out]. Shortcut is C for copy cell and V shift + V to paste below / above.
However if you intend to copy several cells content, you should merge then before by select them and shift + M and then you can copy paste with ctrl + C.
What worked for me is the following:
update jupyter notebook within a cell using:
pip install -U jupyter notebook
go in command mode by clicking to the left of a cell. If you click inside of a cell, it will be green.
Use shift+down/up to select the cells you want to copy and use ctrl+c
Now the most important one: make sure the jupyter file you want to copy the cells into is ALSO in blue/command mode. If this is not the case, you will copy all the cells into a single cell.
Just do:
File > Export Notebook As > Export Notebook to Asciidoc
and it will be easy to copy paste.
This is what an Asciidoc file looks like:
+*In[ ]:*+
[source, ipython3]
----
import pandas as pd
df = pd.read_csv("data/survey_results_public.csv")
df.tail(10)
df.shape
pd.set_option("display.max_columns", 85)
pd.set_option("display.max_rows", 85)
schema_df = pd.read_csv("data/survey_results_schema.csv")
schema_df.head(10)
----
In the latest version of JupyterLabs:
File > Export Notebook As > Executable Script
Gives you the code as a text file.
Open notebook dir as project in PyCharm, and then open the wanted ipynb file, select and copy all the source code, past into notepad++, replace "\r\n#%%\r\n\r\n" by null with extended search mode.
For jupyterlab after Shift + UpArrow or Shift + select with mouse on multiple cells. Right click on cells for copy(C) and paste(P).

Inserting a Link to a Webpage in an IPython Notebook

How is this done? I'd like to have the link be in a markdown cell.
For visual learners:
[blue_text](url_here)
In case it is not a markdown cell, that is with what I went:
from IPython.core.display import display, HTML
display(HTML("""text"""))
Just another tip, using magic expression.
%%html
Showing Text
Improved. Thanks to the comment of calocedrus.
Here is the code I use in my python notebook when I want to insert a link to a webpage inside a markdown cell (in a python notebook).
[Clickable_visible_hyperlink](Hidden_landing_URL)
--note Here is the clickable hyperlink, you can change the value
This might help too, if you're looking to display a link programmatically.
from IPython.display import display, Markdown
display(Markdown("[google](https://www.google.com)"))
I also tried
display(HTML("""<a href="https://www.google.com>google</a>"""))
But somehow I was getting the object printed out, instead of the rendered version.
For programming in R, do the following when using Jupyter Notebook or Jupyter Lab - (using the R kernel). These steps will display a web link and an image in a Notebook markdown cell. The following shows a real-life example of some study notes using Jupyter Lab and R.
First open a markdown cell in Jupyter - can be a new markdown cell or an existing markdown cell. Then copy and paste the actual web address into a markdown cell. This will provide an active link to that website from the Notebook.
Step 2, from that website, copy the image that you want to view in the Notebook. This image should be in a standard image format (.png, .jpg, etc ). Paste this image into the same folder on the computer where the Jupyter notebook file is located. Note: if the image is later deemed too large or small, then resize using any graphics software available - and then save the changed image into this same folder. Note: it is important to know the name of this image file.
Next, paste the name of the image file between the quotation marks in the following code: . If this file in not within your existing jupyter notebook working directory, then a path to the image file will need to be placed inside the quotation marks.
Step 3, also included is an example of the line of code (also used in Notebook markdown cell) to create colored text in markdown cells. In this line of code, the double ## character results in the second largest font being used in Jupyter. Smaller text using more of these characters - with #### being the smallest. One # results in the largest font output.
Last, be sure to close and run the markdown cell to view the output. The code for the markdown cell follows, and further below shows the output from the Notebook.
Code in Markdown cell:
"https://www.tensorflow.org/images/colab_logo_32px.png" # link to website
<img src="tidyflow.png" /> # The image file (This path is the same folder as Notebook file)
## <font color = cyan> Some Colored Text in Notebook Markdown Cell </font> # colored text
Output:

how to upload a text file into ipython notebook

I want a whole file as a text file instead of just a cell in IPython notebook.
I write some codes in IPython notebook and now I want to test them ,so I tried to upload some text file into IPython notebook as the raw data.But the files' extension are always ".ipynb" and the format of the text files have changed so my code can't read it correctly.
How could I upload a text file into Ipython notebook? thanks in advance
Can you be more elaborate?
Are you trying to read data out of the text file? In that case regular python open and read functions can get you the data into variables. You can even do Unix "cat" the contents of the file in a variable:
var=!cat file
Or are you trying to get the text in the ipynb files as content in an input cell so you can edit them and run them? The ipynb files are usually created by the notebook engine and they are JSON formatted. So if you just copy paste the text from them into a cell, they won't be usable as is. Use some JSON parsing to interpret their content. They are not regular python code so "%run file" wont work to get their code executed by ipython.
Elaborating the use cases may help others to give solutions...
The following lines open a text file and display the content in a jupyter notebook cell.
text_file = open(full/path/to/text/file)
file_content = text_file.read()
print(file_content)
text_file.close()
I hope this helps.
It may be late, you can do this
%%pycat full/path/to/text/file

Resources