from script to package in R

Solution for from script to package in R
is Given Below:

I am dealing with a exercise from the university about Rstudio

I have written a script in R that performs a kriging interpolation based on csv file, that scripts do not contain functions in it , I used some function from several libraries.

The script works well and at the end I got a raster file with the kriging surface and a graph from that raster.

But now I have to create a package from that script, I tried it following some tutorials from internet but it didn’t work I think is because my script is not a function

What should I do in order to create a package from my script?

library(sf)
library(sp)
library(raster)
library(rgdal)
library(gstat)
library(tmap)


#configuring folder for the result

setwd("C:")
dir.create("R_Exercise")
setwd("R_Exercise")
getwd()


#reading csv from google drive

id <- "1YOCFoHkKqkc69YfvdV-lrUThTP_32LHn" # google file ID
read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id))

silver_data = read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id))

#checking imported data
str(silver_data)

#data exploration
silver_data$silver
hist(silver_data$silver, breaks = 10)
summary(silver_data$silver)

#Creating Spatial sf object

silver_2 <- st_as_sf(silver_data, coords = c("Este", "Norte"))

#checking the spatial object
str(silver_2)


#asigning crs from EPSG codes: 9155 SIRGAS-Chile 2016 / UTM zone 19S

silver_2 <- st_set_crs(silver_2, 9155)
st_crs(silver_2)


#ploting silver data


plot(silver_2["silver"], asp = 1, cex = 4*silver_2$silver/max(silver_2$silver), main= "Silver Data")

#Starting to solve the problem find the kriging surface for silver grade

#empirical variogram

varem <- variogram(silver ~ 1, silver_2, cutoff = 5000,width = 200)
plot(varem, plot.numbers = T, asp=1)

#experimental variogram
varex <- vgm(psill = 350, model = "Sph", range = 2000,nugget = 0)
plot(varem, pl = T, model = varex)

#automatic adjustment

varaut <- fit.variogram(varem, varex)
plot(varem, pl = T, model = varaut)

#interpolation

#creating empty grid

emptyr <- raster(as(silver_2, "Spatial"), ncols = 200, nrows = 80)


#cheking empty raster
extent(emptyr)
#resize spatial resolution
res(emptyr)<-c(100,100)


Emptygrid <- rasterToPoints(emptyr, spatial = TRUE)
gridded(Emptygrid) <- TRUE
Emptygrid <- as(Emptygrid, "SpatialPixels")

Emptygrid <- sf::st_as_sf(Emptygrid)

#obtaining kriging
Inter <- krige(silver~1, locations=silver_2, newdata=Emptygrid, model=varaut)
Inter

#ploting some verifications

Inter_sp <- as(Inter, Class = "Spatial")

print(spplot(Inter_sp, "var1.pred", asp=1, main="Prediction for Silver Grade , gr/Ton"))


print(spplot(Inter_sp, "var1.var", col.regions=cm.colors(64), asp=1, main="Prediction variance"))

Inter_sp$coeffvar = sqrt(Inter_sp$var1.var)/Inter_sp$var1.pred


print(spplot(Inter_sp, "coeffvar", asp=1, main="Variation Coefficient"))


#creating final file
r <-raster(Inter_sp)
res(r)<-c(100,100)

img <-rasterize (x = Inter_sp, y = r, field = Inter_sp$var1.pred )
writeRaster(img, 'output.tif', overwrite = TRUE)


result <-raster("output.tif")
plot(result)

#Final Graphic

tm_shape(result) + tm_raster(n=10, palette="RdBu", auto.palette.mapping=FALSE,midpoint = NA, title="Kriging Prediction nSilver grade n (gr/Ton)") + tm_shape(silver_2) + tm_dots(size=0.2) + tm_legend(legend.outside=TRUE)