saving file in databricks r language - r

In notebooks with databricks/azure r language, I create output files. How I can save these output files to local directory outside of databricks or open these files in databricks?
Example - I create plotly html plot
library(plotly)
fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig
In r studio, when I type "fig", the file automatically opens by itselfs. In databricks notebook I configure and set up cluster compute cores and permissions, then run r code which works. But in databricks notebook, when I type "fig", nothing happen.
I also try to save to my computer - like
list.files(path = getwd(), recursive = TRUE,full.names=TRUE)
saveWidget(fig, file = "dbfs:/path/to/fig.html")
save_html(fig, file = "dbfs:/path/to/mfig.html")
saveWidget(fig, file = paste0(getwd(),"/fig.html"))
I also try but not work
dbutils.fs.cp("dbfs:/path/to/fig.html", "file:/path/to/local/directory/gif.html")
local_path <- paste0(getwd(), "/fig.html")
dbutils.fs.cp("dbfs:/path/to/fig.html", paste0("file:", local_path))
How to automatically get databricks to save and view?

Related

How to locate Excel table I've created in RStudio with `writexl` package?

After successfully running the following example:
install.packages("writexl")
library(writexl)
tmp <- write_xlsx(list(mysheet = iris))
I can't find the iris Excel table in my Home folder.
Your code did not write anything to disk. It stored the command to write an excel file into the R object tmp, which is quite different.
To write you dataframe as an excel file in disk, do:
write_xlsx(x = list(mysheet = iris), path = "iris_excel_file.xlsx")

How to See Hidden Files in a File Dialog Using R?

Using RStudio on our Linux server, I am attempting to display a file dialog in R that allows the user to see hidden files (files that begin with a period {.*} that are visible with ls -a). I have tried the following file.choose() and rstudioapi::selectFile() with different options, but the hidden files remain hidden in the file dialog in each case:
myHiddenFile1a <- file.choose()
myHiddenFile1b <- file.choose(new = TRUE)
myHiddenFile2a <- rstudioapi::selectFile()
myHiddenFile2b <- rstudioapi::selectFile(existing = FALSE)
myHiddenFile2c <- rstudioapi::selectFile(filter = ".*")

Read / write to folders with AWS and R

I am trying to use the free version of Amazon Web Services EC2 with Ubuntu and R. I created a simple R file I hope will read a small CSV input data file in one folder, perform a trivial operation and write the output to a CSV file in a separate folder. However, the output CSV file is not being created.
Here are the contents of the R file:
my.data <- read.csv('/my_cloud_input_file_test/my_input_test_data_Nov22_2019.csv')
my.data$c <- my.data$a + my.data$b
write.csv(my.data, '/my_cloud_output_file_test/my_output_test_data_Nov22_2019.csv', row.names = FALSE, quote = FALSE)
Here are the contents of the input data file:
a,b
100,12
200,22
300,32
400,42
500,52
Here are the only two lines I used in PuTTY after connecting to the instance:
ubuntu#ip-122-31-22-243:~$ sudo su
root#ip-122-31-22-243:/home/ubuntu# R CMD BATCH Cloud_test_R_file_Nov22_2019.R
The R file is located in the ubuntu folder according to FileZilla, as are my input and output folders.
Can someone please point out my mistake? If I put the R file and input data set both in the ubuntu folder then the output data set is created in the ubuntu folder without me having to use a setwd statement (after I modify the read.csv and write.csv statements to eliminate my input and output folder names). So, I am not using a setwd statement here. If I need a setwd statement here what should it be?
Sorry for such a trivial question.
This code worked:
setwd('/home/ubuntu/')
my.data <- read.csv('retest_input_data/my_input_data_Nov24_2019.csv')
my.data$c <- my.data$a + my.data$b
write.csv(my.data, 'retest_output_data/my_output_data_Nov24_2019.csv', row.names = FALSE, quote = FALSE)
PuTTY line:
ubuntu#ip-122-31-22-243:~$ R CMD BATCH retest_R_file_Nov24_2019.R

Saving plot.ly image to RData file

I create a plot_ly image using:
MilesPlotly <- plot_ly(x = TripDetails$TotalDistanceMiles, type = "histogram")
I then want to save it to an RData file to simply open it later (hence pre-compute)
save(MilesPlotly, file = "my/path/here/myPlot.RData")
Later on I want to simply plot it by doing
load(my/path/here/myPlot.RData)
MilesPlotly
Now, this works on Mac. This does not work on my Ubuntu server on AWS.
Does anyone have any ideas why the discrepancy? The plotly version on both is 3.6.0.
Your code doesn't work on my windows environment and plotly_build() solves it (I'm not sure this code works on your env).
MilesPlotly <- plot_ly(x = TripDetails$TotalDistanceMiles, type = "histogram")
MilesPlotly <- plotly_build(MilesPlotly)
save(MilesPlotly, file = "my/path/here/myPlot.RData")
load("my/path/here/myPlot.RData")
MilesPlotly
After some tweaking, I realized I needed to do two things:
1) I updated R to 3.3.1
2) You need to "build" the plot before saving it. That means:
MilesPlotly <- plot_ly(x = TripDetails$TotalDistanceMiles, type = "histogram") %>%
build()

Exporting PNG files from Plotly in R without internet

In this question, Exporting PNG files from Plotly in R I asked how to export Plotly plots to disk.
I used the function plotly_IMAGE, but later discovered that the function uses the Plotly internet servers.
The question is, now that Plotly JavaScript is local, how can I create a png local file without internet?
I tried this code, without success:
library(plotly)
png(filename = "test.png")
plot_ly(x = 1:10)
dev.off()
The idea is to make it programaticaly, without click on export button over the chart.
They've added a new export function to the plotly package. But to my knowledge it does the same thing as #MLavoie's answer suggests. Usage:
p <- plot_ly(...)
export(p, file = "test.png")
You will to need install Phantom (http://phantomjs.org/download.html) which is quite easy and you can try this:
library(plotly)
library(webshot)
library(htmlwidgets)
m <- plot_ly(x = 1:10)
saveWidget(as.widget(m), "temp.html")
webshot("temp.html", file = "test.png",
cliprect = "viewport")
you will find temp.html and temp.png in your working directory.

Resources