ggplot2azure-synapsesparkr

How can I save a ggplot within Azure Synapse?


I'm trying to save a ggplot that I created in a Synapse Analytics notebook in R.

I found a previous question explaining what to do in Python, but I can't replicate it in R.

I successfully mounted the Azure Blob Storage account using mssparkutils.fs.mount() and am able to see into the account using mssparkutils.fs.ls(). However, when I try to use ggsave() to save the plot to file:/{mount_path}/local/..., I don't see the file at the specified path.

In the notebook itself, I only see a message "Saving 6.67 x 6.67 in image".

I'm a little new at this, so apologies if I used the wrong terms.


Solution

  • I have tried the below approach in matplotlib for plotting and saving images:

    mssparkutils.fs.mount(
    "abfss://rawcontainer@lambostg.dfs.core.windows.net", 
        "/mnt", 
        { "linkedService": "synpsilip-WorkspaceDefaultStorage" }
    )
    mssparkutils.fs.mkdirs("/mnt/folder02")
    local_plot_path = "/tmp/sample_plot.png"
    plt.savefig(local_plot_path)
    print(f"Plot saved locally at: {local_plot_path}")
    dest_plot_path = "/mnt/folder02/sample_plot.png"
    mssparkutils.fs.cp(f"file://{local_plot_path}", f"abfss://rawcontainer@lambostg.dfs.core.windows.net/folder02/sample_plot.png")
    print(f"Plot copied to: {dest_plot_path} in Blob Storage")
    files = mssparkutils.fs.ls("/mnt/folder02")
    for file in files:
        print(file.name)
    

    In the above code mounting the Azure Blob Storage & Creating the directory Saving the plot locally and Copying the file to the mounted Azure Blob Storage Listing files in the directory to verify

    Results:

    /mnt
    Plot saved locally at: /tmp/sample_plot.png
    Plot copied to: /mnt/folder02/sample_plot.png in Blob Storage
    

    enter image description here