Software Notes

Solutions to some programming problems

Useful links/commands
https://github.com/joshkatz/r-script NodeJS to/from R
https://rstudio.github.io/promises/articles/casestudy.html async
https://gist.github.com/wch/5436415/ Shiny example app with dynamic number of plots

If you want to actually browse the environment of a stack frame interactively, use recover() to select a frame.
options(encoding = "UTF-8")
sudo gedit shiny-server.conf
sudo service shiny-server restart

RStudio does not invoke the debugger if it looks like none of your own code is on the stack.
If you find that this is excluding an error you want to catch,
go to Tools -> Global Options and uncheck “Use debug error handler only when my code contains errors”.

Installing missing packages
pkgs <- c("shiny", "dplyr", "treemap", "gridBase")
pkgs_needed <- pkgs[!(pkgs %in% installed.packages()[,"Package"])]
if(length(pkgs_needed)) install.packages(pkgs_needed)
## Run app from Github repo

use installr package to upgrade R
- updateR(TRUE) function which updates R runtime to a new version and also its packages. (ok in Rstudio)
- The function copy.packages.between.libraries to copy files from the old library to the new one. And it doesn't copy fundamental packages as MASS, methods, utils, ...
copy.packages.between.libraries(from='C:\\Users\\user1\\Documents\\R\\win-library\\3.5', to='C:\\Users\\user1\\Documents\\R\\win-library\\3.6') =didn't work

Error: Failed to install 'blotter' from GitHub
(converted from warning) incomplete final line found on 'C:\Users\me\Documents\.Rprofile'
> add empty line at end of .Rprofile

replace current lib with older
.libPaths() - see where your libs are
C:\Users\user1\Documents\R\win-library\3.x > new folder with same lib name (rename current) > copy older filesr code source > There were 12 warnings (use warnings() to see them) > warnings() > Unknown or uninitialised column: 'Date'.
>> Session > Clear Workspace... > fixed
also diagnostics off - https://stackoverflow.com/questions/39041115/fixing-a-multiple-warning-unknown-column

Deploy on shinyapps.io and others
rsconnect::setAccountInfo(name='urname', token='ttt', secret='xxx')
rsconnect::deployApp('C:/Projects/R/urproj') # must have app.R file

Shiny downloadButton Disable "target='_blank" tag
> https://community.rstudio.com/t/downloadbutton-disable-target-blank-tag/6605

rstudio "error creating notebook: cannot open the connection"
> open a Project

compile library source code
> open any R-project ? Project Options : Build Tools: set the folder where the source files are
> Make (somewhere on right top side of Studio..) > "Build Binary" command > will create ZIP file > install BIN package in Studio
(or Open Project to be built from foldr > Build > tar.gz file - needs Rtools.exe installed on computer

when reloading newly compiled library: "quantmod.rdb is corrupt"
> restart R session

Error in order(order_by)[[n]] : subscript out of bounds  when we run: bt.run(bten, trade.summary=F, silent=TRUE)
Called from: nth(x, -1L, order_by = order_by, default = default)
> nth is namespace:dplyr, called by 'last()' also namespace:dplyr in bt.run which relies on base::last() !

Error in EMA(c(NA, NA, NA, NA, NA,  
> number of non-NA values in column(s) 1 > decrease parameter n or replace NAs with data

Read Excel files
https://gist.github.com/schaunwheeler/5825002/raw/3526a15b032c06392740e20b6c9a179add2cee49/xlsxToR.r XLSX
wb = xlsxToR("C:\\Users\\user1\\Downloads\\Ten-Symbol-Ten-Years.xlsx",keep_sheets=NULL, header=TRUE)
|++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed = 47s

'c:/Rtools/mingw_64/bin/g++: not found' cannot compile = wrong folder
> https://github.com/stan-dev/rstan/issues/467
> cat('Sys.setenv(BINPREF = "C:/Util/Rtools/mingw_$(WIN)/bin/")', file = file.path(Sys.getenv("HOME"), ".Rprofile"), sep="\n", append = TRUE)

unable to find an inherited method for function ‘span’ for signature ‘"shiny.tag"’
> was using geosphere::span instead of htmltools::span

# RS-DBI driver: cannot allocate a new connection -- maximum of 16 connections already opened
> lapply(dbListConnections(PostgreSQL()), dbDisconnect)

cannot remove prior installation of package
> stop ALL R sessions > Sys.getenv("R_LIBS_USER") > go to pckgs folder > cmd > del /f filename

Warning: Error in [.data.frame: undefined columns selected
> bad data.frame in chart_Series or add_TA

quantstrat applyStrategy 'Transactions must be added in order'
> .blotter <- new.env(); .strategy <- new.env()
object '.blotter' not found
# https://stat.ethz.ch/pipermail/r-sig-finance/2012q2/010169.html
> if (!exists('.blotter')) .blotter <- new.env()

blotter: Warning in getInstrument(Symbol) : instrument BCOR not found, please create it first.
> stock(symbols, currency="USD")

Error in curl::curl_fetch_memory(url, handle = handle) : Recv failure: Connection was reset
> was HTTP, replace by HTTPS

> You could do it with a plugin like this one https://github.com/rstudio/dygraphs/issues/63
similar to this http://dygraphs.com/tests/visibility.html

r layout 'invalid graphics state'
> dev.off()

need finite 'ylim' values
> In plot1, change y = range(x$VALUE) to y = range(x$VALUE, na.rm=TRUE) to remove NA missing values.

Error in plot.new() : figure margins too large
> graphics.off()
par("mar") #> [1] 5.1 4.1 4.1 2.1
"Error in plot.new() : figure margins too large". To avoid such errors you can first check par("mar") output. You should be getting:
[1] 5.1 4.1 4.1 2.1
To change that write:
par(mar=c(1,1,1,1)) This should rectify the error. Or else you can change the values accordingly.

after restart R: Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
> plot.new()

Warning: Error in xy.coords: 'x' and 'y' lengths differ
> set Sys.envir(TZ='UTC')

rselenium not resolved from current namespace (subprocess)
> restart R

DOM element attributes in Shiny

$('html').on('DOMNodeInserted', 'iframe', function() {
  alert("Fire in the hole!");
  # Getting and setting CSS properties
  $(this).css('background');                  # get
  $(this).css('background-color', '#330000'); # set
  # Getting elements attributes
  $(this).attr('frameborder');      # get
  $(this).attr('frameborder', '0'); # set



document.getElementsByName('mona')[i].onchange = fchange;
document.getElementsByName('mona')[i].addEventListener ("onchange", fchange, false);

LEAFLET Commands
JSON.stringify(table.schema, null, 4));
mlst.splice( mlst.indexOf(sel.value), 1 );
fetch(tileUrl.url, {mode: 'no-cors'}) // avoid CORS errors, but get empty tiles
alert ("Error: " + (e.stack || e));
https://flaviocopes.com/javascript-dates/ dates!

leaflet 'TypeError: Cannot read property '_controlCorners' of undefined
> control defined before map added

Use onEachFeature for stuff you want to get done after the leaflet layers (markers/lines/polygons) have been instantiated.
> otherwise use pointToLayer

check for hasControl:
var customControl = new L.Control.Custom();
console.log(customControl._map); // undefined
console.log(customControl._map); // returns map instance

Uncaught TypeError: Cannot read property '_leaflet_id' of undefined
> expects a Leaflet control but gets something else

http://web.archive.org/web/20160326164021/http://lyzidiamond.com/posts/osgeo-august-meeting/ Adding GeoJSON to Leaflet with Link Relations
https://demo.mapsmarker.com/features/google-maps-import/ https://wordpress.org/plugins/leaflet-maps-marker/

https://gist.github.com/parmentf/035de27d6ed1dce0b36a for github repos

send email from static page: https://formspree.io/

efficient JS:

Error in google chart > CSV > Cannot read property 'getTime' of null
> last line is empty > delete

how to debug Chrome Extension Popups:
> https://stackoverflow.com/questions/5039875/debug-popup-html-of-a-chrome-extension
1.Click your popup button to see the webpage (the popup window itself).
2.Right-click in the window and select Inspect element
3.The Chrome Debugger window comes up with the right context, but you've already missed your breakpoints and debugger statements.
4.Click on the Console part of the debugger and type: location.reload(true) and press enter.
Now your breakpoints are hit! Great way to reload changed scripts without revisiting the Extension page.


useful DOS batch commands
list of commands http://ss64.com/nt/
xcopy https://www.lifewire.com/xcopy-command-2618103

net view \\		remote PC by IP
netstat -anob			Active Connections
dir /s /b /o:n /ad > f.txt	subfolders
dir /s /o:n /a *.mp4		all mp4 in tree
pnputil /enum-drivers		drivers list

check unresponsive Win 10 NMI_HARDWARE_FAILURE
"Remove any hardware or drivers that have been recently installed. Make sure that all memory modules are of the same type."
3 reboots > automatic repair > troubleshoot > advanced options > command prompt / startup options : 4=safe mode / startup repair > diagnosing
sfc /scannow
chkdsk /r
mdsched.exe > restart and check RAM memory
>> was the video card failing

windows 10 taskbar pop up shows but cannot click
http://www.thewindowsclub.com/taskbar-not-working-in-windows-10 tray frozen
> find and restart Win Explorer in Task Manager by right-click > Restart (not File > Run > explorer.exe)

Windows Explorer - how to disable the Preview pane
> Alt-P couple times

change default application for BAT file extension (File Type Associations)
> Win.Explorer > right-click your BAT file > select “Open with” > click on Choose another app > (opt) Look for another app on this PC
or > WIN+X hotkey > Settings > Apps > default apps > click Choose default apps by file type > but choice from Win.Store only (!!?)

fix camera
> Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows Media Foundation\Platform :
> set EnableFrameServerMode (DWORD) = 0

disable calendar notifications windows 10
> open Settings > System > Notifications & actions > Show notifications from these apps > toggle On to Off

gpedit.msc > User Configuration > Administrative Templates > Windows Components > File Explorer
Click the Settings heading to alphabetize the list, then scroll down and find, then double-click 'Turn off the caching of thumbnails in hidden thumbs.db files' > Enable > reboot
see also C: drive cleanup below

C: drive cleanup
> Windows Explorer > C: > Properties > General > Disk Cleanup > Clean system files > select Windows Update, etc

recent files
press Windows Key+R > Enter shell:recent > hit Return

> Win.Explorer > My PC > rightClick > Add Network Location > ftp://xxx

auto download images as JFIF
> Open regedit > HKEY_CLASSES_ROOT > MIME > DATABASE > Content type > image/jpeg > change "extension" value to .jpg

Macrium reflect service cannot be killed
> Services > set it to Manual

1st: set Chromecast thru Google Home from Android phone, 5MHz WiFi network, Bluetooth on
♦ cast to TV without subs:
plug Chromecast in TV & power > LED white > on PC run VLC > Playback > Renderer > choose TV
    Search > ' Media Streaming Options' > All Ratings
♦ cast to TV with subtitles:
PC Chrome > Menu > Cast.. > Cast To > select source = Desktop > select 'Family Room'
https://www.sodaplayer.com/  see also AirFlow

how to Turn Off the Mail Background App ?
> Users can disable Mail running as a background app via Settings. To do that, press the Windows key + S hotkey.
Enter the keyword ‘background app’ in the search box.
Click Background apps to open the Settings window, then turn off the Mail and Calendar app setting
see also https://windowsreport.com/how-to-disable-mail-app/

desktop window manager high cpu windows 10
Microsoft content background task host - high CPU
> The path is: C:\Windows\System32\backgroundTaskHost.exe

Google Video Support Plugin Installer + Win installer = High CPU
> Found "Google Video Support Plugin" installed on Windows 10 laptop after checking Control Panel/Programs due to slow startup.
logs in C:\Users\guest\AppData\Local\Temp
> regedit delete for Phone Plugin 5.41.3.

Replacing antivirus Avast with Kaspersky without uninstalling Avast
install Kaspersky Security Cloud > get "Error 27300. Error installing driver klhk.sys_x64"
> Avast Free Antivirus > Menu > Settings > Components > turn off all shields >
    > Troubleshooting > uncheck all (rootkit_scan, anti-rootkit, etc) > OK