Last updated: 2023-10-08
Checks: 7 0
Knit directory: Pandas-30-R/
This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20221023) was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 066a00f. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish or
wflow_git_commit). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: dev/
Ignored: renv/
Untracked files:
Untracked: mydatabase.db
Unstaged changes:
Modified: renv.lock
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/SQL1-split-part.Rmd) and
HTML (docs/SQL1-split-part.html) files. If you’ve
configured a remote Git repository (see ?wflow_git_remote),
click on the hyperlinks in the table below to view the files as they
were in that past version.
| File | Version | Author | Date | Message |
|---|---|---|---|---|
| Rmd | 066a00f | Mena WANG | 2023-10-08 | first SQL tip |
Use RSQLite library allows us to run SQL queries in R
environment. We also have the option to create an in-memory database for
quick experimentation.
# Load the RSQLite library to use SQL in R
library(RSQLite)
# Create a SQLite database
# con <- dbConnect(SQLite(), dbname = "mydatabase.db")
# Create an SQLite in-memory database connection
con <- dbConnect(SQLite(), dbname = ":memory:")
Sometimes we might want to use SQL query to extract specific element
of a field. On platforms such as Snowflake, I found
SPLIT_PART very helpful in such scenarios. For example, in
the query below, SPLIT_PART is used to extract the domain
name from a URL by first splitting the URL using ‘://’ as a delimiter
and then further splitting the result using ‘/’ as a delimiter to obtain
the domain name.
But the SPLIT_PART function is not supported in RSQLite,
so here in the demo I will show both the SPLIT_PART way and the
alternative that works in RSQLite.
# Create a dummy table with sample data
dbWriteTable(con, "url_demo", data.frame(
id = 1:4,
url = c(
'https://www.example.com/page1',
'https://www.example.com/page2',
'https://www.openai.com/research/',
'https://www.github.com/user/repo'
)
))
# Query that would have worked in Snowflake
query <- "
SELECT
id,
url,
SPLIT_PART(SPLIT_PART(url, '://', 2), '/', 1) AS domain_name
FROM
url_demo;
"
# Define the SQL query to extract domain names
query <- "
SELECT
id,
url,
SUBSTR(url, INSTR(url, '://') + 3, INSTR(SUBSTR(url, INSTR(url, '://') + 3), '/') - 1) AS domain_name
FROM
url_demo;
"
# Execute the SQL query
result <- dbGetQuery(con, query)
# Print the result
print(result)
id url domain_name
1 1 https://www.example.com/page1 www.example.com
2 2 https://www.example.com/page2 www.example.com
3 3 https://www.openai.com/research/ www.openai.com
4 4 https://www.github.com/user/repo www.github.com
sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.utf8 LC_CTYPE=English_Australia.utf8
[3] LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C
[5] LC_TIME=English_Australia.utf8
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] RSQLite_2.3.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.11 pillar_1.9.0 compiler_4.2.1 bslib_0.5.0
[5] later_1.3.1 jquerylib_0.1.4 git2r_0.32.0 workflowr_1.7.0
[9] tools_4.2.1 bit_4.0.5 digest_0.6.33 memoise_2.0.1
[13] jsonlite_1.8.7 evaluate_0.21 lifecycle_1.0.3 tibble_3.2.1
[17] pkgconfig_2.0.3 rlang_1.1.1 cli_3.6.1 DBI_1.1.3
[21] rstudioapi_0.15.0 yaml_2.3.7 xfun_0.39 fastmap_1.1.1
[25] stringr_1.5.0 knitr_1.43 fs_1.6.2 vctrs_0.6.3
[29] sass_0.4.7 bit64_4.0.5 rprojroot_2.0.3 glue_1.6.2
[33] R6_2.5.1 fansi_1.0.4 rmarkdown_2.23 blob_1.2.4
[37] magrittr_2.0.3 whisker_0.4.1 promises_1.2.0.1 htmltools_0.5.5
[41] renv_1.0.0 httpuv_1.6.11 utf8_1.2.3 stringi_1.7.12
[45] cachem_1.0.8