I load the necessary library (ncdf4) and directed the right path but I don't know why it is having permission denied to open it:
nc = nc_open('temp.nc')
I'm not familiar with {ncdf4}, but as in the cited thread I would start with looking for obvious file problems.
What happens if you run this?
readLines('temp.nc')
If you get a result like
1] "CDF\001" "" "_FillValue" "Population" ""
[6] ""
Warning messages:
1: In readLines("temp.nc") :
line 1 appears to contain an embedded nul
that suggests the failure is at the ncdf4 level, whereas if you get the same Permission denied error (or can't open the connection) that suggests it's at the OS level, e.g. the path is incorrect or something.
Also, what is your Operating System, and version of R?