Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R code formatting #5534

Open
strengejacke opened this issue Nov 27, 2024 · 8 comments
Open

R code formatting #5534

strengejacke opened this issue Nov 27, 2024 · 8 comments
Labels
area: formatting Issues related to formatting and indentation bug Something isn't working lang: r support

Comments

@strengejacke
Copy link

I'm not sure if I'm missing something, but is it possible to format R code? I have enabled settings:

  "editor.defaultFormatter": "positron.positron-r",
  "editor.formatOnType": true,

And there are some defined keybindings:

Image

But it doesn't seem to work.

Setup

Positron Version: 2024.12.0 (system setup) build 80
Code - OSS Version: 1.93.0
Commit: c3c4896
Date: 2024-11-26T02:46:23.686Z
Electron: 30.4.0
Chromium: 124.0.6367.243
Node.js: 20.15.1
V8: 12.4.254.20-electron.0
OS: Windows_NT x64 10.0.26100

R 4.4.2

E.g. this code can't be formatted.

data(mtcars)
if(mtcars$mpg>5) {
  print("hi")
}

Result in VSCode and RStudio:

data(mtcars)
if (mtcars$mpg > 5) {
  print("hi")
}
@lionel-
Copy link
Contributor

lionel- commented Nov 27, 2024

Do you have styler installed? We currently depend on it (#2251).

@lionel- lionel- added lang: r area: formatting Issues related to formatting and indentation labels Nov 27, 2024
@strengejacke
Copy link
Author

Yes:

packageVersion("styler")
#> [1] ‘1.10.3’

@strengejacke
Copy link
Author

This also happens when I press Shift+Alt+F:

Image

@zwallen
Copy link

zwallen commented Nov 27, 2024

I am also running into this problem. I have the same version of styler installed as well.

Additionally, when I have my cursor within an R chunk and try to format it via Ctrl + K and then Ctrl + F I get an error message which seems to stem from Quarto (I have it as the default formatter for notebooks).
Image

The .vdoc.r intermediate file is produced but it only shows the code of the chunk as is and additionally shows flags over certain functions like the following
Image

@juliasilge
Copy link
Contributor

juliasilge commented Nov 27, 2024

I noticed that this report plus #5531 were both on Windows, so I just tried formatting ("Format Document" and "Format Selection") with:

Positron Version: 2024.12.0 (system setup) build 80
Code - OSS Version: 1.93.0
Commit: c3c4896
Date: 2024-11-26T02:46:23.686Z
Electron: 30.4.0
Chromium: 124.0.6367.243
Node.js: 20.15.1
V8: 12.4.254.20-electron.0
OS: Windows_NT x64 10.0.19045

This did work for me correctly. So no clue yet!

Question 1

Can you make sure your R kernel log level is debug?

Image

And then can try to format and visit the Output channel for "R 4.4.0: Kernel" (or your equivalent) to confirm you don't see any messages like:

[R] 2024-11-27T21:34:11.410104Z INFO Received shell notification: JupyterMessage { zmq_identities: [[112, 111, 115, 105, 116, 114, 111, 110, 45, 115, 104, 101, 108, 108]], header: JupyterHeader { msg_id: "f3039d98-a7db-45ec-aa7a-6b845618e4fb", session: "7348364a16fb77d781f954f7237f3b19", username: "juliasilge", date: "2024-11-27T21:34:11.409Z", msg_type: "comm_msg", version: "5.0" }, parent_header: None, content: CommWireMsg { comm_id: "positron-ui-r-1-408692b6", data: Object {"jsonrpc": String("2.0"), "method": String("call_method"), "params": Object {"method": String("packageVersion"), "params": Array [String("styler"), Null]}} } }
[R] at crates/amalthea/src/socket/shell.rs:232
[R]
[R] 2024-11-27T21:34:11.412644Z INFO Received shell request: JupyterMessage { zmq_identities: [[112, 111, 115, 105, 116, 114, 111, 110, 45, 115, 104, 101, 108, 108]], header: JupyterHeader { msg_id: "7a2926b4-62d9-42b2-a70f-a84369293e64", session: "7348364a16fb77d781f954f7237f3b19", username: "juliasilge", date: "2024-11-27T21:34:11.412Z", msg_type: "execute_request", version: "5.0" }, parent_header: None, content: ExecuteRequest { code: "styler::style_file('/var/folders/hv/hzsmmyk9393_m7q3nscx1slc0000gn/T/styler-b003adf1-82bb-4929-938e-5ef446e45cea.R')", silent: true, store_history: false, user_expressions: Object {}, allow_stdin: false, stop_on_error: false } }
[R] at crates/amalthea/src/socket/shell.rs:185

Question 2

Can you confirm it is only the formatter that isn't working? We set that up here, so I'd be interested in knowing if both commands and tasks are provided correctly. Can you confirm that you can successfully use:

  • The command "R: New R File"
  • Something that is a task, such as "R: Render document with R Markdown"

Question 3

We have a new kernel supervisor in the latest release. Can you turn it off, restart Positron, and check if you still see the problem?

Image

@juliasilge juliasilge added bug Something isn't working support labels Nov 27, 2024
@lijiaqi-github
Copy link

lijiaqi-github commented Nov 28, 2024

I am reporting some of my confirmations here. Sorry I don't quite understand some of the instructions you gave, so I can only confirm the parts I understand.

Question 1

My previous R kernel log level is warn (default), I change it to info.

I downloaded the R 4.4.0, but I donot know how to visit the Output channel for "R 4.4.0: Kernel" (or your equivalent) . so I cannot confirm this instruction.

Question 2

I do not have enough knowledge to understand those code you give in that new link. so I cannot comfirm this instruction.

But ctrl + shift + n for creating New R File is working for me. Also, ctrl + shift + c and alt + - are working for me. Those are shortcuts I often used, so I can confirm that they work well.

Question 3

I turned off kernel supervisor and restarted Positron.

This is my settings.json:, you can find that I changed R kernel log level and turned off kernel supervisor.

{
"workbench.colorTheme": "Default Positron Dark",
"quarto.visualEditor.spelling": true,
"js/ts.implicitProjectConfig.checkJs": true,
"python.analysis.diagnosticMode": "workspace",
"git.confirmSync": false,
"explorer.confirmDelete": false,
"problems.defaultViewMode": "table",
"problems.showCurrentInStatus": true,
"workbench.editorAssociations": {
"*.tiff": "default"
},
"editor.renderWhitespace": "all",
"editor.autoClosingBrackets": "beforeWhitespace",
"editor.autoClosingDelete": "never",
"positron.r.pipe": "%>%",
"editor.rulers": [
80,
100
],
"editor.minimap.enabled": true,
"editor.minimap.maxColumn": 200,
"application.experimental.positronPlotsInEditorTab": true,
"rstudio.keymap.enable": true,

"positron.r.kernel.logLevel": "info",
"positronKernelSupervisor.enable": false

}

Unfortunately, after doing all this, ctrl + shift + a is still not working. Hopefully someone else can bring good news.

@zwallen
Copy link

zwallen commented Nov 29, 2024

Ok so tried my hand at your questions...

Question 1

I set the Positron > R > Kernel: Log Level to debug as suggested and got the following debug output when I performed formatting of a chunk of R code (Ctrl + K Ctrl + F):

[R]   2024-11-29T00:59:59.319401Z  INFO  Received shell notification: JupyterMessage { zmq_identities: [[114, 45, 53, 102, 57, 56, 100, 57, 98, 54], [114, 45, 53, 102, 57, 56, 100, 57, 98, 54]], header: JupyterHeader { msg_id: "5be715c9", session: "r-5f98d9b6", username: "zdw12", date: "2024-11-29T00:59:59.318Z", msg_type: "comm_msg", version: "5.3" }, parent_header: None, content: CommWireMsg { comm_id: "positron-ui-r-1-eebad9ab", data: Object {"jsonrpc": String("2.0"), "method": String("call_method"), "params": Object {"method": String("packageVersion"), "params": Array [String("styler"), Null]}} } }
[R]     at crates\amalthea\src\socket\shell.rs:232
[R] 
[R]   2024-11-29T00:59:59.331930Z  INFO  Received shell request: JupyterMessage { zmq_identities: [[114, 45, 53, 102, 57, 56, 100, 57, 98, 54], [114, 45, 53, 102, 57, 56, 100, 57, 98, 54]], header: JupyterHeader { msg_id: "e4b3e58a-9ecf-4c08-aa2f-b9771c34ef73", session: "r-5f98d9b6", username: "zdw12", date: "2024-11-29T00:59:59.331Z", msg_type: "execute_request", version: "5.3" }, parent_header: None, content: ExecuteRequest { code: "styler::style_file('C:/Users/zdw12/AppData/Local/Temp/styler-2df9a4f1-2db3-452f-907d-86a7bcee1826.R')", silent: true, store_history: false, user_expressions: Object {}, allow_stdin: true, stop_on_error: false } }
[R]     at crates\amalthea\src\socket\shell.rs:185
[R] 
[R]   2024-11-29T00:59:59.332295Z  INFO  Sending message to UI comm: Event(Busy(BusyParams { busy: true }))
[R]     at crates\ark\src\ui\sender.rs:50
[R] 

Question 2

  • Running the command R: New R File performs as expected, creating a new R file
    Image

  • Running the task R: Render document with R Markdown using the following drop-down option when an R markdown file is loaded and it renders as expected.
    Image

Question 3

As suggested, I turned off the Positron Kernel Supervisor: Enable option, but the formatting issues remain. Below is the debug output I get when I run formatting for a chunk (Ctrl+K Ctrl+F) and the Ctrl + Shift + A still doesn't work.

[R]   2024-11-29T01:20:56.753858Z  INFO  Received shell notification: JupyterMessage { zmq_identities: [[112, 111, 115, 105, 116, 114, 111, 110, 45, 115, 104, 101, 108, 108]], header: JupyterHeader { msg_id: "0949ad50-0efe-4fde-913e-423dfc283f2a", session: "146a802c014a28496c3345a88127fc6f", username: "zdw12", date: "2024-11-29T01:20:56.752Z", msg_type: "comm_msg", version: "5.0" }, parent_header: None, content: CommWireMsg { comm_id: "positron-ui-r-1-37fb16b6", data: Object {"jsonrpc": String("2.0"), "method": String("call_method"), "params": Object {"method": String("packageVersion"), "params": Array [String("styler"), Null]}} } }
[R]     at crates\amalthea\src\socket\shell.rs:232
[R] 
[R]   2024-11-29T01:20:56.762556Z  INFO  Received shell request: JupyterMessage { zmq_identities: [[112, 111, 115, 105, 116, 114, 111, 110, 45, 115, 104, 101, 108, 108]], header: JupyterHeader { msg_id: "d69e645e-cc4e-4440-be2f-cd534e5b8d2b", session: "146a802c014a28496c3345a88127fc6f", username: "zdw12", date: "2024-11-29T01:20:56.761Z", msg_type: "execute_request", version: "5.0" }, parent_header: None, content: ExecuteRequest { code: "styler::style_file('C:/Users/zdw12/AppData/Local/Temp/styler-3c6d3a54-3dbf-48fa-bb08-62a710df89ae.R')", silent: true, store_history: false, user_expressions: Object {}, allow_stdin: false, stop_on_error: false } }
[R]     at crates\amalthea\src\socket\shell.rs:185

@strengejacke
Copy link
Author

I'm not sure if my keyboard shortcut is recognized at all? I can't "format selection", only the document, which then pop ups the window saying no formatter is installed. Ctrl+Shift+A doesn't do anything.

When I press Ctrl+K, Ctrl+F, it says:
Image

Although this is supposed to happen:
Image
Image

Question 2: Creating new R files and rendering R markdown works

Question 3: yes, still nothing happens

Question 1: I tried Ctrl+Shift+A, Ctrl+K & Ctrl+F, and Shift+Alt+A, output did not change:

Image

Here's my settings file:

{
  // this is the settings.json file

  // file behaviour
  "files.hotExit": "onExitAndWindowClose",
  "files.defaultLanguage": "r",
  "files.insertFinalNewline": true,
  "files.trimFinalNewlines": true,
  "files.trimTrailingWhitespace": true,
  "positron.r.diagnostics.enable": false,
  "positron.r.kernel.logLevel": "debug",
  "positronKernelSupervisor.enable": false,

  // editor settings
  "editor.fontFamily": "'Fira Code', Consolas, 'Courier New', monospace",
  "editor.fontLigatures": true,
  "editor.guides.bracketPairs": "active",
  "editor.guides.indentation": false,
  "editor.cursorBlinking": "phase",
  "editor.renderWhitespace": "trailing",
  "editor.defaultFormatter": "positron.positron-r",
  "editor.formatOnType": true,
  "editor.rulers": [
    {
      "column": 80,
      "color": "#5a5a5a80"
    }, // right rule
    {
      "column": 95,
      "color": "#5a5a5a30"
    }, // right rule
    {
      "column": 120,
      "color": "#5a5a5a40"
    } // extra right rule
  ],
  "application.experimental.positronPlotsEditor": true,

  // todo tree
  "todo-tree.highlights.customHighlight": {
    "TODO": {
      "background": "#F5910044",
      "foreground": "#fffc00FF"
    },
    "BUG": {
      "icon": "bug",
      "background": "#D3262655",
      "foreground": "#FF3300FF"
    },
    "HACK": {
      "icon": "tools"
    },
    "FIXME": {
      "icon": "flame",
      "background": "#00996844",
      "foreground": "#25D366FF"
    }
  },

  // git settings
  "git.autofetch": true,
  "git.confirmSync": false,
  "git.suggestSmartCommit": false,

  // github settings
  "githubPullRequests.fileListLayout": "tree",
  "githubPullRequests.defaultMergeMethod": "squash",
  "githubPullRequests.pullBranch": "never",
  "githubIssues.queries": [
    {
      "label": "My Issues",
      "query": "is:open assignee:${user} repo:${owner}/${repository}",
      "groupBy": [
        "milestone"
      ]
    },
    {
      "label": "Created Issues",
      "query": "author:${user} state:open repo:${owner}/${repository} sort:created-desc"
    },
    {
      "label": "Recent Issues",
      "query": "state:open repo:${owner}/${repository} sort:created-desc"
    }
  ],

  // Codesnap
  "codesnap.boxShadow": "rgba(0, 0, 0, 0.55) 0px 20px 60px",
  "codesnap.backgroundColor": "#6090a0",
  "codesnap.containerPadding": "1em",
  "codesnap.showWindowControls": false,
  "codesnap.shutterAction": "copy",

  // Wrap at 80 columns with the "Rewrap" extension
  "rewrap.wrappingColumn": 80,

  // Debugger
  "debug.toolBarLocation": "docked",
  "chatgpt.lang": "en",
  "workbench.colorTheme": "Default Positron Dark"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: formatting Issues related to formatting and indentation bug Something isn't working lang: r support
Projects
None yet
Development

No branches or pull requests

5 participants