library(magrittr)
library(reticulate)
# os <- import("os")
# 

# json <-
  # jsonlite::read_json("../data-raw/saved/Dominican Republic--2021-02-25-17-09-27.json")
json <-
  jsonlite::read_json("../data-raw/saved/Dominican Republic--2021-02-25-17-09-27.json")
# get_ror_curves(filename)

```{python works}

py_run_file("../data-raw/get_ror_curves.py")

py_to_r("../data-raw/get_ror_curves.py")

recomputeDeltas()

THIS WORKS!!!

x=get_ror_curves('C:/Users/jacci/Documents/DS 710/coffee_roasting_profiles/data-raw/saved/Haiti--2021-02-09-20-15-17.json')

```r

# Bring python script into R
reticulate::source_python("../data-raw/get_ror_curves.py")
# reticulate::repl_python("../data-raw/get_ror_curves.py")
# reticulate::py_run_file("../data-raw/get_ror_curves.py")
#WORKS!!
all = py$get_ror_curves(file_raw ='../data-raw/saved/Dominican Republic--2021-02-25-17-09-27.json' )
# json$timex %>% length() verify same length
# py$get_ror_curves('C:/Users/jacci/Documents/DS 710/coffee_roasting_profiles/data-raw/saved/Haiti--2021-02-09-20-15-17.json')
# Puts into a df with 2 delta columns
clean_deltas_from_python <- function(raw_deltas) {
  deltas = raw_deltas %>% dplyr::mutate_if(is.list, as.character)
  # Make all nulls into NA
  deltas[deltas == "NULL"] <- NA
  deltas[deltas == "NaN"] <- NA
  # Columns should be same type
  deltas = deltas %>% dplyr::mutate_if(is.character, as.double)

  t1 = deltas %>% dplyr::slice(1) %>% tidyr::pivot_longer(everything(), names_to = "time", values_to = "dtemp1")
  t2 = deltas %>% dplyr::slice(2) %>% tidyr::pivot_longer(everything(), names_to = "time", values_to = "dtemp2")
  dplyr::left_join(t1, t2, by = "time")
}
# deltas_take_away_charge_time
clean  = clean_deltas_from_python(all)
library(ggplot2)
clean = clean_deltas_from_python(all)
clean %>% 
ggplot() +
  geom_line(aes(x=time, y=dtemp1, group = 1),color = "blue") +
  geom_line(aes(x=time, y=dtemp2, group = 2),color = "red")

js = tibble::tibble(time = as.numeric(json$timex), temp1 = as.numeric(json$temp1), temp2 = as.numeric(json$temp2)) %>% dplyr::mutate(time = as.integer(time))

ggplot() +
  geom_line(data=js, aes(x=time, y=temp1, group = 1),color = "blue") +
   geom_line(data=js, aes(x=time, y=temp2, group = 2),color = "red")




combine = Reduce(function(x, y) merge(x, y, all=TRUE, by = "time"), list(clean, js)) %>% dplyr::distinct(time, .keep_all = TRUE)
combine %>% na.omit() %>% 
ggplot() +
  geom_line(aes(x=time, y=temp1, group = 1),color = "blue") +
   geom_line(aes(x=time, y=temp2, group = 2),color = "red") +
    geom_line(aes(x=time, y=dtemp1*10, group = 1),color = "blue") +
  geom_line(aes(x=time, y=dtemp2*10, group = 2),color = "red") +
   scale_y_continuous(
    sec.axis = sec_axis( trans=~./10, name="Second Axis"))


jacciz/coffee_roasting_profiles documentation built on Sept. 15, 2022, 6:15 a.m.