R中file.edit()对部分文本文件无效,.dat/.inp文件如何正常调用?
file.edit() support .dat or .inp file extensions in RStudio? Great question! The short answer is: Yes, file.edit() can work with .dat and .inp files—the issue you're seeing is likely due to RStudio's default file type associations, not the function itself.
Let's break down why your current setup is behaving this way, and how to fix it:
Why only some files open
RStudio pre-configures associations for common text-based file types (like .md and .log) to open in its built-in editor. For less common extensions like .dat and .inp, it doesn't have a default text association, so file.edit() might either fail to open them in RStudio or try to use a system default program that isn't a text editor.
Fix 1: Add file type associations in RStudio
You can tell RStudio to treat .dat and .inp files as text files permanently:
- Go to Tools > Global Options > Code > Editing
- Scroll down to the File Types section
- Click the Add... button
- For .dat: Enter
datin the "Extension" field, select "Text" as the file type, then click OK - Repeat the same steps for .inp
- For .dat: Enter
- Click Apply and OK to save the settings
After this, running file.edit("d.dat") or file.edit("i.inp") should open the files directly in RStudio's editor, just like your .md and .log files.
Fix 2: Use RStudio's API for a quick workaround
If you don't want to adjust global settings, you can use RStudio's built-in API to force open any text-based file in the editor, regardless of its extension:
rstudioapi::navigateToFile("d.dat") rstudioapi::navigateToFile("i.inp")
This method bypasses the file type association check and works immediately, as long as the file is a plain text file (which you confirmed it is).
Verify your setup
To double-check that the file type associations worked, you can re-run your original file.edit() commands after applying Fix 1—they should now open the .dat and .inp files without issues.
内容的提问来源于stack exchange,提问作者Joshua Rosenberg

