Helgasoft
Back

Software Notes

Solutions to some programming problems
R

Useful links/commands
https://github.com/stan-dev/rstan/wiki/Install-Rtools-for-Windows
https://github.com/joshkatz/r-script NodeJS to/from R
https://rstudio.github.io/promises/
https://rstudio.github.io/promises/articles/casestudy.html async
https://community.rstudio.com/t/error-handling-in-shiny-async-promise-future/9454
https://gist.github.com/wch/5436415/ Shiny example app with dynamic number of plots
https://www.datasciencecentral.com/profiles/blogs/dummy-errors-when-using-neuralnet-package-in-r

Debugging
https://shiny.rstudio.com/articles/debugging.html
If you want to actually browse the environment of a stack frame interactively, use recover() to select a frame.
https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio
options(shiny.reactlog=TRUE)
options(encoding = "UTF-8")
sudo gedit shiny-server.conf
sudo service shiny-server restart
try http://127.0.0.1:3838/sample-apps/sampdistclt/

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)

use installr package to upgrade R link
- updateR(TRUE) function which updates R runtime to a new version and also its packages. Ok in R GUI and Rstudio.
- see also function copy.packages.between.libraries
- see also this link

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

Unwanted warnings in RStudio
When sourcing/executing R code > There were 12 warnings (use warnings() to see them)
> assign("last.warning", NULL, envir = baseenv())
see also diagnostics off in fixing a multiple warning unknown column

Run app from Github repo
shiny::runGitHub('manolo20/shinytreemap')

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 link
> 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
http://www.milanor.net/blog/read-excel-files-from-r/
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
C:/Users/user1/Documents/R/win-library/3.5

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

Dygraph
https://stackoverflow.com/questions/55361481/show-hide-series-interractively-in-dygraph?rq=1
> 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
par(mar=c(1,1,1,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
https://stackoverflow.com/questions/49668048/get-attribute-of-element-in-rendered-ui-with-javascript-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
});

 

Javascript

events:
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!
https://blog.webkid.io/rarely-used-leaflet-features/
https://gis.stackexchange.com/questions/245621/how-to-label-geojson-points-in-leaflet

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
map.addControl(customControl);
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/

EMOJI
https://gist.github.com/roachhd/1f029bd4b50b8a524f3c
https://gist.github.com/parmentf/035de27d6ed1dce0b36a for github repos
https://www.w3schools.com/charsets/ref_utf_dingbats.asp
https://dev.w3.org/html5/html-author/charref
https://www.webpagefx.com/tools/emoji-cheat-sheet/

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

efficient JS:
https://github.com/google/closure-compiler
https://closure-compiler.appspot.com

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.

Windows

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

net view \\192.168.0.3		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."
https://www.solvusoft.com/en/errors/blue-screen-errors/microsoft-corporation/windows-operating-system/bug-check-0x80-nmi-hardware-failure/
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

thumbs.db
https://www.pcworld.com/article/2999243/windows/manage-thumbs-db-files-in-windows-and-on-the-network.html
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
https://www.howtogeek.com/274573/how-do-you-get-the-all-recent-files-list-back-in-windows-10/
press Windows Key+R > Enter shell:recent > hit Return

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

auto download images as JFIF
https://www.cnet.com/forums/discussions/jpeg-to-jfif-automatically-help/
> 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

Chromecast:
1st: set Chromecast thru Google Home from Android phone, 5MHz WiFi network, Bluetooth on
https://widgets.nest.com/chromecast-setup/
♦ 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
https://www.howtogeek.com/207429/4-ways-to-view-your-laptop-or-desktop%E2%80%99s-screen-on-your-tv/
♦ cast to TV with subtitles:
PC Chrome > Menu > Cast.. > Cast To > select source = Desktop > select 'Family Room'
https://www.howtogeek.com/233810/how-to-watch-local-video-files-on-your-chromecast
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
https://appuals.com/fix-desktop-window-manager-high-cpu-usage-dwm-exe/
Microsoft content background task host - high CPU
> The path is: C:\Windows\System32\backgroundTaskHost.exe

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