{"id":1710,"date":"2024-08-30T11:16:53","date_gmt":"2024-08-30T09:16:53","guid":{"rendered":"https:\/\/inseri.swiss\/?p=1710"},"modified":"2024-11-12T15:45:57","modified_gmt":"2024-11-12T15:45:57","slug":"r-and-plotly","status":"publish","type":"post","link":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/2024\/08\/r-and-plotly\/","title":{"rendered":"R and Plotly"},"content":{"rendered":"\n<p>In this example we will show you how to integrate our R code for generating Plotly figures into the R Code block and use the Plotly Chart block to display it.<\/p>\n\n\n\n<p>For this use case please pay attention at the following points (see the code below):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>there is no need to use <kbd><code>install.packages<\/code><\/kbd> because the missing packages are downloaded by default (see the first line of code below and the docs)<\/li>\n\n\n\n<li>for converting the Plotly figure to a <strong>JSON<\/strong> use <code>plotly_json<\/code> (see line 6)<\/li>\n\n\n\n<li>save the json as a json file (see line 7)<\/li>\n<\/ul>\n\n\n\n<p>Now the JSON file is the <strong>output<\/strong> of our R Code, and used further for the JSON description of the Plotly Chart block.<\/p>\n\n\n\n<div class=\"wp-block-inseri-core-r-code\" data-attributes=\"{&quot;autoTrigger&quot;:false,&quot;blockId&quot;:&quot;eydnAJtVNn4ig7yPCmyMv&quot;,&quot;content&quot;:&quot;library(plotly)%0A%0Afig%20%3C-%20plot_ly(x%20%3D%20c(%22a%22%2C%20%22b%22%2C%20%22c%22)%2C%20y%20%3D%20c(1%2C%203%2C%202))%0Afig_as_json%20%3C-%20plotly_json(fig%2C%20pretty%20%3D%20FALSE)%0Acat(fig_as_json%2C%20file%20%3D%20%22plotly_json.json%22)&quot;,&quot;editable&quot;:true,&quot;height&quot;:195,&quot;inputCode&quot;:&quot;&quot;,&quot;inputs&quot;:{},&quot;isVisible&quot;:true,&quot;label&quot;:&quot;Feel free to edit the R code&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;r-plotly&quot;},&quot;mode&quot;:&quot;editor&quot;,&quot;outputs&quot;:[]}\">is loading &#8230;<\/div>\n\n\n\n<div class=\"wp-block-inseri-core-plotly\" data-attributes=\"{&quot;blockId&quot;:&quot;ChfmdLtIZWWJJjlR0y3iQ&quot;,&quot;height&quot;:500,&quot;inputConfig&quot;:&quot;&quot;,&quot;inputData&quot;:&quot;&quot;,&quot;inputFull&quot;:&quot;eydnAJtVNn4ig7yPCmyMv\/plotly_json.json&quot;,&quot;inputLayout&quot;:&quot;&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;plotly&quot;},&quot;outputs&quot;:[]}\">is loading &#8230;<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:60%\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-inseri-core-export alignright\" data-attributes=\"{&quot;align&quot;:&quot;right&quot;,&quot;blockId&quot;:&quot;vaxQQCzmrnvQCf4meQgfk&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;export&quot;},&quot;postId&quot;:1710}\">is loading &#8230;<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-inseri-core-share alignright\" data-attributes=\"{&quot;align&quot;:&quot;right&quot;,&quot;blockId&quot;:&quot;KTETwIissQyi0jL3ZpuwV&quot;,&quot;copiedText&quot;:&quot;Copied&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;share&quot;},&quot;showIcon&quot;:true,&quot;text&quot;:&quot;Share&quot;}\">is loading &#8230;<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we will show you how to integrate our R code for generating Plotly figures into the R Code block and use the Plotly Chart block to display it. For this use case please pay attention at the following points (see the code below): Now the JSON file is the output of our [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[40,47,49,50],"class_list":["post-1710","post","type-post","status-publish","format-standard","hentry","category-basic-example","tag-export","tag-plotly-chart","tag-r-code","tag-share"],"_links":{"self":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/comments?post=1710"}],"version-history":[{"count":1,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1710\/revisions"}],"predecessor-version":[{"id":2095,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1710\/revisions\/2095"}],"wp:attachment":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/media?parent=1710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/categories?post=1710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/tags?post=1710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}