Combining Wolfram Language and R

This content was created by Guy Kawasaki. This content was uploaded by Guy Kawasaki. This is a YouTube video titled "Combining Wolfram Language and R" created on September 19, 2024, discussing the integration of Wolfram Language with R. The content type suggests it is likely a presentation or tutorial.
Chapters
Hello everyone, and welcome to this webinar about combining Wolfram language and R. So today I'm going to be talking to you about how to use both languages from the same environment. And for most of the webinar, that environment is going to be wufra Mathematica. So just the normal standard notebook environment that you're probably all used to. But we are also going to cover how to use Wolfram language from an R environment. So in our case that will be RStudio, but it could be any R ID that you use. So before I get into the plan for the session and the actual session, I just want to say, if you have any questions, feel free to pop them in the chat. Whether you're watching us on BigMarker or YouTube, you should have a live chat on the right. So feel free to leave any questions there and I will do my best to respond to all the questions after the webinar. So for those of us watching us on BigMarker, there are attendance certificates available. So if you want to get one, just feel free to stick. After the webinar there's a survey where one of the questions is whether you want a certificate or not. So that's. If you fill in that survey, you'll have your certificate sent to you in the coming days or weeks. So with that out of the way, let's get into it. So we're going to be using both Wolfram language and R in the same environment to do data analysis, data visualization. The idea would be to combine the strengths, the strengths of each language to achieve something that we wouldn't be able to achieve using only one language. So to get us started and to. Or just a little plan for the session first, we are going to be starting with a little comparison between Wolfram language and R. Then we'll get straight into the functionality, into how do we actually combine the two together. And then for the second half of the webinar, or maybe even more than half of the webinar, we'll be going through example workflows. Obviously the functionality is important, but if I was just here to tell you the functionality, you can just read the documentation for that. That's not very useful, very good use of your time. So we'll be going through actual example, how do we put that functionality into practice to achieve real things? And we are going to be starting with fairly simple examples at the beginning, where we'll basically just be copy and pasting some R code into our notebooks, seeing how we can transfer some kind of more complicated objects between the two languages. But then as we get near the end, we'll get, we'll move on to some kind of more complicated example, more that are more, how would you say, representative of what you might do in real life. So the idea is to start simple and then build our way up to something more interesting. So that's going to be, yeah, as I said, probably more than half of the webinar. But let's start with a little comparison here. So we have this little table that compares two languages based on some criteria. Not going to go into too much detail at this point, just because I expect that you are both familiar with, at least with one of the languages and probably maybe with both of them. But it is still useful to have a quick recap, to just set the scene for what we're going to be talking about. So in terms of focus, we say that Wolfram language is a platform for general computation. So whether you are a physicist, a mathematician, a social scientist, you should have access to all the analyses that you need in Wolfram language. Whereas R is really more focused on data science and statistics. That covers the standard library that's already integrated in R, but it also covers the external library. So if you look at external resources, most of them are going to be about statistics, even though R is a programming language. So you could technically achieve or do anything you want with a programming language, but obviously it's just the functions that you have available out of the box can be specialized. And in the case of R, they are really targeted towards data science. So in terms of paradigm, we say both languages are multi paradigm, which is true to an extent. Even though there is, for example, from language, it's much easier to do operations on images, for example, or on sound data and things like that. But really both languages are multi paradigm and you can do analysis on different types of data. When we get to standard library and external resources, that's where we start to see big differences between the two languages. So really two different approaches or philosophies. In Wolfram language, the standard library is extensive. So by standard library we just mean the functionality that is available to you without you having to install anything external. So in Wolfram language, as I said, really extensive standard library, we have, I think at this point more than 6,000 functions integrated into the language. So what I would say, what I would recommend my advice to people who are just beginning to use Wolfram language, because I imagine that we are going to have some R users watching us who are maybe interested in the Wolfram language. So My advice for those people would be that if you're trying to write a function to do something, just check the documentation first, because it is very likely that that function already exists, given the huge number of functions that we have, that it's not going to always be the case, but it's always worth checking. I mean, myself, I've had this experience where I had to do something and then I start writing a function and I write a function and then I realized that, oh, it actually existed in the language, so I didn't need to do that. So, yeah, especially compared to R, which has a very kind of minimalistic standard library. The standard library, Wolfram language is extensive, however, when it comes to external resources. So that could be just external libraries, but we're also talking about any resources like tutorials, code bases, repositories, whatever that might be. That's where R really shines. It has a great community, it has a lot of users. It's really popular in areas like academia, which means that a lot of people have developed a lot of great, great resources for R, and those are really extensive. Compared to Wolfram language, which, which also has quite good resource repositories. We have example repositories, function repositories, LLM prompt repositories, and a lot of different things. We have the community forums. But even with all of that, R still has the edge when it comes to external resources. So it's a really. It's a difference in kind of philosophy. With Wolfram language, the idea that the goal really is to have everything in one place, whereas R really relies on the community for its resources, then we move on to computation type. In R, we really only do numerical computation, whereas with Wolfram language we can do a symbolic and numerical. It's not very important for this webinar specifically, but it is a difference. So the other big difference is obviously in the license. So Wolfram language is under a proprietary license, meaning in practice that if you want a license to use the language, you need to buy it, essentially. And with R it is distributed under a free and open source license, specifically GPO v3 and v3, v2 and v3, meaning again in practice that if you want to use it, you just have to download it from the Internet for free and you can start using it. So in terms of key advantages, just to wrap this section up, the main advantage of R is obviously that it's free, it is a big advantage and anyone can start using it right away. They don't need to agree to any. Well, they need to agree to the GPL license. But practically speaking, you can just download it and start using it. You don't need to pay anyone any money. And really it is because of that that R has this existing external resources, because the language being free is what has allowed it in part to really become popular in areas like academia. In terms of key advantages for the Wolfram language, we have speed. So even though they're both interpreted languages, which are, although I should say that Wolfram language has a compiler that you can use if you are interested in those things, but it's more advanced and out of the scope of this webinar. But they're both interpreted languages. But even with that, Wolfram language is much, much faster for the majority of the things that you will do than R. I mean, R has never been really known for its speed. Another advantage for the Wolfram language is its integrated knowledge base. So we're going to see how that becomes useful in one of the examples. Actually, in a few of the examples I think we're going to use the knowledge base. But basically the Wolfram language has access to all the data that is in WolframAlpha. So I'm guessing most of you are familiar with WolframAlpha, but it's this symbolic AI system that is underpinned by a lot of computable data on a lot of different domains, from science to pop culture to so on. As I mentioned, we're going to use that in a bit, so you'll see what I mean if you're not familiar with it. And then finally, as of more recently, we have had LLM integration into mathematical notebooks, which is really helpful, um, especially with, if you are a beginner to intermediate user, it's really helpful to have essentially an AI assistant that lives in your notebook, really helps with writing code, getting explanations for things and so on, even though obviously it's not perfect and, but there is a very good integration with external services. So if you, if you already use OpenAI's API, for example, or Anthropic or Palm or whatever, whichever API you're using, you are able to just, in a single basically interaction, just connect that to your Wolfram notebook and start using it. So that's really helpful. So I think that's it for, for comparing the two languages. We're going to see how some of these differences play out once we get into the actual workflows. For example, we're going to have a workflow where we use the extensive standard library of R to do something without having to rewrite it in Wolfram language. And we're going to use Wolfram language to do some, some calculation much faster than we're going to do than, than it would take us to do it in R. So we'll see how these differences play out a bit later on. But this is just a very kind of brief recap of the two languages. All right, so combining the two languages, obviously there are two ways to combine the language. One is to use who from language from R and the other is to use R from Wolfram language. I mentioned that we're going to be focusing on using R from Wolfram language, but we're actually going to start with doing it the other way around. We are going to open up RStudio and we're going to use something called Wolfram Engine or Wolfram Script. And I'll show you how that works. Actually, I'll just open RStudio now and do that. Give me a second. Okay, so hopefully you should be, oh, this is way too small. Give me a second. Hopefully you should be able to see my RStudio window now where I have a few lines of code. And this is just an R script that I have written. Oh, by the way, I forgot to mention that you have access to all of these files, obviously the main notebook. But all of the files that I will be using during this session, you can access them. There is a link to access them at the top of the chat. So if you are interested in kind of following along, or probably more likely if you want to look at those files after the webinar, feel free to download them now. And that should give you access to all the files, including this little R script which is in the main directory in a folder called Wolfram Script. So this is where this little script lives. And you can open it again in RStudio or you can open it in any IDE that you want to use with R. But RStudio is probably for our Wolfram users that are not that familiar with R. Maybe RStudio is essentially probably the, the mathematic of the R language in, in the sense of it's a very popular front end. It has a lot of nice IDE features and so on. But anyway, to get back to the, to the Wolfram script and how we actually use the Wolfram language from R is that we're going to be using something called Wolfram Script. And Wolfram Script is an application that gives us access to the R evaluator from, from any command line interface, really. So that could be the R terminal. It could be the terminal on your system. Just anything that has access to the show of your system is able to use to call the Wolfram evaluator. So in this case, we'll be using this function called System, which. And the function system does exactly that. It is able to access the command line interface, the shell of our system, and through that it's calling Wolfram Script and it's passing it some expression to be evaluated. So let's go line by line here. Let's start with the first one. We have System, which is going to call, which is going to send the command to our command line interface. We're calling Wolfram Script. This C that you see here stands for code. So we'll be sending Wolfram language code to Wolfram Script for evaluation. And then here on the right side, we have our Wolfram language expression. So in that case, just three plus three. This can be any Wolfram language expression. Could be just a simple calculation like you see here, but it could be just any valid Wolfram language code that you can get the result in kind of numerical or text form. So anyway, let's run that and see what happens. So we are looking at the console here. We're running System Wolfram Script three plus three and we're getting back six. Once we have that result, we can do whatever we want with it. We can assign it to a variable, we can do our R calculations to it, we can just treat it as a result. Now, that works. And that calculation was performed from the Wolfram language evaluator. So it is the same thing as going to a Wolfram mathematical notebook and typing 3 3. So it works, and we did it from R. But as you can already see, this is a very kind of limited approach in the sense that, well, first of all, we are not able to automatically store the result in a variable. So if I do this, if I try to store the evaluation into a variable called A, this is not going to work. So that is already very limiting. So if we type A now, it's going to be zero. That's already very limiting. There are other things, like we're not able to combine Wolfram Language and R code in the same line. So it's a functional interaction, but it's not very useful, so to speak. But in any case, it is a way in which we can call Wolfram Language. However, if you have Wolfram scripts, you are also able to call them from System Wolfram Script. So in this example, I am using a little script called BBC Articles Wolfram Script. And just in Case, if you're not familiar with Wolfram scripts, they are just text files that contain Wolfram language code. Typically those scripts perform just any operation. It could be any Wolfram language code, but typically they save it to a local file and we can really make use of that. So in this example, I have this Wolfram script here, BBC article wls. It's a kind of a simple Wolfram language code that takes all the news articles from the front page of the BBC for today and it puts them into a nice data frame for us that really into a CSV file. It saves everything into a CSV file that we can then import from R and use it in that way. So if you have Wolfram language scripts, that's probably a more kind of fruitful way of using Wolfram language from R, because you can just execute the script and if you know that the script is outputting some files. So, for example, my script, I know that my script is going to output this file called BBC Articles CSV. And since I know that I can then import that file from my R environment and then I can do my operations with it. I'm not going to run this script just because it takes a few. Oh, actually, why not? I can. I can run it. It takes a few minutes to get all the articles from the BBC, but let's try to run it and see if it works. And I can, I can kind of talk in the meantime. This should open. Okay, Yeah, I see it on my other screen. So I can just show you what the script is doing. So it's going to the BBC website and it's iterating through all of the news articles for today. And it's getting things like the headlines, it's getting the author, the date of publication, obviously the article itself. And it's putting all of that into this kind of structured format that we can then use to do our data science on. And I'm not really sure what you can do with this data, but it was just something that came to my mind, I guess you can do here. I think the example I'll just leave this window to the side to do its thing. It should be finished in a bit, but I haven't fully develop this example. But once I import the file into R, I then obviously I save it in a data frame type data frame variable. And then what I do with this data frame variable is that I am adding a new column called Word Count, where I am counting all the words from the variable called article text, which is going to contain text from the Article so you can start building up your data frame. Once you have the base data, you can, you can just do essentially anything that you would do with a normal data frame at this point. Once you get to here, once you get into importing the CSV file, you don't really care that it came from move from language or it came from just any CSV file. But our script is finished. So let's just see what our data looks like. If I just open up the data, you can see that all of the articles here, we have 31 for today. All the articles have category, headline, author, publication date, article, text, and this script. You can see that in some cases it failed to get the actual relevant information. I'm not too sure why, but there's a lot of data cleaning to be done. But again, the main point is once you have the CSV file that was produced by Wolfram language, you can then just use it in R as you would any other CSV file. So I think this wraps up the R side of the story. So as I mentioned, you can use woofram script. It is functional. You can do evaluations, you can do calculations with it. Probably if you're using it, you'll probably be using it with Wolfram script files just to. It really allows you to have everything into one environment. So as you saw, I didn't need to leave RStudio to do all of this. So I just have everything. I'm just calling the script. I know the script is outputting some file, I'm reading it back, I'm doing my stuff with it. Okay, so let's actually move on to our link. Hopefully that was clear. Again, you have access to the files, you can run through them at your own pace. They should all work out of the box. So hopefully it's clear or it will be clear. So let's actually move to our link. Let me just collapse. So our link is the Wolfram application, which allows us to do the opposite of what we've just been doing. So it allows us to use the R language from our Wolfram Mathematica environment. And as you will see, this is a lot more useful. It allows us to do things like store the result of some R calculations into Wolfram language variable. It allows us to say functions, it allows us to combine functions from both languages in the same line of code, essentially. So this is where things get a bit interesting, I would say. But to get us started, I will run through the kind of three main high level functions of R link. So those are revaluate R set and R function. Okay, so let's start with revaluate or external language cell. If you're not familiar with external language cells, they're just the cell type in Woofer Mathematica that you can get, well, in many ways. But for example, if you go to this little plus here, you're able to select, and apologies if this is a bit small, but you're able to select external language input, which opens up this external language cell. And here from the left, you can select which external language you actually want to use. So you can see we support quite a few external languages. Obviously in this case we'll just choose R, and then once you have the cell, you can actually type your R code in it. So here we have what we have something simple like print hel. And now once you evaluate it, this, all of this, everything that is in the external language so obviously gets sent to the R runtime which understands our code, obviously, and then it gives us back the result. So in this case, it's just, we're just printing a simple string. You can also obviously include multiple lines. So if you have code that is more than one line, all you have to do is is wrap it in these curly brackets and then you're just able to type in the code as you would in RStudio, let's say. So here, just to keep with the simple examples, we are defining a variable, simple var. We're assigning it a list, just a vector of three integers, and then we are squaring those and we're printing the result from simple var squared. So this should work. So as I mentioned, there is a function called revaluate and there is an external language cell. And for most intents and purposes, those two are the same. So whether you have an external language cell and you want to see the result of this variable, or whether you use revaluate and you pass in the same exact code, the result is going to be the same. So what is the actual difference between revaluate and the external language cell? Basically, you would use revaluate when you want to store the result of the R calculation into a Wolfram language variable. So here we see the next example. We are using our evaluate again with our variable that we defined here. Oh, and another thing to mention just in passing, is that once you define a variable in R space, like we did here with simplevar squared, that variable persists for your Wolfram kernel session. So you can reuse it from any environment or from any input cell, any external language cell. You don't need to define it every time. So for all intents and purposes, it behaves the same as a normal Wolfram language variable. So here we define simple var squared up here. We've been using it here and we're going to use it again here. So what we are doing here is we are. We are getting the result of simple var squared, but this time we're storing it in a Wolram language variable called simple bar squared. Confusingly, but this simple var squared with a capital S is just going to be a normal Wolfram language variable. So at this point, once you store the R expression into the Wolfram language variable, you can just treat it as any other Wolfram language variable that contains some value. So you can use it to do. In this case we're doing just some symbolic calculation, but you can, you can use it without making any reference to R itself. Once you. Once you store it into the Wolfram language variable, this is not too interesting. So essentially with Revaluate and external language cells, you would use an external language cell when you simply want to evaluate some piece of R code. If it's a single line, you just write it here. If it's a multi line, just don't forget to enclose it in these curly brackets and then it's going to work. Whereas with Revaluate you use it when you want to execute some R code, but you also want to store it into a Wolfram language variable that you're going to then probably reuse in later parts of your code. So this is just the most simple of the functions of R link, but already it allows us to do more things than we were able to do with the Wolfram script from R. Let's move on to the second function called rset. So with Revaluate we were storing the results of our code into from language variable. With rset we're going to be doing the opposite. We're going to be storing the results of full form language expressions into R variables. And you can see here immediately we have an example. So rset takes two arguments. The first one is the name of the variable in R space. So in this case we're saying myvar and the second argument is any woo from language expression that is going to resort into something. So essentially what we are doing here with RSAT is we're saying I want a variable in R space called myvar and I want this variable to contain the result of range 20 and range 20 just gives us the integers, the whole numbers from 1 to 20. So let's r Set that and let's check that it has worked correctly. We can just get the value of. Of myvar and we can see that it's just a vector with the whole numbers from 1 to 20. Again, just like before. Once you have it, once you have the variable that contains the value, you can use it from either language, you can use it from either environment just as if it is a normal variable. So here we're going to assign, we're going to bring it back to wolf from language variable and we're going to use some Wolfram language function on it. In this case, list line plot. And we're using the this variable, which contains the R variable. And this is the same. If we can, we can just copy and paste and pass this as an argument, that will be the same. So you can use it as a, as an R variable, you can use it as a Wolfram language variable. It doesn't really matter at this point. Once you have it stored somewhere, you can just call it with the appropriate function. Of course, here we're using a wolf from language function list, line plot, but we could also do, we could also use R functions on it. So in this case we are redefining the variable, we are subsetting the first five elements, and all of this is W from. Sorry, all of this is R code. So we're not making any reference to W from language here and we're still able to get the result back. So yeah, really, once, once, once you have it in a variable, you can do whatever you want with it. So yeah, the main reason you would use RSET over R evaluate is to pass Wolfram language expression to the R workspace. As you saw, this works really well with simple objects like strings. You can pass around vectors, you can pass around booleans. So any simple object is natively supported by R link. So it is going to be simply converted to whatever is the most appropriate type, if you want to think about types. So if you have an integer in Wolfram language, it is going to be converted to, to, I think an integer as well. In R, if you have a Boolean, it's going to be true and false, and so on. However, if you are dealing with any more complicated objects, like for example, here we have a 3D plot, and as you know, 3D plots are these objects in Mathematica that are interactive. They have a lot of properties. They're essentially more complicated objects. So what happens if we try to pass. Let's say we want this 3D plot to be stored in a R variable for some reason. So if we try to do R set, and let's say I want to store this plot into R variable called my 3D plot, we are going to get an error message saying the expression is not convertible to R. And that's simply because R Link doesn't know how to represent this 3D object in the language of R data types. So there is no really obvious representation of this object in R workspace. So RSET just refuses to do it for us. So what do we actually do if we want to pass more complicated objects between the two languages? Well, there are different solutions, and some of them are more complicated. You can define your custom data types, and you can define a function in R link that understands how to map these custom data types from one language to the other. I would actually advise against that simply because, I mean, it is an option, and if you're interested, I would encourage you to look into it. There's excellent documentation on our link, but for most of the time, there are going to be much more simple solutions, and we are going to be using those simple solutions in the examples. But just to foreshadow a little bit, the most common simple solution would be to just save whatever object it is you're trying to pass to the other language, just save it to a local file that both languages understand, and then import the local file from the other language. So when we get to our examples, we will be producing some plots, I believe, using ggplot2, which is R library, a very common R library for producing plots. And those plots will be too complicated for R Link to know how to transfer, even though they're essentially images, but they have some extra properties. So instead of going through all the trouble into defining a custom data type for a ggplot through our link, we're just going to save the plot to a local file into, let's say, a PNG file or JPG file, and then we're going to import that file from wolframatomatica. And that is, I really recommend doing that whenever possible, just because it saves you a lot of time. And essentially, if you think a PNG file is something that both languages already understand, so there's no need to reinvent the wheel in that sense, but we'll look into how that works in practice a bit later on. But just to recap the RSET part, you mainly use it to assign the results of Wolfram language expressions to variables in R. So the final, the final function, the final high level function of R link is going to be R function and what R function is going to allow us to do is to store. Oh, it's in the name. It's. It allows us to store our functions into Wolfram language variables and then we can use those functions alongside our normal. Normal Wolfram language. So here we're taking a very simple example. We have a function that takes string vector. It should. It takes a string. I don't know why there's a vector in there, but it's a function that takes a string and it uses several inbuilt kind of R functions like G sub and to lower kind of modified string. So what it's actually doing is it's going to be replacing spaces with underscores and it's going to be replacing capital letters with lowercase letters and then it's going to return the string. So what we can do is we can pass this whole function. So all of this is just normal R code here. It's what you would write in rStudio. It's what? Yeah, just normal R code. We're passing it as an argument to our function and then we're storing the, the whole thing into a Wolfram language variable, in this case called Snake case. So once you have it in the Wolfram language variable, you can start applying it to strings that come from R to strings that come from move from language. You can start applying it to anything. So here we're testing it on a simple Wolfram language expression. It's just a string, but it is a Wolfram language expression. Nevertheless. We're testing it on normal string, and we can see that the space was replaced with an underscore and then the capital n was replaced with lowercase n. So it works. But really, what, what is the most useful about this is that you can just plug it in into your normal Wolfram language functionality, and it works just as well. So here we have sort of a mini example where we are building a data set that takes some knowledge from the Wolfram knowledge base. In this case, we're looking at animals that have been observed in the Comoros. Excuse me. And we are taking the names of these animals and we're taking their weights. So we're going to have two columns, one with the names, what would one with the weight of these animals. And all you see here is Wolfram language code, except this little Snake case here, which is the variable where we start our R function. So all of these other functions transpose, entity, list, entity, value, class, all of the WolframAlpha stuff, all of that is Wolfram language. But in between that we have our R function and we are essentially going to be applying R function to the first column, so the name of the animals. But we can just plug that function right in between our Wolfram language code and it behaves as we would expect it to behave. So we can test that really quickly just to make sure it does what we think it does. And you can see that because we've applied snakecase to the first column with the names, all of the names here are snake cased. So, yeah, I think this is, this is all there is to say about R function just allows us to store whole R functions into Wolfram language variables and just plug them into our standard Wolfram language code. So that's very useful. So in between these three functions, Revaluate, R set and R function, you have essentially all the tools that you need to start using both languages together. So with that, we're going to move on to the actual examples and we are going to see how we can use these functions to kind of accomplish real world tasks. Okay, so the first example, workflow, as I mentioned, we're going to start fairly simple. But if you remember back when we did the comparison, when we had a little table, we were saying that one of the advantages of R, one of its really strong points, is that it has a lot of external resources. So you may find yourself in that situation where you're looking at how to do something really specific and you search for it online line, how do I do X for example, and you find that somebody has already done this in R and they have left whatever a resource, a function, a tutorial, whatever it might be. And of course you can just look at the. If they have left the code, you can just look at the code, you can try to kind of reverse engineer it and write it in Wolfram language code, which of course is going to work. But what we're going to be looking at here is why would we, why would we go through all the trouble into rewriting a whole function into Wolfram language code when we can just copy and paste it from somebody else and use it immediately? So I have this example function that I found online. Give me a second, I can put that on the screen. Okay, so hopefully you see that. I'll make it a bit bigger. This comes from Rasmus Bath's blog, which, if you're not familiar, absolutely excellent blog, if you're interested in data science, statistics, anything like that. But what Rasmus has done here is he has written this function in R that visualizes how the Bayesian Beta binomial distribution changes as each new element is added in. So he starts from the prior and he shows how the posterior distribution changes as new data points come in. Don't worry too much about it. That's not really the purpose of the example, but it's an R function that does a visualization that is really useful, especially if you're, let's say, in education or if you just think it looks cool, which I do. So let's say we want to use that function in our Wolfram language users and we want to use that function just to play around with it. Well, hopefully for us, Erasmus has left the whole source code of the function here at the bottom. I am by no means going to go line by line through this function. Again, that's not important. Actually, that's one of the points that I want to make, that you don't really need to care how the function works if you don't want to. So what I have done here is I have copy and pasted this whole thing. So starting from. These are just comments here. We don't need comments, but we're starting from the actual function, going all the way down to the end, and we are copy and pasting this whole thing without caring how it works. Let me just move that to the side because I have it here. So as you see, we are using this R function and we're passing the whole function that I just copy and pasted as an argument to this R function. So all of this is just straight copy and paste from. From the block, from Rasmus block. And we're storing that in a variable because we're going to use it. I have added one line. So this is where I kind of. I did a bit of foreshadowing where I say, because this function actually produces a ggplot, which is a special type of plot in R. And more importantly for us, it is a plot that R Link doesn't know how to handle. So if you produce this plot and you try to pass it, for example, using revaluate, in that case R LINQ is going to complain that it doesn't know how to represent that plot in Wolfram language space. So this is one of these examples where we need some way of working around that problem. As I mentioned, a way could be we could define our own custom function with R Link that is going to map an R plot into something that we then import into Wolfram language. Honestly, that's a lot of work. It's completely unnecessary in my opinion. So what I have done Here is, I have added one line at the end of the code. So everything above this line is just copy and paste Erasmus code. This line that I have added here at the end. Sorry, it's just this line is I'm using ggsave to save that plot into a image file, in this case plot png. Because I know that if I save it into an image file, then later on I can just import it from Wolfram Mathematica. I don't need to worry about using R link to transfer them. So let's. Let's define all of this huge piece of code. And here I'm just wrapping it in a Wolfram language function. So let me go just line by line. So the first thing that our function does is it calls plot binomial. And then plot binomial is actually a reference to our R function and it passes it whatever arguments we want to pass it. And then because we know that this plot binomial function is going to save a local file, the second thing we do is we just import this local file into Mathematica. So let me evaluate that. Yes, so with that we can just use this function and pass some arguments. The function is going to pass it to the R function. The R function is going to do its thing. It's going to produce the plot, it's going to save it to a local file, and. And then we're going to import that local file into Mathematic. So here we can test that our function works with just some example data. I think Rasmus's function takes either zeros and ones or trues and false. So here we're just passing a list of some values. 0, 0 essentially standing for false and 1 standing for true, just to check that it works. We can pass some random choice, true and false, 50 of them, just to see, because they're random. The. The plot is going to be different every time, but we can just play around with that just to see how it looks different. And obviously, so the posterior distribution, the more data points come in, the posterior distribution gets narrower towards the real center, which is because they're random. In this case it's going to be 0.5. But if you pass more data points, you can see how the distribution becomes more narrow. Actually, we can pass more. Let's see if we can break this function. This works well so far, but anyway, yeah, you can, yeah, you can have. You can have fun with it. So that's it. That's the whole example. Example number one. And the whole point of this example is that we are. Whenever we find an R function that exists out in the wild on the Internet, instead of rewriting it in Wolfram language code, even if we don't care about R, but we just happen to find something that we want to do written already in R, we just wrap it around this R function, we store it in a variable, and then we can use it. And again with this, we can just use it again. It's. It's quite simple. The first example is meant to be simple, but you can use it alongside any Wolfram language workflow that you already have. This just plugs in like it was a normal Wolfram language function. So that's it for the first example. Let's get to the next one. I want to have time, especially when we get to. I think example 4 is quite long, so I really want to have time for all of them. Okay, so in the first example we used, we used R function, and we fed that R function from language data and then the R function did the analysis and we just got the plot back. So the second example is kind of similar, except that we're doing the opposite thing. So we're using Wolfram language function for our R data. So we're, in this case, we'll actually be getting the data from R, we'll be passing it back to the Wolfram language. We're going to do our main analysis here. And I think in this case we don't send it back to R, but we could if we wanted to. So let's look at what data we have here. Have this R code. So TSDO is just a library that has a collection of time series data. So time series is one of those things that's much easier to do in Wolfram language. So if you've ever tried to do time series in R, you know that it could be a pain. Oh, I guess, depending which library you use to do the analysis. But typically you would have your time series data just as a normal list or a data frame or a matrix or anything like that. And some of the libraries really insist that you format it in a specific way. Whereas with Wolfram language, we're going to use time series model fit. And that just takes anything. It's not picky at all. So the idea here is we're getting some data from R in the form of a list, and this is what we do here. So this is probably not the best example, because tsdl, the library, already contains data that is already formatted as a time series. But. But I want to show you if the Data was a list. If it was your own data, you've collected the data or you found it from somewhere and it's in a list format, just a list or a vector, whatever you want to call it, that you can pass it to a form language and you can do your analysis. So here I'm actually taking a time series data and I'm converting it into a normal list. But in any case, so we are taking the 170th. So this TSDO contains hundreds of different data sets. We're taking number 170 here we're just doing our converting to list and we are storing everything into this variable called for export. Now this is a variable obviously in our workspace. All of this is in an external language cell. So what we're going to do now is we are going to use revaluate to get the value of this variable for export and store it into, into a Wolfram language variable. We can just see, we can just get the description of it as well. So this is Woofer sunspot number. So it's a time series data. To be honest, I don't, I'm not completely sure what it is exactly, but it's some property of the sun that, that people measure every year. And in this case we have the measurements from 1770 to 1869. So the number of woofer spots, whatever they are, that people have observed for this 100 year period, that's just a typical time series data. Normally just regular observations of something across a time period. But anyway, we imported the data here in sunspots. So sunspots at the moment contains just a normal list or it's a list of lists actually, of course, because it has the ear and it has the number of woofer spots in each sub list. But this is just a very kind of basic, probably the simplest way you can store this data and already without doing any additional work, we can apply time series model fit to it. We are applying this to sunspot. So this syntax here, just let me. This is the same as this. So we are applying our time series model fit and we are relying on Wolfram language to pick the most appropriate family of models and we are storing that model in a variable. Once we have that model, we can get its parameters, we can get the predictions for a certain year. So we can say based on this data how many Woofer sunspots should we expect in, in the year 2024. And the model tells us you should expect 47.11 in any case, then we can again, once we have that into our Wolfram language. We can do all of our Wolfram language things in it. I'm not sure why this doesn't work. Sunspots model. Oh, I see. I, I was using some different data before that. Yeah. So this works fine. You can do your Wolfram language visualizations on it. Obviously you can add labels to all of the things. So this is the, this is all the second example really is. It's. It's kind of the opposite, the mirror image of the first example where we can say, well, I guess this is more relevant if you are already an R user. But there's something that is much easier to do in the Wolfram language, for example, time series analysis. So instead of wrestling with R to convert your data into the appropriate type and so on, you can just pass your data to Wolfram and just do your analysis here. Once you have this plot again, you can just very easily export this plot as a local image if you want png and then you can export this and spot labeled plots and spots labeled. Oh, I didn't actually evaluate this. Yeah, I'm still using the old variable names, sunspots plot. Okay, this works now. Spots, label, plot and this exports it. And then you can import it in R if you, if, if you need it for some reason. Because again, if you try to pass this with R set, I believe that this is not going to work because plots in Wolfram language have this kind of interactive. You can see when I hover at a specific point, I get this nice pop up. So for example, in the year 1838 there were 103 woofer spots. So if you try to pass this through R link using rset, that is not going to work. But this is just a very easy way to do it. Okay, so that's it for the second example. And let's move on to the third one. Okay, so the third one again, going back to our comparison table between the two languages, why would we want to combine the two languages? Well, one reason that we mentioned in the beginning in the comparison is that Wolfram language is much faster for some calculations than R. So if you're an R user and you find yourself doing something that takes a long time to do in R, chances are just with a simple passing of the data tool from language, you can reduce that calculation time by a lot. So here we are going to take a book novel, I think in this case. So we're using this library in R called Gutenberg R. If you're not familiar with the Project Gutenberg, it's a project that hosts literature, so books in the public domain that are free for everyone to use, to read, to distribute, to copy, to sell, whatever. And this library in R gives us access, programmatic access to all of the books in the, in the Project Gutenberg. So here we're, we're calling the library and we are assigning, we're saying download, Download Book with ID 2600 and assign that book to a library, to a variable code book. 2600 is war and Peace by Leo Tolstoy. So what we are going to do first we're going to actually get the book and store it in a variable in R. Then let me just. Oh, and then we are going to get the text of the book because it, I think when you download something from Gutenberg using Gutenberg R, it gives you some metadata as well that we don't necessarily care about here. So here we're just getting the whole text of the book and we are storing it in a string which, as you can see, we can see the beginning War and Peace by the Tolstoy. And then we have the, the full book, which is a text file that is 5.6 megabytes, which is quite a lot for a text. Obviously, it's a very long book. The reason why we want a very long book is that we're going to produce a word cloud. I guess you're familiar with word clouds. It's these, it's a visualization of a text where the more often a word is mentioned, the higher so the, the bigger it appears in the word cloud. So word clouds are a fairly simple thing. You, you're probably doing more complicated things with R and move from language. But it's a good example to use because it requires a reasonable amount of computation. So it needs to tokenize the whole novel, to remove stop words, to count frequencies, then to plot everything. So as you see, even so, the computer we use here to present from is quite powerful, quite beefy. But even with our computer, it takes quite a while to actually produce the word plot. So let's actually start producing it now. So essentially what we'll be doing is we got the variable from our into Wolfram language and we are using the word cloud function. This is a Wolfram language function that is going to produce, well, a word cloud of the book. And then we're going to do the same thing in R and we're going to compare how long it takes to do it in Wolfram language and how long it takes to do it in R. So to do that. We can use this absolute timing function which times how long an expression takes to evaluate. So we are doing again the same string, the same thing. We're doing a word cloud on the string join to book. Sorry, tostring for the book. Essentially we're splitting the book into different words and we are applying the word cloud function to that and we're measuring how long it takes. So you can see in the output the, the first value is how long it took. So it took the Wolfram language 17, 0.9 seconds to produce this word cloud. Just to compare, we can do the same thing in R. So again we'll be saving this to a image and we are using essentially the equivalent of this function just in R syntax. So instead of absolute timing, we're using system, system time. And then word cloud is actually the same and we are applying it to book, to the text of the book, which is, which is the same thing that we have here. I'm actually not going to run it because it takes quite a while. But I can show you that the last time I ran, took almost six minutes to produce this word cloud. And we can then import it. Obviously again we're doing this whole exporting to a PNG and we are importing it later from Wolfram Mathematica. But I don't want to sit here for six minutes waiting for this to finish. But by all means you can test this at your own pleasure because the main point here, again, it's not the word cloud. If you have any time consuming analysis, any computationally intensive tasks in R, it may be very well worth it to combine the two languages for that purpose, to offload the computationally intensive tasks to the Wolfram language. And then once you have this, you can, you can transfer it back to R, do whatever you want with it. But yeah, that's the idea. And obviously this is a very kind of a situational example. So if you're mostly doing analysis that don't take that long to, to finish it only take a few seconds, then this is not really going to be worth it. So you can just do everything in R. But big data is becoming very common, so more and more people have databases or tables that are thousands, hundreds of thousands of rows, big matrices, big strings like we have here, if we're doing analysis on books or even on a corpus of books. So it is situational, but it is to just kind of give you the idea that if you find yourself in a situation where this might be useful, then it is a use case for combining both Languages. Okay, so now we're getting into the kind of more detailed examples. I think I have enough time to get through them, which is fine. Okay, so in this example, we will be passing data back and forth between Wolfram Language and R. And we're going to be. We're going to start with a CSV file that already exists that I just found on the Internet. So first of all, we're going to import it into Wolfram Mathematica. We're going to store it in a variable, and we're going to have a brief overview, just taking the first ten rows. Ten? Yes, ten rows, just to see what our data looks like. So I just get rid of this. So what do we have here? We have a CSV file that contains the coffee production, the total production of coffee for different countries. And this is in tons, I believe. So we have a lot more countries in this, but we're just taking the first 10 rows just to visualize it. So for each year, we have the coffee production for all of these countries. So what we want to know, what we want to do with this data is we want to build a model. We want to say, how can we predict whether a country. Because this data, let's say it ends in 2001. I want to say, how do we build a model that predicts what. What factors are likely to influence that. That coffee production? Normally, if you have a lot of variables, you can start experimenting with them. But in this data set, we don't have anything. We just have the. The final coffee production. So it is really up to us to start adding variables so we can start thinking what is likely to. What is likely to be an influential factor on coffee production? And for this example, I have taken two factors. I have said that the population is going to be one factor. So the more populous a country is, the higher its coffee production is going to be. And then the other factor that I think is going to predict coffee production is climate types. So depending on which climate types are present in a country, that's going to predict whether it produces more coffee or less coffee. So normally what we would do is we would try to find that data. So data on the populations of the countries and data on the climate types from some external source, probably search for it in Google, and then we're going to start adding it to our data set. But that takes a lot of time. And this is where we're going to make use of the Wolfram knowledge base, because data on population and data on climates of different countries is something that already exists in the Wolfram knowledge base. So we don't need to go outside of that. To get that information. And so for the first half of this example, we'll just be sitting in Wolfram language. We're not going to be making too many references to R, and we're going to use Wolfram language to kind of populate the data for these two factors that we think are going to predict coffee exports. And then in the second half of the example, we're going to, once we have this data together with our original data, we're going to pass the whole thing to R. And that's where we're going to do our main analysis. We're going to construct a linear model, we're going to plot it, we're going to do a line of best fit and all of that. We'll do that in R. But let's get this. I may not go line by line just because this example is fairly long, but I'll give you kind of gist of what is happening at each step. So, as I mentioned, the data on population, so we're starting with population. We want to know for all these countries here, what is their population? As I mentioned, this data already exists in the Wolfram language, so there's no need to go outside. So if you do something like say, France population, you should be able to get the, the number. And what we want to do is we want to do that for all of the countries in our data set. So we can add this information as another column, and then that's going to be a variable that we use in our model. So before we do that, we are going to do some data cleaning. And for this we actually use our function. So it's just we're using this G sub function, which I think in this case this is a regular expression. No need to worry about it if you don't, if it doesn't look sensible. But what this function does is it's going to delete everything that is in parentheses. So because I think also we can see an example here, we have Bolivia, and then in parentheses we have plurinational state of. So when we interpret those countries as Wolfram language entities, like I did here with France, for example. So France works, but if I have France Republic of, it's probably, oh, this works. But okay, sometimes it doesn't work, sometimes it works. We really want to clean up all these things in parentheses. So then we can, we can be sure that it's going to work when we try to interpret them as countries. So we're preparing this function which is going to take the names of the countries and it's going to remove of everything that is in parentheses. So then we're going to apply it. And then we can see this is the list of all the countries. And now which one was it? Bolivia. Now Bolivia is just Bolivia. It's not Bolivia. Plurination or plurinational state of. So we have cleaned up the data, and since we have cleaned up the data, we can start. We can start getting our action actual values. So to do that, we are using this interpreter function and we are saying, interpret whatever comes here. So this is a slot. Interpret that thing as a country and get the value of its population. So we are passing this whole list of countries here and we're saying, interpret them as countries and give me their populations. So here as the output, we have the population. So the population of Angola is 36 million, et cetera, et cetera. Then we're doing some more data cleaning. We are adding a header and we are appending this whole thing back to our original data set. So here, if you look at the first and the last column of our data set, now we have the population of the countries, which is one of the factors we cared about. Because when we pass this data back to R, we're going to say, okay, this is going to be one of our predictors, one of our independent variables. Okay? And then essentially we do the same thing for climates. Again, climates are information that is already available in the Wolfram knowledge base, so there's no need to look for that elsewhere. In fact, there are a lot of things that I don't know why I'm using France every time, but in any case, I just want to show you the all the properties that you can get of countries that are already integrated in the Wolfram knowledge base. So I'm saying, what can you tell me what information is there in the Wolfram knowledge base about France? And you can see not only do we have population and climate, we have all of these. So we probably could have picked some more interesting factors to predict coffee production. But in any case, you have a lot of information for each country. So, yeah, again, we're interpreting all the country names as countries, and in this case we are getting the climate types. Not going to run line by line through this code because it's the same idea as the previous one, except in this case, we want to be. The way we are going to store the information about climates is we're going to have a separate column for each type of climate that's present in the data set. And then if the country has this climate, if the, if the climate is present in that country, it's going to get a one, and if, if, if the climate is not present, it's going to get a zero. So you can see that in Angola there is no humid subtropical and there's no tropical rainforest climate. But in Ecuador there is tropical rainforest climate. So this is, we are structuring the data. We're anticipating the kind of the structure that our R code is going to require to run the model. Other than that, it's fairly, it's just a lot of data cleaning and putting things together. It's not very interesting. So I'm not going to bore you with that. But at the end we have our final data set, which. Let's see what that looks like. Essentially we're going to have this list of lists, this matrix which is going to contain. So this, the first list is just the headings. So. This, so this total production and all the years were already in our data. Then we added this population variable and then we also added all of these climates that get either 1 or 0 based on whether the countries have those climates or not. So with all of that, we are going to export this whole matrix into a CSV file which we're going to import into our. Again, same strategy. So we did, we did all of the work that we had to do in Wolfram language. Now we're going to pass this whole file to R. With R, we're simply just importing this CSV file. Again, this file, you have access to it. If you want to go into more detail in the example, it's not that interesting, but in any case you have access to it. And so we're going to say we are going to read CSV and we're going to read this file path, which is where we save the file, and we're going to store this whole thing into a variable called coffee data. And, and, and then once we have the whole data set stored in coffee data, we can start getting, we can start doing our, our functionality on the data. So here we're creating a new column called Average Exports and it's going to be the mean of all the years. This is probably a good point to talk about data frames and just data sets in general, because if you notice, before I exported this file to CSV, I converted it to just a simple matrix rather than a data set or a list of associations or anything like that. So in Wolfram language we talk about data sets which are these tabular containers for rectangular data in R. We talk about data frames, which are probably the most common data types in R. So how do data frames work with R link? And the answer is it's kind of complicated. They don't work as you would expect them to, which is unfortunate. So if I had a data set here and I tried to pass it using R link to an R variable directly, that was not going to work, even though you would think that if you try to do that, the kind of, the most sensible mapping would be for R link to map it to a data frame. But it doesn't work, unfortunately. So again, the solution is easy, just export it to a local file. But then weirdly, if you have a data frame in R, you can use table form on that data frame to kind of visualize it in Wolfram language. However, if you use, if you try to store it in a variable, it's not going to work. So it's really unfortunate because as I mentioned, data frames are a really common data type in R and it would be nice if they worked perfectly without any issues. And I'm not really sure what the problem is, but I've been experimenting for them for a bit and yeah, they don't really work work as you would expect them to work. But as I mentioned, in this case if you use table form and you try to, and you apply it to so this coffee data, once it's in R, this is a data frame. So we're just taking some, some of the rows, so the first five rows and the first ten columns. If you stable form you get this really nice table, you get what you would expect. But then if you try to store it in a variable, it doesn't always work. So that's just, that's just a, a little aside just to, to let you know that data frames don't really behave perfectly. But any case, coming back to our, our analysis in our. So here we got the means of all the exports for all the years for each country. We're pending it back. And here we're rich exports. I think I forgot to evaluate this maybe. See. Yep. So here we're doing again not going to go line by line. Here we're just doing our linear model. We're doing a bit, a bit more data cleaning. We're constructing the formula for the linear model and we are doing our linear model. Then we are going to get just the, the values of the model, the parameters of the model that we care about. In this case it's going to be The R squ squared and it is going to be the P value. So you can see the r squared is 0.6, which is quite good, which explains the variance that the model explains in the data and then the P value, which is statistical significance, which is also good. So this is a model that uses population and climate to predict coffee exports. Of course it's going to be good. It's, it's a fairly obvious, it's a fairly obvious relationship between those factors. But in any case, and the final thing we're going to do is we are going to use ggplot2 to visualize that linear model that we just constructed. Again, by no means going line by line because it's quite long, but this is just a standard ggplot2 syntax. And again, as you see. So with this done, we now have the plot stored in this R variable called line of best fit. To actually get it in the Wolfram language, we'll again be saving it to a local file and importing it from there. Hopefully this works. It does. So you can see this, this plot here was produced by R, more specifically by the ggplot library and we have imported it in Wolfram language. And with that we have, we have completed this workflow which started from us just with a simple CSV file that we found on the Internet. We used Wolfram language to supplement that file with external information, so population and climate types. Once we did that, we passed it back to R where we did our linear model and we produced this nice looking plot here where you can see that Brazil obviously is quite a big outlier in the data in that it produces a lot of exports and so on. So that was the fourth example. Apologies if it was a bit long and a bit boring, but I think it's a good kind of representation of again of just combining both languages to use their respective strengths. So in that case it was the integrated knowledge base, of course, Wolf form language, that was its strength. So you couldn't do that in R. Well, you'll have to do some web scraping, I guess. It becomes a, it becomes a much more complicated task in R. And then we passed it in R just because obviously we could have, we could have done the linear model in Wolfram language if we wanted to. But let's say, for example, we are more comfortable with the R syntax. So instead of relearning how to do the same thing in another language, we just passed it back to R and we did that. Okay, So we have arrived at the last example and unfortunately this example no Longer works. I wrote this example a few months ago and it was fine. But as is usually the case with web scraping examples, whenever people change their web pages, your web scraping script no longer works. But I have the data saved from the previous time I ran it. So I can show you what the idea is. Even if we are not able, we're no longer able to run it in real time. So I'll just open this webpage here just to show you what the idea behind this example is. So the idea was to go to this web page. I'll make it a bit bigger. So this is just statistics of different tennis players on different kind of performance indicators, let's say. So you have all the different tennis players here and you have different things for like serve rating, first percent serve. Just a lot of data in a lot of different categories. So you have three tabs here. So what this example, the fifth example actually did, is it used a package called arcellenium. And rselenium allows you to kind of operate a browser programmatically and go to these web pages and collect the data and save it to a CSV format. Actually, it's kind of similar to what we just did back with the BBC articles, if you remember, when we were using Wolfram scripts. But we can obviously do this in R and R. Selenium is probably the most popular package to do that. So what this example used to do is it used to go to that webpage that I just showed you. It grabbed all of that data and it saved it in local files. So as I mentioned, that worked a few months ago. But now what the ATP website has done is it has added these captchas where those things, you know, prove you're a human, select 10,000 fire hydrants, you know, those things. So now our web scraping script no longer works because it's not able to deal with those. So it doesn't get access to the data. But I still have the data saved from the last time. And obviously there are ways around these captures, but I haven't taken the time to kind of try to bypass them, and I probably shouldn't for a webinar. But in any case, that was the idea here. We were going to be using this web scraping script, get some data from the web, and then once we have this data that we got with R Selenium, then we're going to import it back into Wolfram language and we're going to create an interactive plot using Manipulate, which is again, one of the strengths of Wolfram language is that we're Very easily able to create these manipulate elements that are interactive, that allows us to be more interactive with our data. So I'm not going to run this. This would have worked absolutely fine if there was no captcha on the ATP website. But as it is, and it's quite a large piece of code anyway, just because it was going to a different pages, it was clicking buttons on the page to load more players and things like that. So if anyone's interested, feel free to explore it. But the kind of end result of this whole huge block of R code or was this write CSV command which once we had all the data, we were writing it in this file called Tennis data CSV. Luckily I have tennis data CSV from the last time. So we're going to go ahead with the example. We're just going to pretend that we did all the scraping live and we got this tennis data CSV file. Okay. So once we have the tennis data CSV, we import it in Wolfram language. Just going to run through this, see, is this the right. So I'm just looking to see because there were some lines here that were not necessary. I'm going to see if this works now. But essentially the way that the data that we structured the data when we were importing it was that we had a column with the images. Oh, this works. But we had a column with where there was a link to the image of whichever player was that that role belonged to. So what we are doing here in this, in these few lines of code is we are importing the actual image from the link and we are embedding it in the data. Again, going back to our comparison table, we mentioned that one of the things that is very easy to do in Wolfram language is to operate on different types of data. So here we're going to be using the images of the players to do some, some cool stuff with them and then the final step is, yeah, producing this manipulate element which is if we go down we can see what it looks like. So here we have this menu where we can choose the different, different performance indicators and we get these live plots here which shows us how what are the top players based on these indicators. So here essentially we're selecting the X axis which could be anything we want, let's say first serve. And then the Y axis is going to be return games one. And then we can see which player ranks where on these two parameters. So for example, here we have Alexander Zverev at the time of scraping, he had the highest first serve percentage and, and pretty close to the highest percent return gain is 0.1. Yeah, the whole point here was to show that you can, once you get your data with R, you can very easily create these manipulate elements. Oh, and the other thing is of course the images you can, instead of points, you can use the, the heads of the players as points on the plot. So quite cool. It was a quite a cool example when it worked. Unfortunately the captures ruined that for now. But in any case, I'm glad that I'm able to show you what the final result looks like. Oh, and then there's one more command that we can deploy this plot to the cloud and then we can share this link with other people and they're able to do this to kind of play with the plot and have the full interactivity again with a single command. Cloud deploy, it goes on the web, send the link to other people, they're able to use it immediately. And I think this is it. That was the last example. Apologies again that we couldn't do the web scraping part live. It's just really annoying with the captchas. So just to have a little conclusion, hopefully this gave you an idea of how you can combine Wolfram language and R and how to really use the strength of one language to kind of complement the other. So again with the examples, hopefully I know that you're probably most likely not going to be doing these specific examples, but the whole idea behind the examples was that to give you some ideas even if you don't do the exactly the same thing, hopefully it will give you some ideas of what you can use. This especially R link, but also I guess Wolfram script, what you can use it for in kind of real world tasks. So hopefully that was, that was somewhat interesting. And yeah, I'll go through the questions now. I already have a few. So if you have any questions, now would be the time to put them in the chat. If you're watching on big marker, you can put them in the chat on the right. I think we are also streaming to YouTube so if you put your questions there then they will be sent to me as well. So yeah, I'll give you, I'll just give you a few minutes just in case anyone has any questions and I will start answering. Okay, so the first question we have is, is it possible to use external packages like rstand and. Absolutely it is possible. You can use all of the, any external package that you have in R, you can use it via R link and it should work in the same way that it works in your normal R environment. Again, the only caveat with that is if your package is producing some complicated object, you might not be able to transfer it directly via R link. Then you have a few options. We talked about it many times already that the most simple one is going to be using a local file to do the transfer between the two languages. But you could also define custom data types. But to answer your questions, yes, it is absolutely possible to use rstand or any, any other packages. I mean we already used quite a few in these examples we used ggplot, we used Gutenberg R, we used time series data set. So yeah, it is absolutely possible. So the next question is within an R external evaluate cell, does. Does this need to use the standard wolf from language Shift enter to execute to run the R code within it? Yes. Yeah, so the, if we go back to the external language cells. Yeah, you just do the normal shift and enter and it executes the code, it sends it to R and you get the result. So yeah, it behaves very much like a normal Wolfram language cell with the shortcuts. Thank you for, for the nice presentation. Well, thank you for listening. And the question is how to set up R link. So yeah, yeah, obvious have mentioned that. But the good news is that there's not much setup involved. So if you, if you have R already installed on your computer, then you are the first time you run an external language cell or R evaluate, then R is going. R link is going to be automatically set up. So you don't need to do anything. There's no installation, there's no downloads, there's no importing external packages. So just execute an external language cell and hopefully Mathematica should be able to locate R on your machine. Yeah, I think there are some things you can do if you want to manually configure. I think I'm thinking about for people who have multiple installations of R on your machine, I think you can specify which specific one that Mathematica should use. But if you just have R installed, then just execute an external language sound that should work out of the box and if it doesn't, let me know we can troubleshoot. But it should. The only problem you might have is if you don't already have R. I think even then R link is going to try to download it for you but it may fail. So I would recommend, I would always recommend just downloading it from Crown or wherever installing it and then it should work out of the box. Let's get on to more questions. You answer my questions after I posted it with your example using the Gutenberg workload. Yeah, that's okay. That's perfect. Yeah. Yeah, sorry, I don't get to questions right away when they're posted because it's a bit more difficult for me. That's why I always leave them at the end. But thank you for. For the interest and I'm glad I was able to answer the question after that. Could you please provide the link to Rasmus's blog? Thanks. Absolutely. Yes. Let me just see which example was that the second one? No. It wasn't the third one? No. Oh, it was the first one. Of course, yeah. Give us a second. So I believe the link is samsar.net oh, it's just some sort of. Net. I can paste it in the chat. I believe it really is an excellent blog. It's. It's still. Because you can see he always has a lot of cool examples. I'll just paste it in the chat here, So hopefully you should be able to access that. Let me just move that because it's a bit distracting with all the flashing things. Okay, so more questions. The functions needed to perform statistical analysis, data analysis already exists in Mathematica. Write, are there any specific things, functionalities that are not present in Mathematica that require access to R? Few examples, please. That is a good point. So for everything that we have been using R, you can most definitely do the same thing in Mathematica. I'm struggling to think of examples of something that you can only do in R that is impossible to do in Mathematica fundamentally because again, we're talking about programming languages. So you can do anything you want with programming languages. It's just that some things are easier in one language or another. So then the question becomes what is easier to do in R compared to Mathematica? I would say some. There's some simple kind of edge cases. I think doing an ANOVA in in wufra, Mathematica is a bit more involved than it is in R because you have to import an external package. If I'm not mistaken, maybe somebody will correct me, but I think the last time I tried to do an ANOVA in Mathematica, I found that it was more complicated, so I just used R. So it's mostly going to be for things like that that. Not things that only R can do, but things that R is better suited for trying to think of any other examples. I think the other examples will be just some external libraries that. That do something very, very specific that again, you could do in Mathematica, but it would take you some time to rewrite all the code in Mathematica. So for those things, it's probably better to use R. But I don't really have any excellent examples of something you can do in R that it's impossible to do. Mathematic. Then we have one more question. Do these functions come natively or an install for R Link is necessary? So, well, R Link comes natively, so there's no extra installation you need to do. And all of the functions that I have been showing you, at least on mathematical side, are natively supported by R Link. So from the Wolfram language side, there's nothing external for you to download other than R itself. Obviously, when it comes to. When it comes to the R code that you see throughout the notebook, sometimes we need external packages. So for example, here, the example that I'm on, I need the tidyverse and Ggrigis packages. So in those cases, I mean, I have the lines here, you can install them straight from Mathematica, you can install them from Automatic, but I guess those are. Those functions have kind of external dependencies. But not from Wolfram side. No, everything you saw on the Wolfram side is natively supported. Okay, I don't see any more questions. Last chance. If you want to ask something, you can always email us with any questions or any problems, especially if you have trouble setting up our link. As I said, it should work automatically, but maybe sometimes it won't. So we'll be happy to kind of debug that, seeing if there are any questions on YouTube. Okay, I think that was all the questions. So thank you all for, for being here, for sticking around, and I hope this was useful to you. Again, if you have any issues, let us know. And if not, something I forgot to do is we have a next webinar next week, Medical Image Processing and Computer Vision with Mathematica. And if you're interested in that, by all means, sign up. There's a QR code, there's a link, and we are looking forward to seeing you all there. Thank you very much. Hope you have a good day. It.
More from Guy

Cheryl Strauss Einhorn | The Human Edge: Smarter Decisions in the Age of AI | Talks at Google

Rachel Glennerster | Innovation and the Center for Global Development | Talks at Google

Rebecca Winthrop | Rethinking the Purpose of Education in the Age of AI | Talks at Google

Dr. Robert Wachter | A Giant Leap: How AI Is Transforming Healthcare... | Talks at Google






