简体   繁体   English

如何从 R 获取我可以立即粘贴到 Windows 中的路径字符串

[英]How to get a path string from R that I can paste immediately into Windows

Let's say I have a Windows path: "R:\\data\\state-capitals\\final\\"假设我有一个 Windows 路径: "R:\\data\\state-capitals\\final\\"

If I add "\\", I can use the path inside the R console (on a Windows system).如果我添加“\\”,我可以使用 R 控制台中的路径(在 Windows 系统上)。

mypath = "R:\\data\\state-capitals\\final\\";

If I have the R console form如果我有 R 控制台表单

mypath = "R:/data/state-capitals/final/";
normalizePath(mypath);
# "R:\\data\\state-capitals\\final"

How do I go the other way?我怎么走另一条路? How do I get R to print something to the screen that I can copy, click Win-R and paste to use File explorer to go to the location ... I want it to print something to the screen (and also the clipboard).我如何让 R 将某些内容打印到我可以复制的屏幕上,单击 Win-R 并粘贴以使用文件资源管理器转到该位置......我希望它在屏幕(以及剪贴板)上打印一些内容。

I get that nchar("\\\\") is one, but that doesn't allow me to minimize string manipulation... I want R to output something like: R:\\data\\state-capitals\\final\\ so I can copy/paste back into the Windows environment.我知道nchar("\\\\")是一个,但这不允许我最小化字符串操作......我希望 R 输出如下内容: R:\\data\\state-capitals\\final\\所以我可以复制/ 粘贴回 Windows 环境。

And a solution that keeps the trailing slash would be nice.保留尾部斜杠的解决方案会很好。


Based on @Linh, here are a few functions ...基于@Linh,这里有一些功能......

https://gist.github.com/MonteShaffer/8555de33cec2b6ffff81c268bcccc041 https://gist.github.com/MonteShaffer/8555de33cec2b6ffff81c268bcccc041

# https://stackoverflow.com/questions/64476043/
# https://stackoverflow.com/questions/1189759/
convertDirectoryR.toWin = function(myDir = getwd())  # "R:/data/state-capitals/final/"
  {
  cat( normalizePath(myDir) );
    writeClipboard( normalizePath(myDir) ); # R:\data\state-capitals\final\
  }

openDirectoryR.inWin = function(myDir = getwd())  # "R:/data/state-capitals/final/"
  {
  cmd = paste("explorer",  gsub('/', '\\\\', myDir, fixed=TRUE ) );
  suppressWarnings( shell( cmd ) );        # R:\data\state-capitals\final\
  }

convertDirectoryWin.toR = function(myDir = readClipboard()) # # R:\data\state-capitals\final\
  {
  gsub('\\', '/', myDir, fixed=TRUE );   # "R:/data/state-capitals/final/"
  }

Besides paste, how can I read winDir in?除了粘贴,我怎样才能读入 winDir? Using convertDirectoryWin.toR ?使用convertDirectoryWin.toR

I think you can use cat()我认为你可以使用cat()

mypath = "R:/data/state-capitals/final/"
cat(normalizePath(mypath))
# "R:\data\state-capitals\final\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM