{"id":15955,"date":"2017-02-02T14:42:00","date_gmt":"2017-02-02T22:42:00","guid":{"rendered":"https:\/\/devwww.3cloudsolutions.com\/post\/importing-and-exporting-getting-data-into-and-out-of-r-2\/"},"modified":"2024-01-05T14:37:35","modified_gmt":"2024-01-05T22:37:35","slug":"importing-and-exporting-getting-data-into-and-out-of-r","status":"publish","type":"post","link":"https:\/\/3cloudsolutions.com\/resources\/importing-and-exporting-getting-data-into-and-out-of-r\/","title":{"rendered":"ImpoRting and ExpoRting: Getting Data Into and Out of R"},"content":{"rendered":"<p>In any organization, data is housed in many locations and in many formats. Nowadays, many business analytics tools have the native ability to import from almost any data source imaginable. For example, <a href=\"https:\/\/powerbi.microsoft.com\">Microsoft Power BI<\/a> has the ability to get data from over 60 different sources, including many common file types and more esoteric data storage software.<\/p>\n<div class=\"fluid-row\"><img loading=\"lazy\" decoding=\"async\" style=\"margin-right: auto; margin-left: auto; display: block;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/iStock-607969272edited.jpg\" alt=\"iStock-607969272edited.jpg\" width=\"805\" height=\"487\" \/><\/div>\n<div class=\"fluid-row\">From flat files to databases to proprietary file formats, we&#8217;ll take a look at how simple it is to use these data sources in R. Many times, as you\u2019ll see, all it takes is the installation and use of a simple package. This includes many proprietary formats such as SAS and Microsoft Excel. The steps below will take you through each format and help you to master importing and exporting data!<\/div>\n<div class=\"fluid-row\"><\/div>\n<div id=\"reading-and-writing-common-flat-files\" class=\"section level2\">\n<h2>Reading and Writing Common Flat Files<\/h2>\n<h3>Base R<\/h3>\n<p>R comes equipped with the ability to read and write many common text-based flat files including <span style=\"background-color: #f6f5f4;\"><code>.csv<\/code><\/span> files, <span style=\"background-color: #f6f5f4;\"><code>.tsv<\/code><\/span> files, and more. For example, let\u2019s say I have a file called <span style=\"background-color: #f6f5f4;\"><code>mydata.csv<\/code><\/span> in my data folder. R has a function, <span style=\"background-color: #f6f5f4;\"><code>read.table<\/code><\/span>, and its children functions, <code><span style=\"background-color: #f6f5f4;\">read.csv<\/span><\/code>, and <span style=\"background-color: #f6f5f4;\"><code>read.delim<\/code><\/span>.<\/p>\n<div class=\"step_num\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code>#read.table works in the following manner: \"read.table(file, header = FALSE, sep = \"\", quote = \"\"'\",...\".\r\ncsvinput &lt;- read.table(\"data\/mydata.csv\",header = TRUE, sep = \",\",quote = \"\"\")\r\n\r\n#Alternatively, using \"read.csv\"\" will assume the separator is a comma, which may save some time.\r\n#We used the quote parameter to tell R that there are some strings in the file surrounded by double quotes.<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"clearall\">Now, let\u2019s say that you have done some data manipulation and calculations and you want to export your data back to your data folder. This is just as simple as reading things in.<\/div>\n<div class=\"clearall\">\n<div class=\"step_num\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code>#write.table works in the following manner: \"write.table(x, file = \"\", col.names = TRUE, append = FALSE, quote = TRUE, sep = \" \",...\".\r\n#Alternatively, using \"write.csv\"\" will assume the separator is a comma, which may save some time.\r\n#We use the append parameter to tell R to overwrite the file rather than just tack on rows to the end of the old version.\r\nwrite.table(input,file=\"data\/output.csv\",col.names = TRUE, append = FALSE, quote = TRUE,  sep = \",\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p style=\"text-align: right;\">[<a href=\"http:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/utils\/html\/read.table.html\">read.table documentation<\/a>]<\/p>\n<h3>readr<\/h3>\n<p>In the realm of <a href=\"http:\/\/tidyverse.org\/\">tidyverse<\/a>, there is a package known as <span style=\"background-color: #f6f5f4;\"><code>readr<\/code><\/span> that can handle importing flat files, too. This package has multiple functions to handle different flat file types.\u00a0The <span style=\"background-color: #f6f5f4;\"><code>readr<\/code><\/span> package\u00a0runs a bit faster than the base R functions and handles factors and dates a bit better, too.\u00a0It also\u00a0supports seven file formats with seven <span style=\"background-color: #f6f5f4;\"><code>read_<\/code><\/span> functions:<\/p>\n<ul>\n<li><span style=\"background-color: #f6f5f4;\"><code>read_csv()<\/code><\/span>: comma-separated (CSV) files<\/li>\n<li><span style=\"background-color: #f6f5f4;\"><code>read_tsv()<\/code><\/span>: tab-separated files<\/li>\n<li><span style=\"background-color: #f6f5f4;\"><code>read_delim()<\/code><\/span>: general delimited files<\/li>\n<li><span style=\"background-color: #f6f5f4;\"><code>read_fwf()<\/code><\/span>: fixed-width files<\/li>\n<li><span style=\"background-color: #f6f5f4;\"><code>read_table()<\/code><\/span>: tabular files where\u00a0columns are separated by white-space<\/li>\n<li><span style=\"background-color: #f6f5f4;\"><code>read_log()<\/code><\/span>: web log files<\/li>\n<\/ul>\n<div class=\"clearall\">\n<div class=\"step_num\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"tidyverse\") or #install.packages(\"readr\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">readr<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#All of the readr functions work in this way: readr_csv(\"filename\").<\/span>\r\n\r\n<span class=\"identifier\">input<\/span> <span class=\"operator\">&lt;-<\/span> <span class=\"identifier\">read_csv<\/span><span class=\"paren\">(<\/span><span class=\"string\">\"mydata.csv\"<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#readr assumes the filetype and delimiter by the function you choose.<\/span>\r\n<span class=\"comment\">#For example, when you use readr_tsv, the package assumes it's looking for a .tsv file and that the delimiter is a \"t\".<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p style=\"text-align: right;\">[<a href=\"http:\/\/readr.tidyverse.org\/\">readr package documentation<\/a>]<\/p>\n<\/div>\n<div id=\"excel-ling-with-importing-and-exporting\" class=\"section level2\">\n<h2>Excel-ling with Importing and Exporting<\/h2>\n<p>Similar to how we just imported a <span style=\"background-color: #f6f5f4;\"><code>.csv<\/code><\/span>, we can use almost identical functions to the previous section to import Microsoft <span style=\"background-color: #f6f5f4;\"><code>.xls<\/code><\/span> and <span style=\"background-color: #f6f5f4;\"><code>.xlsx<\/code><\/span> Excel files. There are many packages in the R universe that can do this. Here, we&#8217;ll take a look at two: <span style=\"background-color: #f6f5f4;\"><code>openxlsx<\/code><\/span> and <span style=\"background-color: #f6f5f4;\"><code>readxl<\/code><\/span>. First, you\u2019ll need to install one of the packages. After it&#8217;s installed, it is\u00a0practically the same syntax as before.<\/p>\n<h3>openxlsx<\/h3>\n<div class=\"step_num\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"openxlsx\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">openxlsx<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#read.xlsx works in the following manner: \"read.xlsx(xlsxFile, sheet = 1, startRow = 1, colNames = TRUE, detectDates = FALSE,...)\".<\/span>\r\n<span class=\"identifier\">input<\/span> <span class=\"operator\">&lt;-<\/span> <span class=\"identifier\">read.xlsx<\/span><span class=\"paren\">(<\/span><span class=\"string\">\"data\/mydata.xlsx\"<\/span><span class=\"paren\">)<\/span>\r\n\r\n<span class=\"comment\">#You can use the detectDates parameter to tell R that there are dates in the file and to convert them to date strings rather than numbers.<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>After you work with the imported data, writing back to Excel files is also possible in the same manner as before.<\/p>\n<div class=\"step_num\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">openxlsx<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#write.xlsx works in the following manner: \"write.xlsx(x, file, asTable = FALSE,...)\".<\/span>\r\n<span class=\"identifier\">write.xlsx<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">input<\/span>,<span class=\"string\">\"data\/output.xlsx\"<\/span><span class=\"paren\">)<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p align=\"right\">[<a href=\"https:\/\/cran.r-project.org\/web\/packages\/openxlsx\/openxlsx.pdf\">openxlsx package documentation<\/a>]<\/p>\n<h3>readxl<\/h3>\n<div class=\"step_num\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"readxl\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">readxl<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#readxl works the same way as the openxlsx package: read_excel(\"filename\").<\/span>\r\n<span class=\"identifier\">input<\/span> <span class=\"operator\">&lt;-<\/span> <span class=\"identifier\">read_excel<\/span><span class=\"paren\">(<\/span><span class=\"string\">\"data\/mydata.xlsx\"<\/span><span class=\"paren\">)<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p align=\"right\">[<a href=\"https:\/\/cran.r-project.org\/web\/packages\/readxl\/readxl.pdf\">realxl package documentation<\/a>]<\/p>\n<\/div>\n<h2 style=\"display: inline !important; background-color: transparent;\">But What About My SAS Files?<\/h2>\n<p style=\"background-color: transparent;\">Fear not! There are plenty of packages in the R universe to read in your SAS <span style=\"background-color: #f6f5f4;\"><code>.sasb7dat<\/code><\/span> files and other statistical software files as well. To start, the <span style=\"background-color: #f6f5f4;\"><code>sasb7bdat<\/code><\/span> package will do the trick.<\/p>\n<h3 style=\"background-color: transparent;\">sasb7dat<\/h3>\n<div class=\"step_num\" style=\"background-color: transparent;\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"sas7bdat\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">sas7bdat<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#read.sas7bdat works in the following manner: \"read.sas7bdat(filepath)\".<\/span>\r\n<span class=\"identifier\">input<\/span> <span class=\"operator\">&lt;-<\/span>  <span class=\"identifier\">read.sas7bdat<\/span><span class=\"paren\">(<\/span><span class=\"string\">\"data\/mydata.sas7bdat\"<\/span><span class=\"paren\">)<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p style=\"background-color: transparent;\" align=\"right\">[<a href=\"ftp:\/\/cran.r-project.org\/pub\/R\/web\/packages\/sas7bdat\/sas7bdat.pdf\/\">sas7bdat package documentation<\/a>]<\/p>\n<h3 style=\"background-color: transparent;\">haven<\/h3>\n<p>The <a href=\"http:\/\/tidyverse.org\/\">tidyverse<\/a> realm has a package to open SAS, SPSS, and\u00a0Stata data as well. This package is known as <span style=\"background-color: #f6f5f4;\"><code>haven<\/code><\/span>.<\/p>\n<div class=\"step_num\" style=\"background-color: transparent;\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"haven\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">haven<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#read_sas works in the following manner: read_sas(\"filepath\");.<\/span>\r\n<span class=\"identifier\">input<\/span> <span class=\"operator\">&lt;-<\/span>  <span class=\"identifier\">read_sas<\/span><span class=\"paren\">(<\/span><span class=\"string\">\"data\/mydata.sas7bdat\"<\/span><span class=\"paren\">)<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <span style=\"background-color: #f6f5f4;\"><code>haven<\/code><\/span> package can also write SAS files:<\/p>\n<div class=\"step_num\" style=\"background-color: transparent;\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">haven<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"identifier\">write_sas<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">input<\/span>, <span class=\"string\">\"output.sas7bdat\"<\/span><span class=\"paren\">)<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p style=\"text-align: right;\">[<a href=\"http:\/\/haven.tidyverse.org\/\">haven package documentation<\/a>]<\/p>\n<p>Remember that the <span style=\"background-color: #f6f5f4;\"><code>haven<\/code><\/span> package works on SAS, SPSS, and Stata. So, this may be easier than installing different packages for each different statistical software&#8217;s dataset. Currently, <span style=\"background-color: #f6f5f4;\"><code>haven<\/code><\/span> supports:<\/p>\n<ul>\n<li>SAS: <span style=\"background-color: #f6f5f4;\"><code>read_sas()<\/code><\/span> reads <span style=\"background-color: #f6f5f4;\"><code>.sas7bdat<\/code><\/span>\u00a0plus\u00a0<span style=\"background-color: #f6f5f4;\"><code>.sas7bcat<\/code><\/span> files and <span style=\"background-color: #f6f5f4;\"><code>read_xpt()<\/code><\/span> SAS transport files (version 5 and version 8). <span style=\"background-color: #f6f5f4;\"><code>write_sas()<\/code><\/span> writes <span style=\"background-color: #f6f5f4;\"><code>.sas7bdat<\/code><\/span> files.<\/li>\n<li>SPSS: <span style=\"background-color: #f6f5f4;\"><code>read_sav()<\/code><\/span> reads <span style=\"background-color: #f6f5f4;\"><code>.sav<\/code><\/span> files and <span style=\"background-color: #f6f5f4;\"><code>read_por()<\/code><\/span> reads the older <span style=\"background-color: #f6f5f4;\"><code>.por<\/code><\/span> files. <span style=\"background-color: #f6f5f4;\"><code>write_sav()<\/code><\/span> writes <span style=\"background-color: #f6f5f4;\"><code>.sav<\/code><\/span> files.<\/li>\n<li>Stata: <span style=\"background-color: #f6f5f4;\"><code>read_dta()<\/code><\/span> reads <span style=\"background-color: #f6f5f4;\"><code>.dta<\/code><\/span> files (up to version 14). <span style=\"background-color: #f6f5f4;\"><code>write_dta()<\/code><\/span> writes <span style=\"background-color: #f6f5f4;\"><code>.dta<\/code><\/span> files (versions 8-14).<\/li>\n<\/ul>\n<div id=\"data-in-a-database\" class=\"section level2\" style=\"text-align: left;\">\n<h2>Data in a Database<\/h2>\n<h3>RODBC<\/h3>\n<p>Using the package <span style=\"background-color: #f6f5f4;\"><code>RODBC<\/code><\/span>, we can connect to a SQL-based database (such as <a href=\"https:\/\/www.microsoft.com\/en-us\/sql-server\/sql-server-2016\" target=\"_blank\" rel=\"noopener\">Microsoft SQL Server<\/a>) and run queries against it.<\/p>\n<div class=\"step_num\" style=\"background-color: transparent;\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"RODBC\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">RODBC<\/span><span class=\"paren\">)<\/span>\r\n\r\n<span class=\"comment\">#odbcDriverConnect works in the following manner: \"odbcDriverConnect(\"driver={DB Type}; server=servernamer; database=databasename; trusted_connection=true\", uid = \"username\", pwd = \"password\")\"<\/span>\r\n<span class=\"identifier\">connection<\/span> <span class=\"operator\">&lt;-<\/span> <span class=\"identifier\">odbcDriverConnect<\/span><span class=\"paren\">(<\/span><span class=\"string\">\"driver={SQL Server}; server=mysqlserver; database=finance; trusted_connection=true\"<\/span>, <span class=\"identifier\">uid<\/span> <span class=\"operator\">=<\/span> <span class=\"string\">\"admin\"<\/span>, <span class=\"identifier\">pwd<\/span> <span class=\"operator\">=<\/span> <span class=\"string\">\"password1234\"<\/span><span class=\"paren\">)<\/span>\r\n\r\n<span class=\"comment\">#sqlQuery works in the following manner: \"sqlQuery(odbcDriverConnect String, query)\"<\/span>\r\n<span class=\"identifier\">input<\/span> <span class=\"operator\">&lt;-<\/span> <span class=\"identifier\">sqlQuery<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">connection<\/span>, <span class=\"string\">\"select * from mydatatable\"<\/span><span class=\"paren\">)<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This also works with Azure SQL Server, as well as Microsoft SQL Server on an Azure Virtual Machine. You just have to open each server up to external connections. This can work with Oracle, MySQL,\u00a0and other\u00a0databases as long as your machine has the correct ODBC drivers installed.<\/p>\n<p>You can even write data back to a database. This comes in handy when you\u2019ve changed or added to data (especially in\u00a0predictive analytics). Instead of extracting the data out of R and then loading it into the database, you can do it from within the R console.<\/p>\n<div class=\"step_num\" style=\"background-color: transparent;\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"RODBC\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">RODBC<\/span><span class=\"paren\">)<\/span>\r\n\r\n<span class=\"identifier\">connection<\/span> <span class=\"operator\">&lt;-<\/span> <span class=\"identifier\">odbcDriverConnect<\/span><span class=\"paren\">(<\/span><span class=\"string\">\"driver={SQL Server};server=mysqlserver;database=finance;trusted_connection=true\"<\/span>, <span class=\"identifier\">uid<\/span> <span class=\"operator\">=<\/span> <span class=\"string\">\"admin\"<\/span>, <span class=\"identifier\">pwd<\/span> <span class=\"operator\">=<\/span> <span class=\"string\">\"password1234\"<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#sqlwrite works in the following manner: \"sqlwrite(odbcDriverConnect String, dataframe, tablename=\"table\")\"<\/span>\r\n<span class=\"identifier\">sqlwrite<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">connection<\/span>, <span class=\"identifier\">input<\/span>, <span class=\"identifier\">tablename<\/span><span class=\"operator\">=<\/span><span class=\"string\">\"outputtable\")<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"r\" style=\"text-align: right;\">[<a href=\"https:\/\/cran.r-project.org\/web\/packages\/RODBC\/RODBC.pdf\">RODBC package documentation<\/a>]<\/p>\n<h2 style=\"text-align: left;\">Data Online<\/h2>\n<p style=\"text-align: left;\">Many public datasets live on the web. There\u2019s no need to download these to your local machine when you can just import them directly in R. This is especially useful when you need to reshape or manipulate data before it\u2019s useful to you. So, you can just import the data from the web, do whatever you need to do with it, and then write the useful data to your local machine.<\/p>\n<h3 style=\"text-align: left;\">RCurl<\/h3>\n<p>Using the <span style=\"background-color: #f6f5f4;\"><code>RCurl<\/code><\/span> package, we can connect directly to data using its web address. You will just use the <span style=\"background-color: #f6f5f4;\"><code>url<\/code><\/span> function in combination with any other <code>read.*<\/code> function listed earlier.<\/p>\n<div id=\"data-in-a-database\" class=\"section level2\" style=\"text-align: left;\">\n<div class=\"step_num\" style=\"background-color: transparent;\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"RCurl\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">RCurl<\/span><span class=\"paren\">)<\/span>\r\n\r\n<span class=\"identifier\">input<\/span> <span class=\"operator\">&lt;-<\/span> <span class=\"identifier\">read.csv<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">url<\/span><span class=\"paren\">(<\/span><span class=\"string\">\"https:\/\/www.mywebsite.com\/data\/mydata.csv\"<\/span><span class=\"paren\">)<\/span><span class=\"paren\">)<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p style=\"text-align: right;\">[<a href=\"https:\/\/cran.r-project.org\/web\/packages\/RCurl\/RCurl.pdf\">RCurl package documentation<\/a>]<\/p>\n<h2 style=\"text-align: left;\">Getting Help<\/h2>\n<div id=\"conclusion\" class=\"section level2\">\n<p style=\"text-align: left;\">Some\u00a0apprehension to using R comes from users not understanding how to get data into and out of the system. As you can see, importing and exporting are far from difficult. With just a few, simple (and notably very similar) commands, you can pull in data from any source you like.<\/p>\n<p>There are thousands of packages available for R, many of which can do very similar functions to what have been shown here. These are some of the most popular, but each package has its strength. You can search through the <a href=\"https:\/\/cran.r-project.org\/web\/packages\/\">CRAN package list<\/a> for access to many more packages.<\/p>\n<p>After you install any of the packages referenced here, you can view the documentation by adding a <code>?<\/code> before the package name or function name, as shown below.<\/p>\n<div id=\"data-in-a-database\" class=\"section level2\" style=\"text-align: left;\">\n<div class=\"step_num\" style=\"background-color: transparent;\" aria-label=\"Step 3\">\n<div class=\"codebox\" style=\"width: 100%; overflow: auto; padding-top: 0.5em; padding-bottom: 0.5em; margin-top: 1em; margin-bottom: 0.5em; display: inline-block; background-color: #f6f5f4;\">\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"html5 source-html5\">\n<pre class=\"r\"><code class=\"r\"><span class=\"comment\">#install.packages(\"haven\")<\/span>\r\n<span class=\"keyword\">library<\/span><span class=\"paren\">(<\/span><span class=\"identifier\">haven<\/span><span class=\"paren\">)<\/span>\r\n<span class=\"comment\">#Get help on the entire haven package or list out the functions in the package.<\/span>\r\n?<span class=\"identifier\">haven<\/span>\r\n<span class=\"comment\">#Get help on the syntax and options of the read_sas function.<\/span>\r\n?<span class=\"identifier\">read_sas<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"r\">Still looking for assistance? 3Cloud can help! We can work with you and your team to help you learn more about R.\u00a0<a href=\"\/get-started\/\" target=\"_blank\" rel=\"noopener\">Contact us<\/a>\u00a0today for more information.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In any organization, data is housed in many locations and in many formats. Business analytics tools have the ability to import from almost any data source.<\/p>\n","protected":false},"author":21,"featured_media":14723,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[260],"tags":[319,341],"class_list":["post-15955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai","tag-machine-learning-ai","tag-r","topics-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/15955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/comments?post=15955"}],"version-history":[{"count":0,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/15955\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media\/14723"}],"wp:attachment":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media?parent=15955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/categories?post=15955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/tags?post=15955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}