How to load a notebook without the outputs? - jupyter-notebook

I mistakenly printed to much to the output during a single cell's execution and now the browser tab completely freezes every time that notebook is opened. I tried restarting ipython and it didn't help (I am guessing that each time it is loaded, also all the chunk of text is loaded with it).
Is there a way to load a notebook with outputs suspended or clear?

One hack if you're desperate: open the .ipynb file, which is a text file. Scroll down to the lengthy cell output and delete it. Of course, you need to be careful that the result is still a valid .ipynb file.

nbstripout is a simple tool that removes all output from a notebook (without needing to open the notebook in your browser).

your code will be saved in the form of JSON. open it with json viewer and carefully delete the unwanted output cell and save it back.

Related

I have issues with saving my script in R x64 on my windows 10 pc, with crashing etc

Every time I work on a script in R I come across the problem, that R isn't able to save my script, although I don't use problematic file names and put an .R in the name. I get an error message where the pathname is displayed, as to show me that there is a problem with that. I tried copy-pasting my script into a new one, but the problem persists with the new script file. Furthermore, when I try to copy the text and paste it after having restarted R (the console), it does not work. The scripts, the original one which has stayed blank, as saving did not work, or a new blank one, stay blank when trying to past text.
I should add that R seems to crash when it tries to save the original script that is not savable. When I close the console, R warns me that I have not saved the script, so I press on save before closing. Then R seems to save it, but ultimately crashes.
Later on, I realized that the copy-pasting does not work on my computer at all. I think this problem only started from the moment on I could not save my script in R.
I would be grateful for support from the community.
I'll have an exam where we need to upload our script, so I really need the saving to work.
Greetings,
J.C.

My R script has been turned entirely into blank spaces. Is this recoverable?

If I knew how to reproduce this then I would, but I have never encountered this before. I saved my work using RStudio as I normally would, saving the progress of writing my R script. But when I opened it the next day, the entire script has been blanked. It still has the same file size as before, but nothing appears when I open it in RStudio. It just has a blank line 1.
If I open the script in Notepad++ I can see that the entire file has NUL characters. What has happened here? Is this recoverable?

How to stop RStudio from rendering nb.html on save of an R Markdown document

RStudio renders mynotebook.nb.html file every time R Markdown document mynotebook.Rmd is saved. This process does not involve running code in chunks, so it is much faster than knitting a notebook into mynotebook.html. However, for large .Rmd documents, saving nb.html files can take a long time, and, unfortunately, it is required to wait for it to finish before one can start using the notebook again and run code in chunks.
Is there a way to configure RStudio to not to create nb.html files on save of an R Markdown document?
I've found out you can delete relevant output entry from the top section of your file. In my case it looks like:
---
title: "Document"
output:
html_notebook: default
---
Which causes creation of .nb.html on every save. If you remove the output tag, file is no longer created automatically. You can still knit to any output file from the Knit menu on the top (or press Ctrl+Shift+K for default. This will run all the chunks again, which may take a while.
You may want to consult this guide book for more information on how YAML tags work. I'm just beginning with them!
Another reason (and solution) might be, that you clicked incidentally on the "Knit on Save" button located just under the Save-to-Disk-Symbol in RStudio. This was in my case the problem. Usually you should be able to save a rmd-file without triggering the knitting process.
So - given this scenario - just uncheck the "Knit on Save" button.

Saving .R script File Using Script

I am using R Studio and I want to save my script (i.e., the upper left panel). However, the only ways that I can find to do it are by either clicking the blue floppy disk icon to save or using the drop down menu File > Save > name.R
Is there any way besides using these shortcuts to save the script to a .R file or is the shortcut the only way?
Thanks.
You can use rstudioapi::documentSave() to save the currently open script file to disk.
From the source documentation, one can see that it can be used in conjunction with the id returned with getActiveDocumentContext()$id to make sure the document saved is the one running the script.
For your intended use, try:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
For future reference, here is the reference manual of rstudioapi:
https://cran.rstudio.com/web/packages/rstudioapi/rstudioapi.pdf
I'm not yet allowed to comment, but this refers to the comment above that this does not work with .rmd files:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
I tried and in Rstudio Version 1.2.5042 it does seem to work.
Every new tab in R(created by ctrl+shift+n) can be independently saved by using ctrl+s in the respective tab. If you intend to rename the file though, you may do it as you would rename any file in windows(goto the file location and single click on the filename). Hope my answer was of some help!

How can I reduce the file size of my iPython notebook?

I have an IPython notebook which is several megabytes big although the code inside is just about 100 lines. I think it is that huge because I load several images inside.
I would like to add this notebook to a git repository. However, I don't want to upload something that big which can easily be generated again.
Is it possible to save just the code of an IPython notebook to reduce its size?
You can try following steps since it worked for me:
Select the "Cell" -> then select "All Outputs" -> There you will find "Clear" option select that.
And then save the file.
This will reduce the size of your file (From MBs to kbs). It will also reduce the time to load the notebook next time you open it in your browser.
As per my understanding this will clear all the output created after execution of the code. Since Notebook is holding code+images+comments in addition to this its also holding the out put in that file therefore it will increase the size of the notebook.
I run into the exact same problem with one of my notebooks, which I solved by changing my df to df.head(5). I did this instead of clearing all outputs as I still wanted to show on GitHub how my code changed data inside the columns in my df.
You also can run !ls -lh in the last cell of your notebook to check size of your notebook before saving. This will give you an idea if you need to clear outputs/replace df with df.head()/remove images in order to reduce the size and be able to save on the GitHub.
Now you generate a simple script linked to the notebook with jupytext which others can rerun.
If you need to keep the images within (because, for example, you are sharing the notebook with someone who does not want to/can not rerun it) you might want to try to reduce the images.
I found this module ipynbcompress which seems to do exactly this, but so far I could not install it.

Resources