I want to use R-tools for files in Japanese, encode is Shift-JIS.
I think I have to use encoding='cp932' parameter in R script.
But it doesn't work by my trials...
I attached my flow.
Would someone please check my workflow?
Thanks,
Toshi
Hi @Toshi92
It looks like you have backslashes in your working directory path - R only accepts front slashes. Can you please try pasting following code into the R Tool in your workflow and let me know if it works for you?
#set wd to read from/write tosetwd("C:/Test/EncodingError_WorkAround")# helper function to install R packages. source_location optionalinstall_package <- function(package_name, source_location = FALSE) { # grab the alteryx repo altx.repo <- getOption("repos") # set your primary repo if you haven't already altx.repo["CRAN"] <- "http://cran.rstudio.com" options(repos = altx.repo) #check to see if the package we want is already installed # if it is, we will just load it, otherwise we will install it if(package_name %in% rownames(installed.packages())==FALSE){ if(source_location==FALSE) { install.packages(package_name) } else { install.packages(source_location, repos = NULL, type = "source") } } require(package_name, character.only=TRUE)}# install readrinstall_package("readr")library(readr)X <- as.matrix(read_csv("japanesetest.csv"), locale = locale(encoding = "UTF-8"))X[is.na(X)] <- 0n_rows <- nrow(X)iota <- matrix(1, nrow = 1, ncol = n_rows)XtX <- t(X) %*% XY <- iota %*% X# Calculate the similarity or distance matrixthe_matrix <- XtX/(sqrt(t(Y) %*% Y))the_matrix <- cbind(colnames(the_matrix), the_matrix)colnames(the_matrix)[1] <- "rownames"write_csv(as.data.frame(the_matrix), "outputtest.csv")
Thanks!
You could develop a batch macro that reads in an input, and place the batch macro after a block until done tool, attached to the R tool. You would also need to add a Formula Tool to update the filepath to where the UTF-8 file has been written out
The batch macro would be very simple, just a control parameter attached to an Input Tool, configured to update the file path.
Read Input Batch Macro
Action Tool Configuration
Your workflow would look like this:
Workflow with added input/output functionality
The blank blue tool is the new batch macro, and the formula tool is configured to create a new field with your filepath, which is then used as the Control Parameter for your macro.
Formula Tool ConfigurationBatch macro configuration
In order for this to work, you will also need to add the following code at the end of the script in your R tool:
write.Alteryx("output", 1)
All this code does is creates a dummy output for the R Tool, which then allows the downstream tools to run once the R code is finished executing.
Does this all make sense? Please let me know!
Thank you!
I believe the encoding errors you are experiencing are related to how Windows handles the generic read and write functions in R. If you are interested, you can read more about it here.
Can you please tell me about your Operating System? Which Windows version are you using? Which Windows language does it use (Japanese, English?)?
I've developed a work-around that reads in and writes out your data to the R tool using a R package, readr. It is not the most beautiful workaround, but it works. In order for it to run on your machine, you will need to go into the R tool and change the setwd() to the filepath your workflow is saved in.
The output will be saved in the same folder as outputtest.csv. When you read in this file to Alteryx, you will need to change the input setting to UTF-8.
You could edit any of the R based tools to work this way, you will just need to add an output tool and a block until done tool, so that the input to the R tool is written out and of Alteryx and read back in with the readr package.
Does this all make sense? Are there any questions I can answer for you? Please let me know!
Hi, SydneyF
My OS is Windows 7 Pro, the language is Japanese.
I changed the file path, but I still have the error below.
The "readr" library was downloaded correctly by your flow.
But, there is an encoding error when I call out "readr" only.
Could you confirm it?