1

I installed the package tabulate in RStudio by the code from the reticulate documentation.

With the simple installation code:

library(reticulate)
py_install("tabulate")

It works perfectly except the warning message: the tabulate version is low and suggest conda update. So I try the Conda installation code:

library(reticulate)
py_install("tabulate")

# create a new environment 
conda_create("r-reticulate")

# install tabulate
conda_install("r-reticulate", "tabulate")

# import tabulate (it will be automatically discovered in "r-reticulate")
 tabulate <- import("tabulate")

It does not work; but when I switch back to the simple installation, it does not work anymore-it seems like they have to stay with Conda. I wonder if I could go back to simple installation as the very beginning? I guess I need to remove the Conda environment, but I do know how to do it. I really want to remove the Conda and go back the state before Conda installation.

merv
  • 67,214
  • 13
  • 180
  • 245
Pai
  • 41
  • 5
  • What doesn't make sense here is why installing `r-reticulate` into the Conda environment? Co-installing Python (via `tabulate`) and R (via `r-reticulate`) should almost never be necessary. If you want `tabulate` then just install that, i.e., `conda_create("tabulate_py310", c("python=3.10", "tabulate"))`. And it always is helpful to specify the Python version for your Python environments. – merv Dec 24 '22 at 15:09
  • 1
    I have no idea since I am a `Python` newbie. I did that just following the instruction as follows: https://rstudio.github.io/reticulate/articles/python_packages.html#virtualenv-installation. thanks for your help! – Pai Dec 24 '22 at 16:47
  • Oh that's a very weird example they give. I'll have to look into this. – merv Dec 24 '22 at 16:59

3 Answers3

0

Doesn't seem to be in the documentation, but the code shows that if the conda_remove() method receives a packages=NULL argument (same as not specifying), then it will be translated to an --all argument, which would remove the entire environment.

## remove the entire 'r-reticulate' environment
conda_remove("r-reticulate")
merv
  • 67,214
  • 13
  • 180
  • 245
  • After I tried the code, I got the error message as follows: + "C:/Users/sspai/AppData/Local/r-miniconda/condabin/conda.bat" "remove" "--yes" "--name" "r-reticulate" "--all" DirectoryNotACondaEnvironmentError: The target directory exists, but it is not a conda environment. Use 'conda create' to convert the directory to a conda environment. target directory: C:\Users\sspai\AppData\Local\R-MINI~1\envs\r-reticulate Error: Error 1 occurred removing conda environment r-reticulate – Pai Dec 24 '22 at 16:53
  • Well, that sounds like a different problem now. If you run `py_discover_config()`, does it show that environment location? – merv Dec 24 '22 at 17:04
  • After I followed that, I got the following error message: Error in conda_python(envpath, conda = miniconda) : no conda environment exists at path 'C:/Users/sspai/AppData/Local/r-miniconda/envs/r-reticulate' – Pai Dec 24 '22 at 17:18
  • I cannot recreate this behavior. So, not much more I can help with. Sorry. – merv Dec 25 '22 at 02:35
  • Not that this answers anything, but the functionality pointed out in this answer is now documented. – merv Feb 04 '23 at 02:51
0

I am not sure if this would be the right answer-I just delete the whole folder by hand: "C:/Users/sspai/AppData/Local/r-miniconda"

And then everything worked as before I conda_installed the "tabulate" package.

My codes are as follows:

{python}
#IMPORT LIBRARIES
from bs4 import BeautifulSoup
import requests
#IMPORT CSV LIBRARY
import csv
#OPEN A NEW CSV FILE. IT CAN BE CALLED ANYTHING
file= open('csv_py.csv', 'w')
#CREATE A VARIABLE FOR WRITING TO THE CSV
writer = csv.writer(file)
#CREATE THE HEADER ROW OF THE CSV
writer.writerow(['Date', 'Content'])
#REQUEST WEBPAGE AND STORE IT AS A VARIABLE
page_to_scrape = requests.get("https://www.liechi.org/en/")
print(page_to_scrape)
#USE BEAUTIFULSOUP TO PARSE THE HTML AND STORE IT AS A VARIABLE
soup = BeautifulSoup(page_to_scrape.text, 'html.parser')
#FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'archive-item-link'
#AND STORE THE LIST AS A VARIABLE
contents= soup.findAll('a', attrs={'class':'archive-item-link'})
#FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'archive-item-date'
#AND STORE THE LIST AS A VARIABLE
 dates = soup.findAll('span', attrs={'class':'archive-item-date'})

#LOOP THROUGH BOTH LISTS USING THE 'ZIP' FUNCTION
#AND PRINT AND FORMAT THE RESULTS
for date, content in zip(dates, contents):
    clean_date = date.text.strip()
    clean_content = content.text.strip()
    print(clean_date + "(" + clean_content+ ")")
    #WRITE EACH ITEM AS A NEW ROW IN THE CSV
    writer.writerow([clean_date, clean_content])
#CLOSE THE CSV FILE
file.close()
{r include=FALSE}
library(reticulate)
py_install("tabulate")
py_install("pandas")
{python}
import pandas
from tabulate import tabulate
data=pandas.read_csv("ecommerce.csv")
print(tabulate(data,headers=data.head(), tablefmt="grid", showindex="always"))
Pai
  • 41
  • 5
-1

You can try this approach:

Remove "r-reticulate" environment:

library(reticulate)
conda_remove_env("r-reticulate")

After remove, you can install the 'tabulate' package:

library(reticulate)
py_install("tabulate")
rzz
  • 77
  • 6
  • thanks for your help, but I got this error message: `Error in conda_remove_env("r-reticulate") : could not find function "conda_remove_env"`. – Pai Dec 24 '22 at 10:00
  • Try "install.packages("reticulate")" first and them: library(reticulate). After that i think you will can use 'conda_remove_env'. Reference link of reticulate: https://rstudio.github.io/reticulate/ – rzz Dec 24 '22 at 14:47
  • I still got the same error message. Sorry! – Pai Dec 24 '22 at 17:15