bnosac / taskscheduleR

Schedule R scripts/processes with the Windows task scheduler.
331 stars 72 forks source link

encoding problems #54

Closed earthcli closed 6 years ago

earthcli commented 6 years ago

The taskscheduleR is a wonderful package that my help me a lot. but I have a problem with using it in my windows7 system, with the default encoding of CP936, generally, there is an error when run taskscheduler_ls in R, and also in the cmd window when running schtasks.exe /query , 错误: 无法加载列资源。 when debug from the cmd: chcp 936 after run chcp 437, I can run schtasks.exe /query successfully,

so I must open a cmd window, do "chcp 437", and then open R.exe in the same cmd window, and then using the function in taskscheduleR, such as taskscheduler_ls, I wonder how I can do this in RStudio, that is change encoding in R, and then using taskscheduler_ls functions?

sessionInfo() R version 3.4.1 (2017-06-30) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale: [1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936 [2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936 [4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936

earthcli commented 6 years ago

I have tried that: options(encoding="UTF-8") taskscheduler_ls()

but it does not works.

jwijffels commented 6 years ago

Hmm, I don't know what your problem is. Can you show me the file which is generated with


cmd <- sprintf('schtasks /Query /FO CSV /V')
  x <- system(cmd, intern = TRUE)
  f <- tempfile()
writeLines(x, f)

Did you try to run the following code taskscheduler_ls(encoding = 'UTF-8') or whichever encoding you have.

earthcli commented 6 years ago

Thanks for your response. after run the script, the generated file content is : 错误: 无法加载列资源。

this is because the default encoding of my computer( in chinese), I can sucessfully run it after chcp 437

earthcli commented 6 years ago

this works:

cmd="chcp 437" x <- system(cmd, intern = TRUE) taskscheduler_ls()

HostName TaskName Next Run Time Status Logon Mode 1 360ZipUpdater N/A Ready Interactive only 2 At1 N/A Ready Interactive/Background 3 At10 N/A Ready Interactive/Background 4 At11 N/A Ready Interactive/Background

earthcli commented 6 years ago

I have another question, how can I achieve system sleep at specific time with taskscheduler_create function? Is taskscheduler_create allow a windows cmd bat file as input ?

jwijffels commented 6 years ago

Thanks for the suggest on chcp. Can you tell me what the following gives on your machine?

system("chcp 65001", intern = TRUE)
taskscheduler_ls(encoding = "UTF-8")

The package currently does not handle bat files. About system sleep, it's unclear what you mean. Do you mean taskscheduler_stop? All commands which can be passed on to taskscheduler_createare listed in the help. Extra arguments which are not listed in the help of that function are available in the schtasks.exe manual: https://github.com/bnosac/taskscheduleR/blob/master/inst/docs/schtasks.pdf and can be passed as in the schtasks_extraargument of taskscheduler_create

earthcli commented 6 years ago

Thanks for your kindly response. The "chcp 65001" command works fine on my computer. HostName TaskName Next Run Time Status Logon Mode 1 360ZipUpdater N/A Ready Interactive only 2 At1 N/A Ready Interactive/Background

the requirement of system sleep task is meaning for the computer sleep (weak up), Because there is no function for computer sleep in R, I have to write a cmd bat scrip as follow (writted in systemsleep.bat): rundll32.exe powrprof.dll,SetSuspendState

Now I can write a R scrip with one line: system(command="systemsleep.bat"), and then use taskscheduler_create to force my computer sleep at specific time, but there should be a directly way. I have learned from the source code of taskscheduler_create function, this may be achieved if you add a cmdbatfilename name as input and replace the task string if the cmdbatfilename parameter not missing. Or you can add another function for scheduler based on general windows bat file.

Thank you very much for your work.

jwijffels commented 6 years ago

I made a fix by changing the codepage to 65001 (UTF-8). Current taskscheduler_create does not handle .bat files. Feel free to make a pull request with the changes needed to support these.

jwijffels commented 6 years ago

Closing this as fix was done changing the codepage to UTF-8. If you want to let the taskscheduler_create to handle bat files, please create a new issue.

Vidaringa commented 5 years ago

I'm having the same issues but I don't know how to change the encoding. I'm using the addin in RStudio. How can I change the encoding to UTF-8 (which I think will solve my problem)?

jwijffels commented 5 years ago

which version of taskscheduleR are you using?

Vidaringa commented 5 years ago

I have the latest version, 1.4

jwijffels commented 5 years ago

What does the following give you

installed.packages()["taskscheduleR", ]
taskscheduler_ls()

system("chcp 65001", intern = TRUE)
cmd <- sprintf('schtasks /Query /FO CSV /V')
system(cmd, intern = TRUE)
Vidaringa commented 5 years ago

I'm not 100% sure what you want me to paste but I assume you want this?

"\"COMPUTERNAME\",\"\elko1.R\",\"6.2.2019 18:30:00\",\"Ready\",\"Interactive only\",\"30.11.1999 00:00:00\",\"267011\",\"COMPANY\user\",\"cmd /c C:/PROGRA~1/R/R-35~1.2/bin/Rscript.exe \"H:/Rwd/gagnasofnun/elko/elko1.R\" >> \"H:/Rwd/gagnasofnun/elko/elko1.log\" 2>&1\",\"N/A\",\"N/A\",\"Enabled\",\"Disabled\",\"Stop On Battery Mode, No Start On Batteries\",\"user\",\"Disabled\",\"72:00:00\",\"Scheduling data is not available in this format.\",\"Daily \",\"18:30:00\",\"6.2.2019\",\"N/A\",\"Every 1 day(s)\",\"N/A\",\"Disabled\",\"Disabled\",\"Disabled\",\"Disabled\""

Vidaringa commented 5 years ago

taskscheduler_ls() gives this warning message

Warning message: In data.table::fread(f, encoding = encoding, ...) : Found and resolved improper quoting in first 100 rows. If the fields are not quoted (e.g. field separator does not appear within any field), try quote="" to avoid this warning.

jwijffels commented 5 years ago

The full output of the code and what it shows is needed to see what your problem is.

Vidaringa commented 5 years ago

I solved the problem. Instead of scheduling myscript.R I now schedule myscript_2.R where myscript_2.R has only one line of code: source(myscript.R, encoding = "UTF-8").

jwijffels commented 5 years ago

That doesn't look like you had an encoding problem as mentioned in this thread but only R code with a different encoding than e.g. ASCII

samuelreuther commented 5 years ago

Thanks Vidaringa, Thanks to your hint, I did the same workaround to schedule a script with UTF-8 encoding. I have set the global option in RStudio for this encoding, but it did not work. Maybe there are other options to set in profile, which I didn't find out. It would be good if there would be an option in taskscheduler_create() to set encoding. Anyways, thank you for taskscheduleR! Great work!